remove the borders from grid.arrange

拈花ヽ惹草 提交于 2019-12-10 20:07:29

问题


I am combining multip ggplot graphs using gridExtra package grid.arrange function.

I doing this:

p1<-ggplot(x, aes(Date, Value)) + geom_line()
p2<-ggplot(y, aes(Date, Score)) + geom_point()
grid.arrange(p1, p2,  main=textGrob("Head Line", gp=gpar(cex=1.5, fontface="bold", col="#990000")), ncol = 1, clip=TRUE)

this command puts border between p1 and p2. I couldn't find any info on removeing the borders in grid.arrange. Is it possible to remove the borders?


回答1:


gridExtra doesn't put any additional border between the plots. All you are seeing are the borders that already surround each plot. That is, there is a border at the bottom of p1 and a border at the top of p2. Put the two together, and it might look like there is additional space between the two.

To remove or to adjust each plot's borders, use the plot.margin element in the theme function. The following removes the bottom margin of p1 and the top margin of p2.

library(ggplot2)
library(gridExtra)

p1<-ggplot(data.frame(x = 1:10, y = 1:10), aes(x, y)) + geom_line() +
      theme(plot.margin = unit(c(1,1,0,1), "lines"))

p2<-ggplot(data.frame(x = 1:10, y = 1:10), aes(x, y)) + geom_point() +
   theme(plot.margin = unit(c(0,1,1,1), "lines"))

grid.arrange(p1, p2,  top=textGrob("Head Line", 
     gp=gpar(cex=1.5, fontface="bold", col="#990000")), ncol = 1, clip=TRUE)

Edit (16/07/2015): with gridExtra >= 2.0.0, the main parameter has been renamed top.



来源:https://stackoverflow.com/questions/13728272/remove-the-borders-from-grid-arrange

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!