使用 gridExtra (ggplot) 的奇怪行为
Curious behaviour using gridExtra (ggplot)
我正在尝试使用 gridExtra
包将三个地块堆叠在一起。我已经尝试了第一个使用 here 中的 grid.arrange
的例子,它工作得很好。
但是,当我尝试使用自己的图时,我得到了每个图的轴但没有数据,所有格式都被删除了。最小工作示例:
library(ggplot2)
library(gridExtra)
popu_H0 <- seq(10, 30, length=100)
popu_H0_norm <- dnorm(popu_H0, mean = 20, sd = 4)
popu_H0_df <- as.data.frame(cbind(popu_H0, popu_H0_norm))
plot_H0 <- ggplot(popu_H0_df, aes(x=popu_H0, y=popu_H0_norm))
plot_H0 +
geom_line() +
theme(
text = element_text(size=20),
axis.title.x = element_text(vjust=0.1),
axis.text.x = element_text(size = rel(1.8)),
legend.position = "none",
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.y = element_blank()
) +
xlab("New label") +
annotate("text", x = 20, y = 0.05, label = "Some annotation", size = 10)
grid.arrange(plot_H0, plot_H0, plot_H0, ncol = 1, nrow = 3)
ggplot
产生预期的输出,但 grid.arrange
产生 this.
您忘记替换绘图对象。
library(ggplot2)
library(gridExtra)
popu_H0 <- seq(10, 30, length=100)
popu_H0_norm <- dnorm(popu_H0, mean = 20, sd = 4)
popu_H0_df <- as.data.frame(cbind(popu_H0, popu_H0_norm))
plot_H0 <- ggplot(popu_H0_df, aes(x=popu_H0, y=popu_H0_norm))
plot_H0 <- plot_H0 + # Here you need `<-` to update the plot
geom_line() +
theme(
text = element_text(size=20),
axis.title.x = element_text(vjust=0.1),
axis.text.x = element_text(size = rel(1.8)),
legend.position = "none",
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.y = element_blank()
) +
xlab("New label") +
annotate("text", x = 20, y = 0.05, label = "Some annotation", size = 10)
grid.arrange(plot_H0, plot_H0, plot_H0, ncol = 1, nrow = 3)
我正在尝试使用 gridExtra
包将三个地块堆叠在一起。我已经尝试了第一个使用 here 中的 grid.arrange
的例子,它工作得很好。
但是,当我尝试使用自己的图时,我得到了每个图的轴但没有数据,所有格式都被删除了。最小工作示例:
library(ggplot2)
library(gridExtra)
popu_H0 <- seq(10, 30, length=100)
popu_H0_norm <- dnorm(popu_H0, mean = 20, sd = 4)
popu_H0_df <- as.data.frame(cbind(popu_H0, popu_H0_norm))
plot_H0 <- ggplot(popu_H0_df, aes(x=popu_H0, y=popu_H0_norm))
plot_H0 +
geom_line() +
theme(
text = element_text(size=20),
axis.title.x = element_text(vjust=0.1),
axis.text.x = element_text(size = rel(1.8)),
legend.position = "none",
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.y = element_blank()
) +
xlab("New label") +
annotate("text", x = 20, y = 0.05, label = "Some annotation", size = 10)
grid.arrange(plot_H0, plot_H0, plot_H0, ncol = 1, nrow = 3)
ggplot
产生预期的输出,但 grid.arrange
产生 this.
您忘记替换绘图对象。
library(ggplot2)
library(gridExtra)
popu_H0 <- seq(10, 30, length=100)
popu_H0_norm <- dnorm(popu_H0, mean = 20, sd = 4)
popu_H0_df <- as.data.frame(cbind(popu_H0, popu_H0_norm))
plot_H0 <- ggplot(popu_H0_df, aes(x=popu_H0, y=popu_H0_norm))
plot_H0 <- plot_H0 + # Here you need `<-` to update the plot
geom_line() +
theme(
text = element_text(size=20),
axis.title.x = element_text(vjust=0.1),
axis.text.x = element_text(size = rel(1.8)),
legend.position = "none",
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.y = element_blank()
) +
xlab("New label") +
annotate("text", x = 20, y = 0.05, label = "Some annotation", size = 10)
grid.arrange(plot_H0, plot_H0, plot_H0, ncol = 1, nrow = 3)