ggplot2 - Any way to have fine control on facet position?

I am plotting a ggplot2 graph that has an uneven number of facets. My plot currently looks something like this:

library(ggplot2)

df <- data.frame(group=c(1,1,2,2,3),
                 name=c('a','b','c','d','e'),
                 x=c(1,2,3,4,5),
                 y=c(2,3,4,5,6))
ggplot(df, aes(x,y)) + geom_point() + facet_wrap(~ name, ncol=3) 

Note that the default positioning for the facets is:

a b c
d e

However, I would like to have a finer control on the positioning of the facets. Specifically, I would like to "center" the bottom two facets in order to distribute the void space more evenly on the sides of the plot.

In other words, the ideal placement for me would look like:

a b c
 d e

Is this something that can be achieved with ggplot2? How? Many thanks in advance.

My solution may not be the best.

p_a = ggplot(df%>%filter(name=="a"), aes(x,y)) + geom_point()
p_b = ggplot(df%>%filter(name=="b"), aes(x,y)) + geom_point()
p_c = ggplot(df%>%filter(name=="c"), aes(x,y)) + geom_point()
p_d = ggplot(df%>%filter(name=="d"), aes(x,y)) + geom_point()
p_e = ggplot(df%>%filter(name=="e"), aes(x,y)) + geom_point()

library(patchwork)
(p_a | p_b | p_c) /
(p_d | p_e)

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.