grid.arrange 的背景

Gab*_*bra 6 r ggplot2 r-grid

我制作了一个类似于下面代码所描述的图,从而生成了发布的图像。我不知道如何将整个背景设置为与定义子图时使用的相同的“grey80”颜色。IE。我想用相同的颜色为绘图之间和图例两侧的白色区域着色。这是否有可能实现,也许是通过一些奇特的 gridgrob-magic?

这个问题类似于更改 grid.arrange 输出的背景颜色,但如果可能的话,我想要一个不使用 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)
Run Code Online (Sandbox Code Playgroud)

网格排列图

use*_*650 7

升级评论

您可以通过向图形窗口添加灰色背景,然后在顶部添加绘图来实现此目的。当您的图例函数使用它时,grid.arrange它会生成一个新页面;所以添加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'))
Run Code Online (Sandbox Code Playgroud)

调整你的功能

# Change grid.arrange to arrangeGrob
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)
  arrangeGrob( # change here
    do.call(arrangeGrob, lapply(plots, function(x)
                                   x + theme(legend.position="none"))),
    legend,
    ncol = 1,
    heights = unit.c(unit(1, "npc") - lheight, lheight))
}
Run Code Online (Sandbox Code Playgroud)

阴谋

grid.draw(grobTree(rectGrob(gp=gpar(fill="grey80", lwd=0)), 
                   grid_arrange_shared_legend(p1,p2)))
Run Code Online (Sandbox Code Playgroud)

这使

在此处输入图片说明