使用for循环或lapply从一个数据帧中的选择列创建多个ggplots

时间:2020-08-31 15:40:44

标签: r loops for-loop ggplot2 lapply

尝试创建循环或使用lapply从一个数据帧生成多个图时,我遇到各种麻烦。

df
   target  A.O2 A.H2O A.conc A.bias  B.O2 B.H2O B.conc B.bias  C.O2 C.H2O C.conc C.bias
1     85 20.90  0.06 254.96   0.01 20.90  0.06 255.02   0.03 20.90  0.06 254.98   0.01
2     50 20.90  0.09 150.09   0.09 20.90  0.09 150.06   0.08 20.90  0.09 150.00   0.03
3     25 20.94  0.09  75.24   0.31 20.94  0.09  75.47   0.62 20.94  0.09  74.98  -0.04
4     85 10.00  0.08 251.99  -1.22 10.00  0.08 252.02  -1.21 10.00  0.08 252.01  -1.21
5     50 10.00  0.09 148.51  -1.06 10.00  0.09 148.52  -1.05 10.00  0.09 148.50  -1.06
6     25 10.00  0.07  74.00  -1.27 10.00  0.07  74.03  -1.24 10.00  0.07  74.03  -1.24
7     85  0.10  0.06 246.99  -3.13  0.10  0.06 247.01  -3.13  0.10  0.06 247.00  -3.13
8     50  0.10  0.14 146.50  -2.39  0.10  0.14 146.50  -2.39  0.10  0.14 146.45  -2.42
9     25  0.10  0.10  72.97  -2.55  0.10  0.10  73.04  -2.45  0.10  0.10  73.04  -2.44

我想创建其中X = O2(A.O2,B.O2,C.O2)和Y =偏差(A.bias,B.bias,C.bias)的图,并根据目标列中的值。

library(ggrepel)

ggplot(df, aes(A.O2, A.bias)) +
  theme_bw() + 
  theme(legend.position = 'bottom', plot.title = element_text(hjust=0.5)) + 
  geom_point(aes(colour = factor(target))) +
  geom_line(aes(colour = factor(target))) +
  geom_text_repel(aes(label=paste(A.bias),
                      hjust= 0.4,
                      vjust=-.8, colour = factor(target)),
                  size = 3) +
  ggtitle('A') +
  labs(
    x = expression('O'[2]),
    y = "bias",
    colour = 'conc'
  )

enter image description here

我想重复相同的代码,唯一的变化是 aes() ggtitle() 中的X和Y值。我尝试查找类似的帖子以使用for循环或lapply来执行此操作,但似乎没有任何效果。

1 个答案:

答案 0 :(得分:1)

可能将数据重塑为长格式并使用facet_grid。当我们切换列名的后缀和前缀时,使用reshape很容易。

names(df) <- sapply(lapply(strsplit(names(df), "\\."), rev), paste, collapse=".")
dfl <- reshape(df, varying=2:13, direction="long")

library(ggplot2)
library(ggrepel)
ggplot(dfl, aes(O2, bias)) +
  theme_bw() + 
  theme(legend.position = 'bottom', plot.title = element_text(hjust=0.5)) + 
  geom_point(aes(colour = factor(target))) +
  geom_line(aes(colour = factor(target)))+
  geom_text_repel(aes(label=paste(bias),
                      hjust= 0.4,
                      vjust=-.8, colour = factor(target)),
                  size = 3) +
  facet_grid("time") +
  # ggtitle(z) +  ## not needed
  labs(
    x = expression('O'[2]),
    y = "bias",
    colour = 'conc'
  )

enter image description here

或者,如果您希望三个单图,可以将代码与ggsave一起放入一个函数中,以在lapply循环中使用。

FUN <- function(x) {
  ggplot(dfl[dfl$time == x, ], aes(O2, bias)) +
  theme_bw() + 
  theme(legend.position = 'bottom', plot.title = element_text(hjust=0.5)) + 
  geom_point(aes(colour = factor(target))) +
  geom_line(aes(colour = factor(target)))+
  geom_text_repel(aes(label=paste(bias),
                      hjust= 0.4,
                      vjust=-.8, colour = factor(target)),
                  size = 3) +
  # facet_grid("time") +  ## not needed
  ggtitle(x) +
  labs(
    x = expression('O'[2]),
    y = "bias",
    colour = 'conc'
  )
  ggsave(paste0("plot", x, ".png"))
}

times <- c("A", "B", "C")
lapply(times, FUN)

这会将三个图保存在您的工作目录中:

dir()
# [1] plotA.png
# [2] plotB.png
# [3] plotC.png

示例图:

enter image description here


数据:

df <- structure(list(target = c(85L, 50L, 25L, 85L, 50L, 25L, 85L, 
50L, 25L), A.O2 = c(20.9, 20.9, 20.94, 10, 10, 10, 0.1, 0.1, 
0.1), A.H2O = c(0.06, 0.09, 0.09, 0.08, 0.09, 0.07, 0.06, 0.14, 
0.1), A.conc = c(254.96, 150.09, 75.24, 251.99, 148.51, 74, 246.99, 
146.5, 72.97), A.bias = c(0.01, 0.09, 0.31, -1.22, -1.06, -1.27, 
-3.13, -2.39, -2.55), B.O2 = c(20.9, 20.9, 20.94, 10, 10, 10, 
0.1, 0.1, 0.1), B.H2O = c(0.06, 0.09, 0.09, 0.08, 0.09, 0.07, 
0.06, 0.14, 0.1), B.conc = c(255.02, 150.06, 75.47, 252.02, 148.52, 
74.03, 247.01, 146.5, 73.04), B.bias = c(0.03, 0.08, 0.62, -1.21, 
-1.05, -1.24, -3.13, -2.39, -2.45), C.O2 = c(20.9, 20.9, 20.94, 
10, 10, 10, 0.1, 0.1, 0.1), C.H2O = c(0.06, 0.09, 0.09, 0.08, 
0.09, 0.07, 0.06, 0.14, 0.1), C.conc = c(254.98, 150, 74.98, 
252.01, 148.5, 74.03, 247, 146.45, 73.04), C.bias = c(0.01, 0.03, 
-0.04, -1.21, -1.06, -1.24, -3.13, -2.42, -2.44)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9"))
相关问题