【问题标题】:Creating multiple ggplots from select columns in one data frame using for loop or lapply使用 for 循环或 lapply 从一个数据框中的选定列创建多个 ggplots
【发布时间】:2020-12-19 18:09:04
【问题描述】:

我在尝试创建循环或使用 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'
  )

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

【问题讨论】:

    标签: r loops for-loop ggplot2 lapply


    【解决方案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'
      )
    

    或者,如果您想要三个单图,您可以将代码与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
    

    示例图:


    数据:

    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"))
    

    【讨论】:

    • 非常感谢,太好了!我有一个问题。对于带有单独图的第二个选项,我希望能够调出每个单独的图,以便我可以将其与 markdown 一起使用或将其导出到 excel。我该怎么做呢?
    • @lawrenceof'R'abia 欢迎您!试试FUN("A")
    • 完美。再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2020-09-29
    • 1970-01-01
    • 2014-11-19
    相关资源
    最近更新 更多