【问题标题】:remove the borders from grid.arrange从 grid.arrange 中删除边框
【发布时间】:2012-12-05 16:51:51
【问题描述】:

我正在使用 gridExtra 包 grid.arrange 函数组合多个 ggplot 图。

我这样做:

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)

此命令在 p1 和 p2 之间设置边界。我在 grid.arrange 中找不到有关删除边框的任何信息。可以去掉边框吗?

【问题讨论】:

    标签: r ggplot2 gridextra


    【解决方案1】:

    gridExtra 没有在图之间添加任何额外的边界。您所看到的只是已经围绕每个地块的边界。即p1底部有边框,p2顶部有边框。将两者放在一起,可能看起来两者之间还有额外的空间。

    要删除或调整每个绘图的边界,请使用theme 函数中的plot.margin 元素。下面删除 p1 的下边距和 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)
    

    编辑 (16/07/2015):gridExtra >= 2.0.0,main 参数已重命名为 top

    【讨论】:

      【解决方案2】:

      有点晚了,但我遇到了同样的问题,我想我已经找到了解决方案。 plot.margin 没有帮助,但是将 panel.border 和 plot.background(参数颜色和填充)调整为绘图的背景颜色就可以了。

      【讨论】:

      • 请添加一个简单的示例来执行您的建议。
      猜你喜欢
      • 2010-10-21
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      相关资源
      最近更新 更多