使用 plot_grid 更改背景颜色
Change background color with plot_grid
如何在使用 plot_grid 时更改背景颜色?我有以下图形,但我希望背景中的所有内容都是灰色的并且没有高度差异。我怎样才能改变这个?
这是我的图形和数据代码:
数据
set.seed(123456)
Test_1 <- round(rnorm(20,mean=35,sd=3),0)/100
Test_2 <- round(rnorm(20,mean=70,sd=3),0)/100
ei.data <- as.data.frame(cbind(Test_1,Test_2))
intercept <- as.data.frame(matrix(0,20,1))
slope <- as.data.frame(matrix(0,20,1))
data <- cbind(intercept,slope)
colnames(data) <- c("intercept","slope")
for (i in 1:nrow(ei.data)){
data[i,1] <- (ei.data[i,2]/(1-ei.data[i,1]))
data[i,2] <- ((ei.data[i,1]/(1-ei.data[i,1]))*(-1))
}
左图
p <- ggplot(data, aes(Test_1,Test_2))+
geom_point(shape=1,size=1)+
theme_bw()+
xlab(TeX("$n_{1,i}$"))+
ylab(TeX("$t_{1,i}$"))+
scale_y_continuous(limits=c(0,1),breaks=seq(0,1,0.2))+
scale_x_continuous(limits = c(0,1),breaks=seq(0,1,0.2))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "grey92", colour = NA),
plot.background = element_rect(fill = "grey92", colour = NA),
axis.line = element_line(colour = "black"))+
theme(aspect.ratio=1)
p
右图
df <- data.frame()
q <- ggplot(df)+
geom_point()+
theme_bw()+
scale_y_continuous(limits = c(0, 1),breaks=seq(0,1,0.2))+
scale_x_continuous(limits = c(0, 1),breaks=seq(0,1,0.2))+
xlab(TeX("$\beta_i^{1,1}"))+
ylab(TeX("$\beta_i^{2,1}"))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "grey92", colour = NA),
plot.background = element_rect(fill = "grey92", colour = NA), axis.line = element_line(colour = "black"))+
theme(aspect.ratio=1)+
geom_abline(slope =data[1,2] , intercept =data[1,1], size = 0.3)+
geom_abline(slope =data[2,2] , intercept =data[2,1], size = 0.3)+
geom_abline(slope =data[3,2] , intercept =data[3,1], size = 0.3)+
geom_abline(slope =data[4,2] , intercept =data[4,1], size = 0.3)+
geom_abline(slope =data[5,2] , intercept =data[5,1], size = 0.3)+
geom_abline(slope =data[6,2] , intercept =data[6,1], size = 0.3)+
geom_abline(slope =data[7,2] , intercept =data[7,1], size = 0.3)+
geom_abline(slope =data[8,2] , intercept =data[8,1], size = 0.3)+
geom_abline(slope =data[9,2] , intercept =data[9,1], size = 0.3)+
geom_abline(slope =data[10,2] , intercept =data[10,1], size = 0.3)+
geom_abline(slope =data[11,2] , intercept =data[11,1], size = 0.3)+
geom_abline(slope =data[12,2] , intercept =data[12,1], size = 0.3)+
geom_abline(slope =data[13,2] , intercept =data[13,1], size = 0.3)+
geom_abline(slope =data[14,2] , intercept =data[14,1], size = 0.3)+
geom_abline(slope =data[15,2] , intercept =data[15,1], size = 0.3)+
geom_abline(slope =data[16,2] , intercept =data[16,1], size = 0.3)+
geom_abline(slope =data[17,2] , intercept =data[17,1], size = 0.3)+
geom_abline(slope =data[18,2] , intercept =data[18,1], size = 0.3)+
geom_abline(slope =data[19,2] , intercept =data[19,1], size = 0.3)+
geom_abline(slope =data[20,2] , intercept =data[20,1], size = 0.3)
q
正在安排
plot_grid(p,q,ncol=2, align = "v")
使用 png()
您可以通过更改 bg
:
来正确保存图像
png(bg = "grey92") # set the same bg
cowplot::plot_grid(p,q,ncol=2, align = "v")
#gridExtra::grid.arrange(p,q,ncol=2)
dev.off()
更新:
有了这个你甚至可以删除图形中的白色边框(无需保存png
):
library(gridExtra)
library(grid)
grid.draw(grobTree(rectGrob(gp=gpar(fill="grey92", lwd=0)), # this changes the bg in the graphics (R viewer)
arrangeGrob(p,q,ncol=2)))
由于您以相同的方式自定义绘图,因此让我们更轻松地调整这些自定义(如果您改变主意):
theme_plt <- function() {
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "grey92", colour = NA),
plot.background = element_rect(fill = "grey92", colour = NA),
axis.line = element_line(colour = "black")
) +
theme(aspect.ratio = 1)
}
common_scales <- function() {
list(
scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2)),
scale_x_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2))
)
}
您的左图调用使用了错误的 data
参数,已在此处修复:
ggplot(ei.data, aes(Test_1, Test_2)) +
geom_point(shape = 1, size = 1) +
common_scales() +
labs(
x = TeX("$n_{1,i}$"), y = TeX("$t_{1,i}$")
) +
theme_plt() -> gg1
您可以通过以下方式简化 abline
重复:
ggplot() +
geom_point() +
geom_abline(
data = data, aes(slope = slope, intercept = intercept), size = 0.3
) +
common_scales() +
labs(
x = TeX("$\beta_i^{1,1}"), y = TeX("$\beta_i^{2,1}")
) +
theme_plt() -> gg2
现在,高度差异的原因是由于正确的情节同时具有子脚本和超级脚本。因此,我们可以通过以下方式确保所有位的高度相同(因为这些地块具有相同的共同地块区域元素):
gt1 <- ggplot_gtable(ggplot_build(gg1))
gt2 <- ggplot_gtable(ggplot_build(gg2))
gt1$heights <- gt2$heights
一起来看看:
cowplot::plot_grid(gt1, gt2, ncol = 2, align = "v")
你无法从 ^^ 看出,但由于你设置的 aspect.ratio
,图表上方和下方有一个水平的白色 margin/border。 RStudio 永远不会用白色以外的任何其他颜色显示它(mebbe,最终可能在 1.2 中 "dark" 模式下 "black" )。
其他绘图设备具有您可以指定的 bg
颜色。我们可以使用 magick
设备并适当地设置 height/width 以确保没有白色 borders/margin:
image_graph(900, 446, bg = "grey92")
cowplot::plot_grid(gt1, gt2, ncol = 2, align = "v")
dev.off()
^^ 仍然 看起来 就像它在 RStudio 中有一个 top/bottom 边框,如果图 pane/window 的大小不是根据纵横比而是根据实际plot "image" 不会有任何.
我认为提供的各种解决方案过于复杂。因为 cowplot::plot_grid()
returns 一个新的 ggplot2 对象,您可以使用 ggplot2 的主题机制简单地设置样式。
首先是问题代码的可重现示例,已简化
library(ggplot2)
library(latex2exp)
set.seed(123456)
Test_1 <- round(rnorm(20,mean=35,sd=3),0)/100
Test_2 <- round(rnorm(20,mean=70,sd=3),0)/100
ei.data <- as.data.frame(cbind(Test_1,Test_2))
intercept <- as.data.frame(matrix(0,20,1))
slope <- as.data.frame(matrix(0,20,1))
data <- cbind(intercept,slope)
colnames(data) <- c("intercept","slope")
for (i in 1:nrow(ei.data)){
data[i,1] <- (ei.data[i,2]/(1-ei.data[i,1]))
data[i,2] <- ((ei.data[i,1]/(1-ei.data[i,1]))*(-1))
}
theme_plt <- function() {
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "grey92", colour = NA),
plot.background = element_rect(fill = "grey92", colour = NA),
axis.line = element_line(colour = "black")
) +
theme(aspect.ratio = 1)
}
common_scales <- function() {
list(
scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2)),
scale_x_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2))
)
}
ggplot(ei.data, aes(Test_1, Test_2)) +
geom_point(shape = 1, size = 1) +
common_scales() +
labs(
x = TeX("$n_{1,i}$"), y = TeX("$t_{1,i}$")
) +
theme_plt() -> gg1
ggplot() +
geom_point() +
geom_abline(
data = data, aes(slope = slope, intercept = intercept), size = 0.3
) +
common_scales() +
labs(
x = TeX("$\beta_i^{1,1}"), y = TeX("$\beta_i^{2,1}")
) +
theme_plt() -> gg2
cowplot::plot_grid(gg1, gg2, align = "v")
正如我们所见,两个图形的尺寸略有不同,因此背景不匹配。
解决方法是在plot_grid()
调用后简单地添加一个主题语句:
cowplot::plot_grid(gg1, gg2, align = "v") +
theme(plot.background = element_rect(fill = "grey92", colour = NA))
这已经创建了所选颜色的统一背景。您当然必须调整绘图的输出尺寸以避免两个数字上方和下方的大量灰色。
为了更清楚地突出显示正在发生的事情,让我们用不同的颜色选择组合图的样式:
cowplot::plot_grid(gg1, gg2, align = "v") +
theme(plot.background = element_rect(fill = "cornsilk", colour = "blue"))
我们可以看到主题陈述应用于canvas,plot_grid()
将两个情节粘贴到canvas。
最后,我们可以问一开始为什么会出现这个问题,答案是因为绘图没有对齐。为了使它们完美对齐,我们需要垂直和水平对齐,当我们这样做时,事情会按预期工作:
cowplot::plot_grid(gg1, gg2, align = "vh")
通常情况下,align = "h"
就足够了(当绘图放在同一行时,align = "v"
是不正确的),但是因为主题有固定的纵横比,所以我们需要水平和对齐垂直,因此 align = "vh"
.
如何在使用 plot_grid 时更改背景颜色?我有以下图形,但我希望背景中的所有内容都是灰色的并且没有高度差异。我怎样才能改变这个?
这是我的图形和数据代码:
数据
set.seed(123456)
Test_1 <- round(rnorm(20,mean=35,sd=3),0)/100
Test_2 <- round(rnorm(20,mean=70,sd=3),0)/100
ei.data <- as.data.frame(cbind(Test_1,Test_2))
intercept <- as.data.frame(matrix(0,20,1))
slope <- as.data.frame(matrix(0,20,1))
data <- cbind(intercept,slope)
colnames(data) <- c("intercept","slope")
for (i in 1:nrow(ei.data)){
data[i,1] <- (ei.data[i,2]/(1-ei.data[i,1]))
data[i,2] <- ((ei.data[i,1]/(1-ei.data[i,1]))*(-1))
}
左图
p <- ggplot(data, aes(Test_1,Test_2))+
geom_point(shape=1,size=1)+
theme_bw()+
xlab(TeX("$n_{1,i}$"))+
ylab(TeX("$t_{1,i}$"))+
scale_y_continuous(limits=c(0,1),breaks=seq(0,1,0.2))+
scale_x_continuous(limits = c(0,1),breaks=seq(0,1,0.2))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "grey92", colour = NA),
plot.background = element_rect(fill = "grey92", colour = NA),
axis.line = element_line(colour = "black"))+
theme(aspect.ratio=1)
p
右图
df <- data.frame()
q <- ggplot(df)+
geom_point()+
theme_bw()+
scale_y_continuous(limits = c(0, 1),breaks=seq(0,1,0.2))+
scale_x_continuous(limits = c(0, 1),breaks=seq(0,1,0.2))+
xlab(TeX("$\beta_i^{1,1}"))+
ylab(TeX("$\beta_i^{2,1}"))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "grey92", colour = NA),
plot.background = element_rect(fill = "grey92", colour = NA), axis.line = element_line(colour = "black"))+
theme(aspect.ratio=1)+
geom_abline(slope =data[1,2] , intercept =data[1,1], size = 0.3)+
geom_abline(slope =data[2,2] , intercept =data[2,1], size = 0.3)+
geom_abline(slope =data[3,2] , intercept =data[3,1], size = 0.3)+
geom_abline(slope =data[4,2] , intercept =data[4,1], size = 0.3)+
geom_abline(slope =data[5,2] , intercept =data[5,1], size = 0.3)+
geom_abline(slope =data[6,2] , intercept =data[6,1], size = 0.3)+
geom_abline(slope =data[7,2] , intercept =data[7,1], size = 0.3)+
geom_abline(slope =data[8,2] , intercept =data[8,1], size = 0.3)+
geom_abline(slope =data[9,2] , intercept =data[9,1], size = 0.3)+
geom_abline(slope =data[10,2] , intercept =data[10,1], size = 0.3)+
geom_abline(slope =data[11,2] , intercept =data[11,1], size = 0.3)+
geom_abline(slope =data[12,2] , intercept =data[12,1], size = 0.3)+
geom_abline(slope =data[13,2] , intercept =data[13,1], size = 0.3)+
geom_abline(slope =data[14,2] , intercept =data[14,1], size = 0.3)+
geom_abline(slope =data[15,2] , intercept =data[15,1], size = 0.3)+
geom_abline(slope =data[16,2] , intercept =data[16,1], size = 0.3)+
geom_abline(slope =data[17,2] , intercept =data[17,1], size = 0.3)+
geom_abline(slope =data[18,2] , intercept =data[18,1], size = 0.3)+
geom_abline(slope =data[19,2] , intercept =data[19,1], size = 0.3)+
geom_abline(slope =data[20,2] , intercept =data[20,1], size = 0.3)
q
正在安排
plot_grid(p,q,ncol=2, align = "v")
使用 png()
您可以通过更改 bg
:
png(bg = "grey92") # set the same bg
cowplot::plot_grid(p,q,ncol=2, align = "v")
#gridExtra::grid.arrange(p,q,ncol=2)
dev.off()
更新:
有了这个你甚至可以删除图形中的白色边框(无需保存png
):
library(gridExtra)
library(grid)
grid.draw(grobTree(rectGrob(gp=gpar(fill="grey92", lwd=0)), # this changes the bg in the graphics (R viewer)
arrangeGrob(p,q,ncol=2)))
由于您以相同的方式自定义绘图,因此让我们更轻松地调整这些自定义(如果您改变主意):
theme_plt <- function() {
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "grey92", colour = NA),
plot.background = element_rect(fill = "grey92", colour = NA),
axis.line = element_line(colour = "black")
) +
theme(aspect.ratio = 1)
}
common_scales <- function() {
list(
scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2)),
scale_x_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2))
)
}
您的左图调用使用了错误的 data
参数,已在此处修复:
ggplot(ei.data, aes(Test_1, Test_2)) +
geom_point(shape = 1, size = 1) +
common_scales() +
labs(
x = TeX("$n_{1,i}$"), y = TeX("$t_{1,i}$")
) +
theme_plt() -> gg1
您可以通过以下方式简化 abline
重复:
ggplot() +
geom_point() +
geom_abline(
data = data, aes(slope = slope, intercept = intercept), size = 0.3
) +
common_scales() +
labs(
x = TeX("$\beta_i^{1,1}"), y = TeX("$\beta_i^{2,1}")
) +
theme_plt() -> gg2
现在,高度差异的原因是由于正确的情节同时具有子脚本和超级脚本。因此,我们可以通过以下方式确保所有位的高度相同(因为这些地块具有相同的共同地块区域元素):
gt1 <- ggplot_gtable(ggplot_build(gg1))
gt2 <- ggplot_gtable(ggplot_build(gg2))
gt1$heights <- gt2$heights
一起来看看:
cowplot::plot_grid(gt1, gt2, ncol = 2, align = "v")
你无法从 ^^ 看出,但由于你设置的 aspect.ratio
,图表上方和下方有一个水平的白色 margin/border。 RStudio 永远不会用白色以外的任何其他颜色显示它(mebbe,最终可能在 1.2 中 "dark" 模式下 "black" )。
其他绘图设备具有您可以指定的 bg
颜色。我们可以使用 magick
设备并适当地设置 height/width 以确保没有白色 borders/margin:
image_graph(900, 446, bg = "grey92")
cowplot::plot_grid(gt1, gt2, ncol = 2, align = "v")
dev.off()
^^ 仍然 看起来 就像它在 RStudio 中有一个 top/bottom 边框,如果图 pane/window 的大小不是根据纵横比而是根据实际plot "image" 不会有任何.
我认为提供的各种解决方案过于复杂。因为 cowplot::plot_grid()
returns 一个新的 ggplot2 对象,您可以使用 ggplot2 的主题机制简单地设置样式。
首先是问题代码的可重现示例,已简化
library(ggplot2)
library(latex2exp)
set.seed(123456)
Test_1 <- round(rnorm(20,mean=35,sd=3),0)/100
Test_2 <- round(rnorm(20,mean=70,sd=3),0)/100
ei.data <- as.data.frame(cbind(Test_1,Test_2))
intercept <- as.data.frame(matrix(0,20,1))
slope <- as.data.frame(matrix(0,20,1))
data <- cbind(intercept,slope)
colnames(data) <- c("intercept","slope")
for (i in 1:nrow(ei.data)){
data[i,1] <- (ei.data[i,2]/(1-ei.data[i,1]))
data[i,2] <- ((ei.data[i,1]/(1-ei.data[i,1]))*(-1))
}
theme_plt <- function() {
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "grey92", colour = NA),
plot.background = element_rect(fill = "grey92", colour = NA),
axis.line = element_line(colour = "black")
) +
theme(aspect.ratio = 1)
}
common_scales <- function() {
list(
scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2)),
scale_x_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2))
)
}
ggplot(ei.data, aes(Test_1, Test_2)) +
geom_point(shape = 1, size = 1) +
common_scales() +
labs(
x = TeX("$n_{1,i}$"), y = TeX("$t_{1,i}$")
) +
theme_plt() -> gg1
ggplot() +
geom_point() +
geom_abline(
data = data, aes(slope = slope, intercept = intercept), size = 0.3
) +
common_scales() +
labs(
x = TeX("$\beta_i^{1,1}"), y = TeX("$\beta_i^{2,1}")
) +
theme_plt() -> gg2
cowplot::plot_grid(gg1, gg2, align = "v")
正如我们所见,两个图形的尺寸略有不同,因此背景不匹配。
解决方法是在plot_grid()
调用后简单地添加一个主题语句:
cowplot::plot_grid(gg1, gg2, align = "v") +
theme(plot.background = element_rect(fill = "grey92", colour = NA))
这已经创建了所选颜色的统一背景。您当然必须调整绘图的输出尺寸以避免两个数字上方和下方的大量灰色。
为了更清楚地突出显示正在发生的事情,让我们用不同的颜色选择组合图的样式:
cowplot::plot_grid(gg1, gg2, align = "v") +
theme(plot.background = element_rect(fill = "cornsilk", colour = "blue"))
我们可以看到主题陈述应用于canvas,plot_grid()
将两个情节粘贴到canvas。
最后,我们可以问一开始为什么会出现这个问题,答案是因为绘图没有对齐。为了使它们完美对齐,我们需要垂直和水平对齐,当我们这样做时,事情会按预期工作:
cowplot::plot_grid(gg1, gg2, align = "vh")
通常情况下,align = "h"
就足够了(当绘图放在同一行时,align = "v"
是不正确的),但是因为主题有固定的纵横比,所以我们需要水平和对齐垂直,因此 align = "vh"
.