使用 cowplot 时减少图之间的边距
Decrease margins between plots when using cowplot
我想使用 cowplot 将一些图形组合在一起。但我无法更改边距大小。我只想使用一个 y 轴,但余量仍然很大,我想减少它。我使用了 ggplot 中的 plot.margin 代码,虽然当我查看单个图时它有效,但当图组合时它似乎不起作用。
我做了一些示例代码:
library(ggplot2)
library(cowplot)
x <- c("a", "b")
y1 <- c(3,6)
y2 <- c(10,15)
data1 <- data.frame(x,y1)
data2 <- data.frame(x, y2)
ylab1 <- ylab("Very nice y values")
xlab1 <- xlab("Very nice factors")
plot1 <- ggplot(data1, aes(x=x, y = y1)) +
geom_bar(stat ="identity", position=position_dodge(), fill = "grey")+
theme(plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm")) + xlab1 + ylab1
plot1
ylab2 <- ylab("")
xlab2 <- xlab("Very nice factors")
plot2 <- ggplot(data2, aes(x=x, y = y2)) +
geom_bar(stat = "identity",position=position_dodge(), fill = "grey")+
theme(plot.margin = unit(c(0.5,0.5,0.5,-0.5), "cm")) + xlab2 + ylab2
plot2
plot3 <- plot_grid(plot1, plot2, labels = c("A", "B"), align = "hv",nrow = 1, ncol = 2)
plot3 # Quite large margin between the two plots
我知道我可以通过使用分面来避免这个问题,但是我的真实情节比这张图要复杂得多。
您的 plot.margin
实际上对您不利。将它们设置为零以填充白色 space.
plot1 <- ggplot(data1, aes(x=x, y = y1)) +
geom_bar(stat ="identity", position=position_dodge(), fill = "grey")+
theme(plot.margin = unit(c(0,0,0,0), "cm")) + xlab1 + ylab1
plot1
ylab2 <- ylab("")
xlab2 <- xlab("Very nice factors")
plot2 <- ggplot(data2, aes(x=x, y = y2)) +
geom_bar(stat = "identity",position=position_dodge(), fill = "grey")+
theme(plot.margin = unit(c(0,0,0,0), "cm")) + xlab2 + ylab2
plot2
plot3 <- plot_grid(plot1, plot2, labels = c("A", "B"), align = "hv",nrow = 1, ncol = 2)
plot3
增加 plot_grid
中地块之间的 space 也得到解决 in this issue。
一个额外有趣的解决方案是建议的 in this comment - 尝试在两个图之间添加一个额外的空图并调整相对列宽:
plot4 <- plot_grid(plot1, NULL, plot2, rel_widths = c(1, 0, 1), align = "hv",
labels = c("A", "B"), nrow = 1)
plot4
甚至可以在 rel_widths
中尝试负值,这会给出更好的结果:
plot5 <- plot_grid(plot1, NULL, plot2, rel_widths = c(1, -0.1, 1), align = "hv",
labels = c("A", "B"), nrow = 1)
plot5
因此,尝试结合调整 plot.margin
(如@J.Con 所回答)并通过调整 rel_widths
.
添加额外的空图
编辑 2019-12-11
另请参阅 cowplot
的作者(Claus Wilke)的 this comment:
For those kinds of problems I would now recommend the patchwork
library. It's inherently difficult with plot_grid()
, due to its underlying design
因此,基于他们的插图 Adding Annotation and Style 的 patchwork
的快速示例如下所示:
library(patchwork)
plot3 <- plot1 + plot2 +
plot_annotation(tag_levels = 'A') &
theme(plot.tag = element_text(size = 8))
plot3
由 reprex package (v0.3.0)
于 2019-12-11 创建
我想使用 cowplot 将一些图形组合在一起。但我无法更改边距大小。我只想使用一个 y 轴,但余量仍然很大,我想减少它。我使用了 ggplot 中的 plot.margin 代码,虽然当我查看单个图时它有效,但当图组合时它似乎不起作用。
我做了一些示例代码:
library(ggplot2)
library(cowplot)
x <- c("a", "b")
y1 <- c(3,6)
y2 <- c(10,15)
data1 <- data.frame(x,y1)
data2 <- data.frame(x, y2)
ylab1 <- ylab("Very nice y values")
xlab1 <- xlab("Very nice factors")
plot1 <- ggplot(data1, aes(x=x, y = y1)) +
geom_bar(stat ="identity", position=position_dodge(), fill = "grey")+
theme(plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm")) + xlab1 + ylab1
plot1
ylab2 <- ylab("")
xlab2 <- xlab("Very nice factors")
plot2 <- ggplot(data2, aes(x=x, y = y2)) +
geom_bar(stat = "identity",position=position_dodge(), fill = "grey")+
theme(plot.margin = unit(c(0.5,0.5,0.5,-0.5), "cm")) + xlab2 + ylab2
plot2
plot3 <- plot_grid(plot1, plot2, labels = c("A", "B"), align = "hv",nrow = 1, ncol = 2)
plot3 # Quite large margin between the two plots
我知道我可以通过使用分面来避免这个问题,但是我的真实情节比这张图要复杂得多。
您的 plot.margin
实际上对您不利。将它们设置为零以填充白色 space.
plot1 <- ggplot(data1, aes(x=x, y = y1)) +
geom_bar(stat ="identity", position=position_dodge(), fill = "grey")+
theme(plot.margin = unit(c(0,0,0,0), "cm")) + xlab1 + ylab1
plot1
ylab2 <- ylab("")
xlab2 <- xlab("Very nice factors")
plot2 <- ggplot(data2, aes(x=x, y = y2)) +
geom_bar(stat = "identity",position=position_dodge(), fill = "grey")+
theme(plot.margin = unit(c(0,0,0,0), "cm")) + xlab2 + ylab2
plot2
plot3 <- plot_grid(plot1, plot2, labels = c("A", "B"), align = "hv",nrow = 1, ncol = 2)
plot3
增加 plot_grid
中地块之间的 space 也得到解决 in this issue。
一个额外有趣的解决方案是建议的 in this comment - 尝试在两个图之间添加一个额外的空图并调整相对列宽:
plot4 <- plot_grid(plot1, NULL, plot2, rel_widths = c(1, 0, 1), align = "hv",
labels = c("A", "B"), nrow = 1)
plot4
甚至可以在 rel_widths
中尝试负值,这会给出更好的结果:
plot5 <- plot_grid(plot1, NULL, plot2, rel_widths = c(1, -0.1, 1), align = "hv",
labels = c("A", "B"), nrow = 1)
plot5
因此,尝试结合调整 plot.margin
(如@J.Con 所回答)并通过调整 rel_widths
.
编辑 2019-12-11
另请参阅 cowplot
的作者(Claus Wilke)的 this comment:
For those kinds of problems I would now recommend the
patchwork
library. It's inherently difficult withplot_grid()
, due to its underlying design
因此,基于他们的插图 Adding Annotation and Style 的 patchwork
的快速示例如下所示:
library(patchwork)
plot3 <- plot1 + plot2 +
plot_annotation(tag_levels = 'A') &
theme(plot.tag = element_text(size = 8))
plot3
由 reprex package (v0.3.0)
于 2019-12-11 创建