0

我一直在使用下面的代码创建 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")

以下是单个绘图的示例: 在此处输入图像描述

这是组合图的样子:

在此处输入图像描述

4

3 回答 3

1

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

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

于 2019-05-01T19:17:26.687 回答
0

你可能想看看拼凑包。

或者,如果您的所有绘图在图形元素方面具有相同数量的行,您可以将绘图转换为 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 包。

于 2019-05-01T19:33:06.907 回答
0

您也可以使用包grid.arrange()中的功能gridExtra。这是 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)

于 2019-05-01T19:25:53.570 回答