Search code examples
rlistplotjpeglapply

How to save plots in list as jpeg using lapply in R?


I have a list of 10 plots/graphs from model_list for which I used the following code below. I stored these plots in the list var_list.

library(mixOmics)

var_list<-lapply(model_list, function(x) plotVar(x))

var_list contains thus 10 plots, for example below the first element of the list:

> var_list[[1]]
                x          y Block  names pch cex     col font                  Overlap
TPI200 -0.6975577 -0.5582925     X TPI200   1   5 #388ECC    1 Correlation Circle Plots
TPI350 -0.8561514 -0.4101970     X TPI350   1   5 #388ECC    1 Correlation Circle Plots
TPI500 -0.9403552 -0.1074518     X TPI500   1   5 #388ECC    1 Correlation Circle Plots
TPI700 -0.9256605  0.3070954     X TPI700   1   5 #388ECC    1 Correlation Circle Plots
TPI900 -0.8697037  0.4699423     X TPI900   1   5 #388ECC    1 Correlation Circle Plots

I want to save these plots from this list as a jpeg (resulting in 10 different jpeg's). I used the following code and R creates 10 images, but all the images are the same (so only the first plot is created and duplicated for the rest).

lapply(1:length(model_list), function (x) {
  jpeg(paste0(names(model_list)[x], ".jpg"))
  lapply(model_list, function(x) plotVar(x))
  dev.off()
})

I have seen similar questions, but I can't find the right solution to have a jpg for each plot for each dataframe in the list! How can I solve this? Many thanks in advance!

Via this link you can find the dput(model_list[[1]]).


Solution

  • With data provided in a similar post by you, here a possible solution to your issue. It is better if you work around model_list because when you transform to var_list all data become graphical elements. Next code contains a replicate of model_list using datalist but in your real problem you must have it, also must include names for each of the components of the list:

    library(mixOmics)
    #Data
    datalist <- list(df1 = structure(list(OID = c(-1, -1, -1, -1, -1, -1), POINTID = c(1, 
    2, 3, 4, 5, 6), WETLAND = c("no wetl", "no wetl", "no wetl", 
    "wetl", "wetl", "wetl"), TPI200 = c(70, 37, 45, 46, 58, 56), 
        TPI350 = c(67, 42, 55, 58, 55, 53), TPI500 = c(55, 35, 45, 
        51, 53, 51), TPI700 = c(50, 29, 39, 43, 49, 49), TPI900 = c(48, 
        32, 41, 46, 47, 46), TPI1000 = c(46, 16, 41, 36, 46, 46), 
        TPI2000 = c(53, 17, 53, 54, 54, 54), TPI3000 = c(47, 35, 
        47, 47, 47, 47), TPI4000 = c(49, 49, 49, 49, 49, 49), TPI5000 = c(63, 
        63, 63, 62, 62, 61), TPI2500 = c(48, 26, 48, 49, 49, 49)), row.names = c(NA, 
    6L), class = "data.frame"), df2 = structure(list(OID = c(-1, 
    -1, -1, -1, -1, -1), POINTID = c(1, 2, 3, 4, 5, 6), WETLAND = c("no wetl", 
    "no wetl", "no wetl", "wetl", "wetl", "wetl"), TPI200 = c(70, 
    37, 45, 46, 58, 56), TPI350 = c(67, 42, 55, 58, 55, 53), TPI500 = c(55, 
    35, 45, 51, 53, 51), TPI700 = c(50, 29, 39, 43, 49, 49), TPI900 = c(48, 
    32, 41, 46, 47, 46), TPI1000 = c(46, 16, 41, 36, 46, 46), TPI2000 = c(53, 
    17, 53, 54, 54, 54), TPI3000 = c(47, 35, 47, 47, 47, 47), TPI4000 = c(49, 
    49, 49, 49, 49, 49), TPI5000 = c(63, 63, 63, 62, 62, 61), TPI2500 = c(48, 
    26, 48, 49, 49, 49)), row.names = c(NA, 6L), class = "data.frame")) 
    #Function
    custom_splsda <- function(datalist, ncomp, keepX, ..., Xcols, Ycol){
      Y <- datalist[[Ycol]]
      X <- datalist[Xcols]
      res <- splsda(X, Y, ncomp = ncomp, keepX = keepX, ...)
      res
    }
    #Create model_list, you must have the object
    model_list <- lapply(datalist, custom_splsda,
                         ncomp = 2, keepX = c(5, 5),
                         Xcols = 4:8, Ycol = "WETLAND")
    

    Next the loop for plots:

    #Loop
    for(i in 1:length(model_list))
    {
      jpeg(paste0(names(model_list)[i], ".jpg"))
      plotVar(model_list[[i]],title = names(model_list)[i])
      dev.off()
    }
    

    That will produce plots in your folder as you can see here:

    enter image description here

    And also the plots that change (see titles):

    enter image description here

    enter image description here