Saving plots within lapply

后端 未结 1 1803
忘掉有多难
忘掉有多难 2020-12-20 14:48

I have a list of dataframes:

str(subsets.d)
List of 22
 $ 1       :\'data.frame\':   358 obs. of  118 variables:
  ..$ Ac_2017_1 : num [1:358] 0 0 0 0 0 0 0          


        
相关标签:
1条回答
  • 2020-12-20 15:17

    Your plots are stored in a list, hence you can use lapply on the output to save all of them. This has been answered here: Saving a list of plots by their names()

    # create data for this example (data above too involved)
    df <- data.frame(value = rnorm(100), dates = rep(1:50, 2), type = rep(c("a", "b")))
    list1 <- split(df, df$type)
    
    
    plots <- lapply(list1, function(x) ggplot(x, aes(dates, value)) + geom_boxplot())
    
    lapply(names(plots), 
           function(x) ggsave(filename=paste(x,".jpeg",sep=""), plot=plots[[x]]))
    
    0 讨论(0)
提交回复
热议问题