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

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

我有一个来自 model_list 的 10 plots/graphs 的列表,我在下面使用了以下代码。我将这些地块存储在列表 var_list.

library(mixOmics)

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

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

我想将此列表中的这些图保存为 jpeg(生成 10 个不同的 jpeg)。我使用了以下代码,R 创建了 10 张图像,但所有图像都是相同的(因此只创建了第一个图并为其余图复制)。

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

我见过类似的问题,但我找不到正确的解决方案来为列表中的每个数据帧的每个图设置一个 jpg!我该如何解决这个问题?非常感谢!

通过这个 link 你可以找到 dput(model_list[[1]]).

根据您在类似 post 中提供的数据,这里有可能解决您的问题。最好解决 model_list,因为当您转换为 var_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")

接下来是绘图循环:

#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()
}

这将在您的文件夹中生成图表,您可以在此处看到:

还有变化的情节(见标题):