首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用gridExtra (ggplot)的奇怪行为

使用gridExtra (ggplot)的奇怪行为
EN

Stack Overflow用户
提问于 2016-11-30 00:58:48
回答 1查看 36关注 0票数 0

我正试图使用gridExtra包将三幅图叠加在一起。我已经尝试了第一个使用grid.arrange这里示例,它运行得非常好。

但是,当我尝试使用我自己的绘图时,我会得到每个绘图的轴,但是没有数据,所有的格式都被去掉了。最低工作实例:

代码语言:javascript
运行
复制
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生成

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-30 01:08:17

你忘了替换绘图对象了。

代码语言:javascript
运行
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40878503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档