我已经使用下面的代码创建了3个独立的图,然后使用ggpubr包中的ggarrange将它们组合成一个图,并将其保存为图像。然而,一旦我创建了我的脚本的Rmd,这个函数就非常不稳定,并给出错误“刻面变量必须至少有一个值”,即使它在Rmd之外运行得很好。有没有不同的方法可以使用ggplot2包中的某些东西来获得相同的结果?或者是另一种更简单的方法?编辑:我想维护我的图表中的网格,而不是使用一种复杂的方式来获得一个共同的图例,这是牛图所需要的。
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")以下是单个绘图的示例:

下面是合并后的图:

发布于 2019-05-02 03:17:27
您可以尝试使用cowplot包中的plot_grid()函数。你会发现一个vignette 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-02 03:25:54
您还可以使用gridExtra包中的grid.arrange()函数。这里有一个来自R Blogger's https://www.r-bloggers.com/extra-extra-get-your-gridextra/的例子。这也是vignette https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html包。
试试grid.arrange(graph_1,graph_2,graph_3, ncol = 3)
发布于 2019-05-02 03:33:07
你可能想看看patchwork包。
或者,如果所有绘图在图形元素方面具有相同的行数,则可以将绘图转换为gtable并将它们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包。
https://stackoverflow.com/questions/55941227
复制相似问题