gpt4 book ai didi

r - 如何防止 grid.arrange 在 R 中的 4 个面板图上切断 x 轴标签?

转载 作者:行者123 更新时间:2023-12-04 07:18:03 25 4
gpt4 key购买 nike

我有 4 个数字,我想将它们组合成一个图。我可以通过制作 ggplot 中的数字来做到这一点。然后将它们转换为 grob 以在 grid.arrange 中使用.然而,grid.arrange似乎切断了左下角和右下角数字的 x 轴标签,在这种情况下应该是日期。关于如何保留 grid.arrange 的任何想法从切断这个 4 面板图的 x 轴标签?
最终产品应该是这样的,所有的轴都对齐并且具有相同的宽度和高度。
enter image description here
可重现的例子:

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

df1 <- data.frame(matrix(ncol = 2, nrow = 7))
x <- c("Date", "Value")
colnames(df1) <- x
df1$Date <- seq(as.Date("2021-08-01"), as.Date("2021-08-07"), by = "day")
set.seed(321)
df1$Value <- rnorm(7,20,2)

df2 <- data.frame(matrix(ncol = 2, nrow = 7))
colnames(df2) <- x
df2$Date <- seq(as.Date("2021-08-01"), as.Date("2021-08-07"), by = "day")
df2$Value <- rnorm(7,19,2)

df3 <- data.frame(matrix(ncol = 2, nrow = 7))
colnames(df3) <- x
df3$Date <- seq(as.Date("2021-08-01"), as.Date("2021-08-07"), by = "day")
df3$Value <- rnorm(7,7,1)

df4 <- data.frame(matrix(ncol = 2, nrow = 7))
colnames(df4) <- x
df4$Date <- seq(as.Date("2021-08-01"), as.Date("2021-08-07"), by = "day")
df4$Value <- rnorm(7,10,3)

p1 <- ggplot(data = df1, aes(Date,Value)) +
geom_line() +
theme_bw() +
theme(panel.grid = element_blank(),
text = element_text(size = 16),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(size = 16, color = "black"),
legend.position = "none")
p2 <- ggplot(data = df2, aes(Date,Value)) +
geom_line() +
theme_bw() +
theme(panel.grid = element_blank(),
text = element_text(size = 16),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(size = 16, color = "black"),
legend.position = "none")
p3 <- ggplot(data = df3, aes(Date,Value)) +
geom_line() +
theme_bw() +
theme(panel.grid = element_blank(),
text = element_text(size = 16),
axis.title.x = element_blank(),
axis.text.x = element_text(size = 16, color = "black", angle = 30, vjust = 1, hjust = 1),
axis.text.y = element_text(size = 16, color = "black"),
legend.position = "none")
p4 <- ggplot(data = df4, aes(Date,Value)) +
geom_line() +
theme_bw() +
theme(panel.grid = element_blank(),
text = element_text(size = 16),
axis.title.x = element_blank(),
axis.text.x = element_text(size = 16, color = "black", angle = 30, vjust = 1, hjust = 1),
axis.text.y = element_text(size = 16, color = "black"),
legend.position = "none")

g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
g3 <- ggplotGrob(p3)
g4 <- ggplotGrob(p4)

g2$widths <- g1$widths
g3$widths <- g1$widths
g4$widths <- g1$widths

g3$heights <- g1$heights
g4$heights <- g1$heights

grid.newpage()
grid.arrange(g1, g2, g3, g4, ncol = 2) #cuts off x-axes labels of g3 and g4

最佳答案

拼凑包的示例(假设您已经运行了问题中的代码)。如您所见,顶部的两个图并不比底部的宽。

library(patchwork)
p1 + p2 + p3 + p4 + plot_layout(ncol = 2)

创建于 2021-08-05 由 reprex package (v1.0.0)

关于r - 如何防止 grid.arrange 在 R 中的 4 个面板图上切断 x 轴标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68673638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com