首页 > 解决方案 > 自动将组合散点图和直方图写入文件夹

问题描述

我有一个大型数据框,其中包含需要在导出到文件夹的图像中绘制的原始变量和转换变量。该图像包含数据的散点图和两个单独的直方图。我无法创建循环或找到可以自动执行此过程的函数。

下面是我正在使用的练习数据集以及我用来创建图形的手动过程。

df <- data.frame("ID" = 1:16)
df$Var_A <- c(1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31)
df$Var_B <- c(10,0,0,0,12,12,12,12,0,14,NA_real_,14,16,16,16,16)
df$Var_C <- c(10,12,14,16,10,12,14,16,10,12,14,16,10,12,14,16)
df$Var_D <- c(10,12,14,16,10,12,14,16,10,12,14,16,10,12,14,16)
df$New_A <- c(2,5,5,8,11,14,15,17,20,21,22,23,25,25,27,30)
df$New_B <- c(10,0,0,0,12,12,12,12,0,14,NA_real_,14,16,16,16,16)
df$New_C <- c(10,12,14,16,10,12,14,16,10,12,14,16,10,12,14,16)
df$New_D <- c(10,12,14,16,10,12,14,16,10,12,14,16,10,12,14,16)
df


   ID Var_A Var_B Var_C Var_D New_A New_B New_C New_D
1   1     1    10    10    10     2    10    10    10
2   2     3     0    12    12     5     0    12    12
3   3     5     0    14    14     5     0    14    14
4   4     7     0    16    16     8     0    16    16
5   5     9    12    10    10    11    12    10    10
6   6    11    12    12    12    14    12    12    12
7   7    13    12    14    14    15    12    14    14
8   8    15    12    16    16    17    12    16    16
9   9    17     0    10    10    20     0    10    10
10 10    19    14    12    12    21    14    12    12
11 11    21    NA    14    14    22    NA    14    14
12 12    23    14    16    16    23    14    16    16
13 13    25    16    10    10    25    16    10    10
14 14    27    16    12    12    25    16    12    12
15 15    29    16    14    14    27    16    14    14
16 16    31    16    16    16    30    16    16    16][1]][1]

jpeg(filename = "C:\\Users\\Documents\\R\\Images\\Output_A.jpg", quality = 100, width = 1000, height = 1000)
close.screen(all = TRUE) 
my_screen_step1 <- split.screen(c(2, 1))
screen(my_screen_step1[1])
plot(df$Var_A, df$New_A, pch=20 , xlab="Var_A", ylab="New_A", cex=3 , col=rgb(0.4,0.9,0.8,0.5))
my_screen_step2 <- split.screen(c(1, 2), screen = my_screen_step1[2])
screen(my_screen_step2[1])
hist(df$Var_A, border=F , col=rgb(0.2,0.2,0.8,0.7) , main="" , xlab="Var_A", breaks = 30)
screen(my_screen_step2[2])
hist(df$New_A, border=F , col=rgb(0.8,0.2,0.8,0.7) , main="" ,  xlab="New_A", breaks = 30)
dev.off()

我已经能够使用代码来提取自动化过程所需的列名(见下文),但我不确定如何使用这些列表来创建循环或利用可以自动化此过程的函数。

test_var_names  <- colnames(df %>% select(grep("Var_", colnames(.), value = TRUE)))
test_var_names
new_var_names <- colnames(df %>% select(grep("New_", colnames(.), value = TRUE)))
new_var_names

预期产出

标签: r

解决方案


将代码放入函数中,用变量替换常量。

save_plots <- function(data, x, y) {
  file_name <- sub('.*_', '', x)
  jpeg(filename = sprintf('C:\\Users\\Documents\\R\\Images\\Output_%s.jpg', file_name), quality = 100, width = 1000, height = 1000)
  close.screen(all = TRUE) 
  my_screen_step1 <- split.screen(c(2, 1))
  screen(my_screen_step1[1])
  plot(data[[x]], data[[y]], pch=20 , xlab=x, ylab=y, cex=3 , col=rgb(0.4,0.9,0.8,0.5))
  my_screen_step2 <- split.screen(c(1, 2), screen = my_screen_step1[2])
  screen(my_screen_step2[1])
  hist(data[[x]], border=F , col=rgb(0.2,0.2,0.8,0.7) , main="" , xlab=x, breaks = 30)
  screen(my_screen_step2[2])
  hist(data[[y]], border=F , col=rgb(0.8,0.2,0.8,0.7) , main="" ,  xlab=y, breaks = 30)
  dev.off()
}

使用以下方法为每对列名调用函数mapply

mapply(save_plots, test_var_names, new_var_names, MoreArgs = list(data = df))  

推荐阅读