Adding x and y laxis label to ggplot-grid build wi

2019-03-03 21:46发布

问题:

I produced a grid with cowplot:

library(ggplot2)
library(cowplot)

ggg1 <- ggplot(mtcars, aes(mpg,vs)) + geom_point() +
  theme(axis.title.x=element_blank(),
        axis.title.y=element_blank())
ggg2 <- ggplot(mtcars, aes(mpg,vs)) + geom_point() +
  theme(axis.title.x=element_blank(),
        axis.title.y=element_blank())
ggg3 <- ggplot(mtcars, aes(mpg,vs)) + geom_point() +
  theme(axis.title.x=element_blank(),
        axis.title.y=element_blank())
plot_grid(plot_grid(ggg1, ggg2, labels=c("", ""), ncol = 1), ggg3, labels=c("", ""), ncol = 2)

resulting in the image

(of course this is just a minimal working example).

Now I would like to have a xaxis with the title "mpg" and a yaxis with the title "vs" - each centered like so:

How can I do this with ggplot. Nothing I tried with add_sub or draw_label or How do I customize the margin and label settings with plot_grid? worked. The answer to ggplot: how to add common x and y labels to a grid of plots does not use cowplot. Can I do this with cowplot?

Important: I want to be able to set the size of the font of the labels.

回答1:

This seems to work...

plot_grid(plot_grid(ggg1, ggg2, labels=c("", ""), ncol = 1), 
          ggg3, 
          labels=c("", ""), 
          ncol =2,  
          scale=0.9) + #perhaps reduce this for a bit more space
draw_label("xlab", x=0.5, y=  0, vjust=-0.5, angle= 0) +
draw_label("ylab", x=  0, y=0.5, vjust= 1.5, angle=90)



回答2:

here's an alternative strategy

library(grid)
library(gridExtra)
library(egg)
grid.arrange(gtable_cbind(gtable_frame(ggarrange(ggg1, ggg2, draw=F)), 
                          gtable_frame(ggplotGrob(ggg3))),
             left = textGrob("Left",rot=90), 
             bottom = textGrob("Bottom"))

enter image description here