Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ggarrange在列表上迭代_R_Loops_Ggplot2_Ggpubr - Fatal编程技术网

使用ggarrange在列表上迭代

使用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(.~

我有以下代码,不明白为什么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(.~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)
组图