I'm using egg
to align multiple plots on a page. I'm wondering if it's possible to align two columns by the titles a)
and c)
instead of plot area? Thanks!
Code:
library(egg)
library(grid)
p1 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point() + ggtitle("a)")
p1
p2 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point() + facet_wrap(~ cyl, ncol = 2, scales = "free") +
guides(colour = "none") +
theme() + ggtitle("b)")
p2
p3 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point() + facet_grid(. ~ am, scales = "free") + guides(colour="none") +
ggtitle("c)")
p3
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
g3 <- ggplotGrob(p3)
fg1 <- gtable_frame(g1, debug = TRUE)
fg2 <- gtable_frame(g2, debug = TRUE)
fg12 <- gtable_frame(gtable_rbind(fg1, fg2),
width = unit(2, "null"),
height = unit(1, "null"))
fg3 <-
gtable_frame(
g3,
width = unit(2, "null"),
height = unit(1, "null"),
debug = TRUE
)
grid.newpage()
combined <- gtable_cbind(fg12, fg3)
grid.draw(combined)
Plot:
I found another way by using
cowplot
packageSee also here
Edit:
A recently developed package
patchwork
forggplot2
can also get the job doneAdding a blank dummy faceting variable to plot
p1
/a)
seems like the easiest solution