首页 > 解决方案 > 如何在r的for循环中组合多个散点图?

问题描述

我有一个关于在循环中生成多个 ggscter 图的问题。我有一个数据集:

dput(head(data_1))

structure(list(pop.id = c("pop1", "pop2", "pop3", "pop4", "pop5", 
"pop6"), pos_1 = c(0.75, 0.95, 0.75, 0.7, 1, 1), pos_2 = c(0, 
0, 0.05, 0, 0, 0), pos_3 = c(0L, 0L, 0L, 0L, 0L, 0L), pos_4 = c(0L, 
0L, 0L, 0L, 0L, 0L), pos_5 = c(0L, 0L, 0L, 0L, 0L, 0L), pos_6 = c(0.05, 
0.05, 0, 0, 0.15, 0.05), pos_7 = c(0L, 0L, 0L, 0L, 0L, 0L), Latitude_e = 
c(1.868536, 
1.627792, 1.489075, 1.816299, 1.642119, 1.433996), Longitude_e = c(-0.5019046, 
-0.6238463, -0.6386181, -0.5478555, -0.5133615, -0.4472022), 
Elevation_e = c(2.183258, 2.006655, 1.565147, 2.337786, 1.543071, 
1.454769), MAT_e = c(-1.919178, -1.648235, -1.377292, -1.919178, 
-1.648235, -1.241821), MWMT_e = c(-0.24204516, -0.24204516, 
-0.24204516, -0.24204516, -0.24204516, 0.06051129), MCMT_e = c(-2.031283, 
-1.691319, -1.436346, -2.031283, -1.691319, -1.351355), TD_e = c(2.085645, 
1.723449, 1.542351, 2.085645, 1.723449, 1.451802), MAP_e = c(0.9177859, 
0.6161032, 0.5155423, 0.8775616, 0.6864958, 0.5255984), MSP_e = c(-0.393691, 
-0.418043, -0.2962829, -0.393691, -0.3449869, -0.2719309), 
AHM_e = c(-1.008593, -0.774882, -0.6813975, -0.9930123, -0.8216242, 
-0.6658168), SHM_e = c(0.2504209, 0.2654162, 0.1754446, 0.2504209, 
0.2054351, 0.1604493), DD_0_e = c(2.235186, 1.82631, 1.417435, 
2.235186, 1.82631, 1.417435), DD5_e = c(-1.926029, -1.606345, 
-1.360735, -1.965015, -1.602447, -1.239879), DD_18_e = c(2.10857, 
1.718094, 1.457777, 2.146852, 1.718094, 1.335274)), class = "data.frame", 
row.names = c(NA, -6L))

我正在尝试为所有环境变量(latitude_e,...)中的每个位置(pos_1...pop_7)生成一些散点图。我想将每个位置的这些图保存在单独的文件中。这意味着我应该得到 7 个文件(因为总共有 7 个位置),每个文件包含 11 个图(因为它们是 11 个环境变量)。这是我正在使用的代码,但它没有给我想要的输出。如果有人能帮我解决这个问题,我将不胜感激!

这是我目前使用的代码:

test_type_env_inv<-array ("0",ncol (data_1))
env_type<-grep ("e",colnames(data_1))


inv_type<-grep ("pos",colnames(data_1))


the_e<-colnames(data_1[env_type])
the_i<-colnames(data_1[inv_type])



#pdf(file="annuus_part5.pdf", height= 15, width = 15)
plots <- list() 

for (i in 1:11){

     for (j in 1:7){
         y<-max(data_1[,the_i[j]])+0.3
         x<-min(data_1[,the_i[i]])+ (-1)
         sp<-print(ggscatter(data_1, x = the_e[i], y = the_i[j],
                             add = "reg.line",  # Add regressin line
                             add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line
                             conf.int = TRUE)




                   + stat_cor(method = "pearson", label.x = x, label.y = y))

     }

                   plots[[i]] <- sp

 }

multiplot(plotlist = plots, cols = 4)

这是图表,我的代码仅针对 7 个环境因素的第一个位置绘制。

在此处输入图像描述

标签: rloopsggplot2

解决方案


您可以使用该cowplot库通过使用绘图列表将每个位置的绘图(即pos_*变量)与所有环境变量(即 *_e变量)合并。在每个位置有一个图后,可以使用for-loop轻松导出每个新图:

the_i = grep( "pos_", colnames(data_1), value = T)
the_e = grep( "_e", colnames(data_1), value = T)

for( is_ in the_i  ){

  ## list of plots
  tmp_gglist = lapply( the_e, function(  es_ ){

    y <- max(data_1[, is_ ]) 
    x <- min(data_1[, es_ ])

    ggscatter(data_1, x = es_, y = is_,
              add = "reg.line",  
              add.params = list(color = "blue", fill = "lightgray"), 
              conf.int = TRUE) +
      stat_cor(method = "pearson",label.x = x, label.y = y*1.1)
  })

  ## exporting merged list of plots
  png( paste("Out_" , is_ , ".png", sep = ""), 
       width = 9,
       height = 8, 
       units = 'in',
       res = 100)
  q <- cowplot::plot_grid(plotlist = tmp_gglist )
  print(q)
  dev.off()
}

然后,所有绘图都将在您的工作目录中可用:

grep(".png", dir() , value = T)

# [1] "Out_pos_1.png" "Out_pos_2.png" "Out_pos_3.png" "Out_pos_4.png"
# [5] "Out_pos_5.png" "Out_pos_6.png" "Out_pos_7.png"

推荐阅读