使用ggarrange在列表上迭代
我有以下代码,不明白为什么for循环不起作用。我是新手,如果这是显而易见的,请原谅,但它实际上并没有生成一组组合的图(正如下面更为暴力的方法所做的),它只是单独打印出每个图使用ggarrange在列表上迭代,r,loops,ggplot2,ggpubr,R,Loops,Ggplot2,Ggpubr,我有以下代码,不明白为什么for循环不起作用。我是新手,如果这是显而易见的,请原谅,但它实际上并没有生成一组组合的图(正如下面更为暴力的方法所做的),它只是单独打印出每个图 library(ggpubr) graphs <- lapply(names(hemi_split), function(i){ ggplot(data=hemi_split[[i]], aes(x=type, y=shoot.mass))+ geom_point()+ facet_wrap(.~
library(ggpubr)
graphs <- lapply(names(hemi_split), function(i){
ggplot(data=hemi_split[[i]], aes(x=type, y=shoot.mass))+
geom_point()+
facet_wrap(.~host, scales="free")+
theme_minimal()+
labs(title=i)
});graphs
for (i in 1:length(graphs)) {
ggarrange(graphs[[i]])
} ##not working
## this works, and is the desired output
ggarrange(graphs[[1]], graphs[[2]], graphs[[3]],
graphs[[4]], graphs[[5]], graphs[[6]],
graphs[[7]], graphs[[8]], graphs[[9]],
graphs[[10]], graphs[[11]])
库(ggpubr)
图形您可以使用do.call
提供图形的所有列表元素作为ggarrange
的参数:
library(ggpubr)
graphs <- lapply(names(mtcars)[2:5],function(x){
ggplot(mtcars,aes_string(x = x, y = "mpg")) +
geom_point()})
do.call(ggarrange,graphs)
库(ggpubr)
图使用purrr的另一种解决方案
library(tidyverse)
ggraphs <- map(names(mtcars)[2:5],
~ ggplot(mtcars,aes_string(x = .x, y = "mpg")) +
geom_point())
ggarrange(plotlist = ggraphs)
库(tidyverse)
组图