首页 > 解决方案 > 如何从列表中在 R 中制作具有背景(splsda 模型)的多个个体观察图?

问题描述

我在 R 中对 10 个数据帧(10 个研究区域的数据)执行 splsda 模型,存储为列表(datalist)。所有这些数据框都是相似的,具有相同的变量,但只是不同的值。

> head(datalist[[1]])
  OID POINTID WETLAND TPI200 TPI350 TPI500 TPI700 TPI900 TPI1000 TPI2000 TPI3000 TPI4000 TPI5000 TPI2500
1  -1       1 no wetl     70     67     55     50     48      46      53      47      49      63      48
2  -1       2 no wetl     37     42     35     29     32      16      17      35      49      63      26
3  -1       3 no wetl     45     55     45     39     41      41      53      47      49      63      48
4  -1       4 no wetl     46     58     51     43     46      36      54      47      49      62      49
5  -1       5 no wetl     58     55     53     49     47      46      54      47      49      62      49
6  -1       6 no wetl     56     53     51     49     46      46      54      47      49      61      49

dput(datalist[[1]])可以在这里找到(wetransfer 中的 txt 文件)。

我已经splsda.model使用两个组件执行了lapply,结果是model_list

library(mixOmics)

custom_splsda <- function(datalist, ncomp, keepX, ..., Xcols, Ycol){
  Y <- datalist[[Ycol]]
  X <- datalist[Xcols]
  res <- splsda(X, Y, ncomp = ncomp, keepX = keepX, ...)
  res
}

model_list <- lapply(datalist, custom_splsda, ncomp = 2, keepX = c(5, 5), Xcols = 4:8, Ycol = "WETLAND")

我想针对这两个组件和背景可视化单个观察结果,以便为每个数据框(研究区域)绘制图表,例如: 示例背景图

对于列表的一个元素(学习区),可以通过以下代码生成并保存为pdf:

background <- background.predict(model_list, comp.predicted = 2, dist = "max.dist")
pdf("backgroundPlot_studyarea1.pdf")
plotIndiv(model_list, comp = 1:2, group = datastudyarea1$WETLAND,
          ind.names = FALSE, title = "Maximum distance",
          legend = TRUE,  background = background,cex = 0.5)
dev.off()

我想为所有 10 个学习区域提供这个datalist,因此将 10 个背景图作为 pdf(backgroundPlot_studyarea1.pdf、backgroundPlot_studyarea2.pdf 等)(或 jpeg,也可以)。我怎样才能做到这一点lapply?还是我必须使用for loop?我已经尝试了很多东西,但这些东西似乎都不起作用......

标签: rlistplotlapply

解决方案


使用您提供的示例数据,这里有一个解决方案,可以将每个图保存在一个 pdf 中。为了使练习具有可重复性,我datalist使用您添加的输入进行了创建。在您的实际问题中,您必须datalist使用名称才能将标题添加到绘图中。此外,一些绘图数据不可用,datastudyarea1但为您完成这部分代码并不难。这是草图:

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 created
model_list <- lapply(datalist, custom_splsda,
                     ncomp = 2, keepX = c(5, 5),
                     Xcols = 4:8, Ycol = "WETLAND")

#Iterate to save
#Create pdf
pdf('Summaryplots.pdf',width = 14)
for(i in 1:length(model_list))
{
  #Create background
  background <- background.predict(model_list[[i]], 
                                   comp.predicted = 2, 
                                   dist = "max.dist")
  #Plot
  plotIndiv(model_list[[i]], comp = 1:2,
            ind.names = FALSE, title = paste0("Maximum distance ",names(model_list)[i]),
            legend = TRUE,  background = background,cex = 0.5)
}
#Turn devices off
dev.off()

该代码将生成一个 pdf 文件,其中保存了所有绘图。只是一些细节:paste0("Maximum distance ",names(model_list)[i])允许使用model_list. 同样在您的原始代码中,您拥有group = datastudyarea1$WETLAND. 我删除了,因为我没有关于它的数据,但您可以再次添加以补充您想要的输出。最后,我展示了保存在 pdf 中的结果。这是一个带有两张幻灯片的pdf:

在此处输入图像描述


推荐阅读