简体   繁体   English

如何在 R 中使用 lapply 将列表中的绘图保存为 jpeg?

[英]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.我有一个来自model_list的 10 个图/图的列表,我在下面使用了以下代码。 I stored these plots in the list var_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包含 10 个图,例如在列表的第一个元素下方:

> 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).我想将此列表中的这些图保存为 jpeg(产生 10 个不同的 jpeg)。 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).我使用了以下代码,R 创建了 10 张图像,但所有图像都是相同的(因此只有第一个 plot 被创建并为其余图像复制)。

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?我见过类似的问题,但我找不到正确的解决方案,让列表中的每个 plot 都有一个 jpg 给每个 dataframe? How can I solve this?我该如何解决这个问题? Many thanks in advance!提前谢谢了!

Via this link you can find the dput(model_list[[1]]) .通过此链接,您可以找到dput(model_list[[1]])

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.如果您解决model_list会更好,因为当您转换为var_list时,所有数据都会变成图形元素。 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:下一个代码包含使用datalistmodel_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:正如您在此处看到的那样,这将在您的文件夹中生成图:

在此处输入图像描述

And also the plots that change (see titles):还有变化的情节(见标题):

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM