grid.arrange 的背景
Background of grid.arrange
我制作了一个类似于下面代码所描述的情节,从而产生了发布的图像。我不知道如何将整个背景设置为我在定义子图时使用的相同 "grey80"-color。 IE。我想用相同的颜色为图之间和图例两侧的白色区域着色。
这有可能实现吗,也许需要一些花哨的 gridgrob-magic?
这个问题类似于change the background color of grid.arrange output,但如果可能的话,我想要一个不使用 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)
升级评论
您可以通过向图形添加灰色背景来做到这一点 window 并且
然后在顶部添加地块。由于您的图例函数使用 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'))
调整你的功能
# 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))
}
情节
grid.draw(grobTree(rectGrob(gp=gpar(fill="grey80", lwd=0)),
grid_arrange_shared_legend(p1,p2)))
给出
我认为您可以利用 cowplot
包中的 ggdraw()
函数,如图 here.
在你的情况下,你只需要在每个情节 p1
和 p2
的 theme()
中添加 plot.background = element_rect(fill="grey80", color = NA)
,将它们与你的函数 grid_arrange_shared_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)
我制作了一个类似于下面代码所描述的情节,从而产生了发布的图像。我不知道如何将整个背景设置为我在定义子图时使用的相同 "grey80"-color。 IE。我想用相同的颜色为图之间和图例两侧的白色区域着色。 这有可能实现吗,也许需要一些花哨的 gridgrob-magic?
这个问题类似于change the background color of grid.arrange output,但如果可能的话,我想要一个不使用 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)
升级评论
您可以通过向图形添加灰色背景来做到这一点 window 并且
然后在顶部添加地块。由于您的图例函数使用 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'))
调整你的功能
# 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))
}
情节
grid.draw(grobTree(rectGrob(gp=gpar(fill="grey80", lwd=0)),
grid_arrange_shared_legend(p1,p2)))
给出
我认为您可以利用 cowplot
包中的 ggdraw()
函数,如图 here.
在你的情况下,你只需要在每个情节 p1
和 p2
的 theme()
中添加 plot.background = element_rect(fill="grey80", color = NA)
,将它们与你的函数 grid_arrange_shared_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)