grid.arrange的背景

grid.arrange的背景,r,ggplot2,r-grid,R,Ggplot2,R Grid,我已经做了一个像下面的代码描述的一个在张贴的图像结果的情节。我不知道如何将整个背景设置为定义子地块时使用的同一种“灰色80”颜色。也就是说,我想用同一种颜色给绘图之间和图例两侧的白色区域上色。 这是可能实现的,也许用一些奇特的gridgrob魔法 这个问题与类似,但如果可能的话,我想要一个不使用png()函数的解决方案 library(ggplot2) library(gridExtra) library(ggthemes) library(grid) p1 <- ggplot(iris

我已经做了一个像下面的代码描述的一个在张贴的图像结果的情节。我不知道如何将整个背景设置为定义子地块时使用的同一种“灰色80”颜色。也就是说,我想用同一种颜色给绘图之间和图例两侧的白色区域上色。 这是可能实现的,也许用一些奇特的gridgrob魔法

这个问题与类似,但如果可能的话,我想要一个不使用png()函数的解决方案

library(ggplot2)
library(gridExtra)
library(ggthemes)
library(grid)

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width,
                       colour = Species)) + 
  ggtitle('Sepal') + 
  geom_point() + theme_bw() + 
  theme(rect = element_rect(fill = 'grey80'))


p2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width,
                       colour = Species)) + 
  ggtitle('Petal') + 
  geom_point() + theme_bw() + 
  theme(rect = element_rect(fill = 'grey80'))

grid_arrange_shared_legend <- function(...) {
  plots <- list(...)
  g <- ggplotGrob(plots[[1]] + theme(legend.position = "bottom"))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  grid.arrange(
    do.call(arrangeGrob, lapply(plots, function(x)
      x + theme(legend.position="none"))),
    legend,
    ncol = 1,
    heights = unit.c(unit(1, "npc") - lheight, lheight))
}

grid_arrange_shared_legend(p1,p2)
库(ggplot2)
图书馆(gridExtra)
图书馆(主题)
图书馆(网格)
p1升级评论

可以通过在图形窗口中添加灰色背景和 然后在顶部添加绘图。当您的图例函数使用
网格时,排列
将生成一个新页面;因此,在函数中添加
newpage=FALSE
或更改为
arrangeGrob

你的榜样

library(ggplot2)
library(gridExtra)
library(ggthemes)
library(grid)

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) + 
         ggtitle('Sepal') + 
         geom_point() + theme_bw() + 
         # by adding colour=grey removes the white border of the plot and
         # so removes the lines between the plots
         # add panel.background = element_rect(fill = "grey80") 
         # if you want the plot panel grey aswell
         theme(plot.background=element_rect(fill="grey80", colour="grey80"),
               rect = element_rect(fill = 'grey80'))

p2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width, colour = Species)) + 
         ggtitle('Petal') + 
         geom_point() + theme_bw() + 
         theme(plot.background=element_rect(fill="grey80", colour="grey80"),
               rect = element_rect(fill = 'grey80'))


我认为您可以利用所示的
cowplot
包中的
ggdraw()
函数

在您的情况下,只需在每个绘图
p1
p2
主题()中添加
plot.background=element\u rect(fill=“grey80”,color=NA)
,将它们与函数
grid\u arrange\u shared\u legend()
缝合在一起,然后在其输出上调用
ggdraw()

g <- grid_arrange_shared_legend(p1,p2)
g2 <- cowplot::ggdraw(g) + 
  # same plot.background should be in the theme of p1 and p2 as mentioned above
  theme(plot.background = element_rect(fill="grey80", color = NA))
plot(g2)

g just add panel.background=element_rect(fill=“grey80”)感谢您的评论MLavoie,但这会改变绘图区域的背景,我有兴趣更改绘图外部的颜色。太好了!这就是我需要的解决方案。为了保持一致性,我将使用arrangeGrob()。我还将col=“grey80”添加到gpar()-函数中,以消除黑色边框。关于如何删除边框,有什么想法吗?
grid.draw(grobTree(rectGrob(gp=gpar(lwd=0))
在这个图上几乎看不到。毫不奇怪,没有人对此抱怨。但是我将这个解决方案应用到另一个有更多间距的图中,使得边缘非常明显。
grid.draw(grobTree(rectGrob(gp=gpar(fill="grey80", lwd=0)), 
                   grid_arrange_shared_legend(p1,p2)))
g <- grid_arrange_shared_legend(p1,p2)
g2 <- cowplot::ggdraw(g) + 
  # same plot.background should be in the theme of p1 and p2 as mentioned above
  theme(plot.background = element_rect(fill="grey80", color = NA))
plot(g2)