【问题标题】:Is there a different way to put 3 plots together into 1 image besides using ggpubr in R?除了在 R 中使用 ggpubr 之外,还有其他方法可以将 3 个图放在 1 个图像中吗?
【发布时间】:2019-05-01 18:56:55
【问题描述】:

我一直在使用下面的代码创建 3 个单独的图,然后使用 ggpubr 包中的 ggarrange 将它们组合成一个图并将其保存为图像。但是,一旦我创建了我的脚本的 Rmd,这个函数就非常喜怒无常,即使它在 Rmd 之外运行良好,也会给我一个错误“Faceting variables must have at least one value”。有没有使用 ggplot2 包中的东西获得相同结果的不同方法?还是其他更简单的方法? 编辑:我想维护图表中的网格,而不是使用复杂的方法来获得cowplot 所需的常见图例。

graph_1 <- ggplot(subset(data_p, Target == "1"), aes(x=as.integer(volume), y=percent_yield, color=Tech.Rep))+
  geom_point()+
  geom_smooth(se=FALSE)+
  facet_wrap(~sample)+
  ggtitle(paste("Graph 1"))+
  ylab("PERCENT YIELD")+
  scale_x_discrete(limits= levels(data_p$volume))+
  ylim(min=-150, max=150)+
  theme(axis.text.x=element_text(size=10,angle=85,hjust=1,vjust=1))+
  theme(legend.position="none")+
  geom_ribbon(aes(ymax=100, ymin=50), alpha = 0.2, fill="green", col="green")+
  geom_ribbon(aes(ymax=0, ymin=0), alpha = 0.2, fill="black", col="black", size=1.0)

graph_2 <- ggplot(subset(data_p, Target == "2"), aes(x=as.integer(volume), y=percent_yield, color=Tech.Rep))+
  geom_point()+
  geom_smooth(se=FALSE)+
  facet_wrap(~sample)+
  ggtitle(paste("Graph 2"))+
  ylab("PERCENT YIELD")+
  scale_x_discrete(limits= levels(data_p$volume))+
  ylim(min=-150, max=150)+
  theme(axis.text.x=element_text(size=10,angle=85,hjust=1,vjust=1))+
  theme(legend.position="none")+
  geom_ribbon(aes(ymax=100, ymin=50), alpha = 0.2, fill="green", col="green")+
  geom_ribbon(aes(ymax=0, ymin=0), alpha = 0.2, fill="black", col="black", size=1.0)


graph_3 <- ggplot(subset(data_p, Target == "3"), aes(x=as.integer(volume), y=percent_yield, color=Tech.Rep))+
  geom_point()+
  geom_smooth(se=FALSE)+
  facet_wrap(~sample)+
  ggtitle(paste("Graph 3"))+
  ylab("PERCENT YIELD")+
  scale_x_discrete(limits= levels(data_p$volume))+
  ylim(min=-150, max=150)+
  theme(axis.text.x=element_text(size=10,angle=85,hjust=1,vjust=1))+
  theme(legend.position="none")+
  geom_ribbon(aes(ymax=100, ymin=50), alpha = 0.2, fill="green", col="green")+
  geom_ribbon(aes(ymax=0, ymin=0), alpha = 0.2, fill="black", col="black", size=1.0)

ggarrange(graph_1, graph_2, graph_3, ncol=3, nrow=1, common.legend=TRUE, legend= "bottom")

以下是单个绘图的示例:

下面是组合图的样子:

【问题讨论】:

标签: r ggplot2 ggpubr


【解决方案1】:

您可以尝试使用 cowplot 包中的 plot_grid() 函数。你会发现一个包小插曲 https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html.

在你的情况下,试试, cowplot::plot_grid(graph_1, graph_2, graph_3, ncol = 3, align = "h")

【讨论】:

  • 这个包在我的图表中删除了网格,似乎有一个非常引人入胜的方式来获得一个共同的传说。我可能是错的,所以如果是,请纠正我。我正在尽量减少行数,以减少以后出现这种情况的可能性
【解决方案2】:

您也可以使用gridExtra 包中的grid.arrange() 函数。这是来自 R Blogger 的https://www.r-bloggers.com/extra-extra-get-your-gridextra/ 的示例。这也是包小插图https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html

试试grid.arrange(graph_1,graph_2,graph_3, ncol = 3)

【讨论】:

    【解决方案3】:

    您可能想看看patchwork 包。

    或者,如果您的所有绘图在图形元素方面具有相同的行数,您可以将绘图转换为 gtables 并将它们 cbind(或 rbind)在一起。

    library(grid)
    p <- p <- ggplot(iris, aes(Petal.Length, Sepal.Length)) + 
      geom_point() + 
      facet_wrap(~ Species, nrow = 3)
    
    grobs <- ggplotGrob(p)
    
    multi <- cbind(grobs, grobs, grobs, size = "first")
    
    grid.newpage(); grid.draw(multi)
    

    如果图例只出现在中间的情节中,可能会有点棘手,因为这算作一行图形元素。

    如果您想深入了解细节,可以使用 gtables,如果您想直观地组合绘图,我建议您使用 patchwork 包。

    【讨论】:

    • R 版本 3.5.2 似乎没有这个包
    • 对不起,我不知道。希望你能找到解决办法!
    猜你喜欢
    • 2019-12-07
    • 1970-01-01
    • 2017-11-30
    • 2012-05-23
    • 1970-01-01
    • 2018-03-01
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多