【问题标题】:How to loop df subsetting and ggplot?如何循环df子集和ggplot?
【发布时间】:2021-06-10 09:00:41
【问题描述】:

我想在循环中包含子设置以生成多个 ggplots使用 R。我尝试调整其他帖子中建议的解决方案,但没有奏效。

name  char  stat  z
n1    c1    2.1   1
n1    c2    1.9   2
n1    c3    2.0   4
n1    c4    3.4   4
n2    c1    1.1   2
n2    c2    1.2   1
n2    c3    2.0   3
n2    c4    1.8   4
n3    c1    5.1   2
n3    c2    3.3   3
n3    c3    4.7   1
n3    c4    0.5   2

最多 n12(可能更多)。

目前我根据我需要的名称手动对数据框进行子集化并生成其绘图:

n1 <- df[df$name=="n1",]
p1 <- ggplot(n1, aes(x=char, y=stat)) +
  geom_col(fill = palette[n1$z])
p1

有没有办法创建一个循环,自动将 df 子集化为 n1/n2/n3 并创建 p1/p2/p3,以便我可以单独导出它们或将它们包装成单个图像?

我试过了:

for (i in df$name) {
  ggplot(df[df$name[i]], aes(x=char, y=stat))
}

但它会返回

Error: Can't use NA as column index with `[` at positions 1, 2, 3, 4, 5, and 16 more.

同样,我尝试创建一个函数,以便可以使用 lapply 循环它:

draft <- function(var) {
  ggplot(var, aes(x=char, y=stat))
}
draft(n1)

但它会返回

Error in ggplot(var, aes(x = char, y = stat)) : 
  object 'n1' not found.

我尝试调整其他一些解决方案,但 - 仍然 - 它不起作用。你有什么建议吗?

【问题讨论】:

    标签: r loops ggplot2 subset


    【解决方案1】:

    我根据此博客中的信息构建了一个解决方案:https://aosmith.rbind.io/2018/08/20/automating-exploratory-plots/

    您可以先创建一个应为其提供子集的函数。并使用 purrr::map 函数进行迭代。

    df <- tibble(name = c("n1", "n1","n1", "n1", "n2","n2","n2","n2","n3","n3","n3","n3"),
                char = c("c1", "c2", "c3", "c4","c1", "c2", "c3", "c4","c1", "c2", "c3", "c4"),
                stat = c(2.1, 1.9,2.0,3.4,1.1,1.2,2.0,1.8,5.1,3.3,4.7,0.5),
                z = c(1,2,4,4,2,1,3,4,2,3,1,2))
    
    
    plot_function <- function(sub_set) {
      n <- df %>% filter(name %in% c(sub_set))
      p <- n %>% ggplot(aes(x=char, y=stat, fill = z)) +
        geom_col()
    } 
    
    uniq_names <- df %>% distinct(name)
    all_groups <- uniq_names$name
    all_groups = set_names(all_groups)
    
    all_plots <- map(all_groups, ~plot_function(.x))
    
    all_plots$n1
    all_plots$n2
    all_plots$n3
    
    

    额外的好处是命名列表的好技巧,因此您可以将它们用作参考 - all_plots$n2 - 而不是 all_plots[2]。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-24
      • 2021-09-12
      • 2015-01-15
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 2021-02-12
      • 2021-02-05
      相关资源
      最近更新 更多