Add a title below the four-graph chart in ggplot

Due to data privacy, I use the mtcar dataset in ggplot2 to explain my question.

There are four graphs:

 g1 <- ggplot(mtcars,aes(mpg,wt)) + geom_point() g2 <- ggplot(mtcars,aes(mpg,disp)) + geom_point() g3 <- ggplot(mtcars,aes(mpg,drat)) + geom_point() g4 <- ggplot(mtcars,aes(mpg,qsec)) + geom_point() 

I want to put these four graphs in one graph, so I use the grid.arrange() function in the grid.Extra package:

 grid.arrange(g1,g2,g3,g4,ncol=2) 

raw graph

And now, I want to add a title under each plot on this graph, something like the picture below (I modified it in Word, so it’s not beautiful) modified pic Before the request, I searched in SO , and I know how to add a title below one graph, for example, use grid.text() or these three methods. Display text under the plot generated by ggplot2 or element_text(vjust=-10) , but I can’t apply it to four charts in one chart. Meanwhile, I have some results in the base plot. How to add a title for each plot in R? or The general main title is the shape bar compiled with the par (mfrow) parameter , qustion is what I want to hold it in ggplot2 , and the title below each chart, how to implement it? Thanks!

+6
source share
1 answer

you can transfer each chart with arrGrob first,

 g1 <- g2 <- g3 <- g4 <- ggplot() titles = LETTERS[1:4] plots = mapply(arrangeGrob, list(g1,g2,g3,g4), bottom = titles, SIMPLIFY=FALSE) grid.arrange(grobs = plots, ncol=2) 

enter image description here

+7
source

All Articles