【问题标题】:extracting name from nested data to use as plot label in purrr:map( ~ggplot call从嵌套数据中提取名称以用作 purrr:map(~ggplot 调用中的绘图标签
【发布时间】:2019-08-11 17:41:57
【问题描述】:

我正在尝试使用 tidyverse 进行一些探索性数据分析。我有一个庞大而复杂的数据集,但重要的部分归结为类似于以下内容:


my_df <- data.frame(Expt = rep(c("Expt1", "Expt2", "Expt3", "Expt4"), each = 96),
                  ExpType = rep(c("A", "B"), each = 192),
                  Treatment = c(rep("T1", 192), rep("T2", 144), rep("T1", 48)),
                  Subject = c(rep(c("S01", "S02", "S03", "S04", "S05", "S06", "S07", "S08"), 24), rep("S01", 96), rep("S06", 96)),
                  xvar = as.factor(rep(rep(c(10, 5, 2.5, 1.25, 0.6, 0.3, 0.16, 0.08, 0.04, 0.02, 0, "NA"), each = 8),  4)),
                  yvar = runif(384))

(Expt 是每个单独实验的唯一但无信息标识符。每个 Expt 始终只有一个 ExpType,但可能包括一个或多个级别的处理和主题。)

我按 ExpType、Treatment、Subject 和 Expt 对数据进行分组,然后制作图表。因此,我正在制作大量图表,如果这些图表有信息丰富的标题,那会让我的生活更加更轻松。

我可以对数据进行分组并制作所有内容的图表,如下所示:

my_df2 <- my_df %>%
group_by(ExpType, Treatment, Expt) %>% 
nest() %>%

mutate(plots1 = map(
  .x = data,
  ~ggplot(data = .x, aes(x=as.factor(xvar), y = yvar)) + # 
    theme_classic() + theme(legend.key.width = unit(2, "lines"), legend.justification = c(1, 1), legend.position = c(1, 1)) +
    geom_smooth(method = "loess", se = FALSE, aes(group=Subject, color=Subject, linetype = Subject))+ 
    geom_point(aes(fill = Subject), size = 2.5) 
))


walk(.x = my_df2$plots1,  ~print(.x))  

我不知道该怎么做是为每个情节添加一个标题以告诉我它是什么。我尝试制作一个包含所有相关信息的唯一标识符:

my_df3 <- my_df %>%
  mutate(FullID = paste0(my_df$ExpType, "_", my_df$Treatment, "_", my_df$Expt)) %>%   
  group_by(ExpType, Treatment, Expt) %>% 
  nest() %>%
  arrange(ExpType, Treatment) 

我可以再次取出 FullID:

#  Either of these will successfully extract a list of FullIDs
map(my_df3$data, "FullID")

my_df3$data %>% 
  map("FullID")  

我不知道该怎么做是在地图中降低额外的嵌套级别(~ggplot 调用以使用 FullID 作为绘图标题,使用类似的东西:

my_df3 <- my_df3 %>%  
  mutate(plots2 = map2(
    .x = data, 
    .y = map_chr(data$FullID),
    ~ggplot(.x, aes(x=xvar, y = yvar)) + # 
      theme_classic() + theme(legend.key.width = unit(2, "lines"), legend.justification = c(1, 1), legend.position = c(1, 1)) +

      geom_smooth(method = "loess", se = FALSE, aes(group=Subject, color=Subject, linetype = Subject))+ 
      geom_point(aes(fill=Subject, shape = Subject), size = 2.5) + 
      labs(title = unique(.y))
  ))

我知道一定有办法做到这一点,但我只是不明白语法。有什么建议吗?

【问题讨论】:

  • 问题是map_chr(data$FullID)
  • 我在您的数据中找不到“应变”列

标签: r ggplot2 purrr


【解决方案1】:

FullID 也可以使用unite 创建(请注意,dplyr 函数中不需要.$)。在nest/arrange 之后,在OP 的代码中,map2 与作为map_chr(data$FullID) 的输入参数之一一起使用。要使map 正常工作,它需要应用一个不存在的函数 (.f)。此外,当我们从list 列“数据”中的一列中提取信息时。我们不需要map2,但单个map 以后可以提取labs 中的列信息

my_df2 <- my_df %>% 
             unite(FullID, ExpType, Treatment,  Expt, sep="_", remove = FALSE) %>% 
             group_by(ExpType, Treatment, Expt) %>%
             nest %>% 
             arrange(ExpType, Treatment) %>%
             mutate(plots = map(data, ~ 
                  ggplot(.x, aes(x=xvar, y = yvar))   + 
                     theme_classic() + 
                     theme(legend.key.width = unit(2, "lines"), 
                       legend.justification = c(1, 1), legend.position = c(1, 1)) + 
                     geom_smooth(method = "loess", se = FALSE, 
                        aes(group=Subject, color=Subject, linetype = Subject))+ 
                     geom_point(aes(fill=Subject, shape = Subject), size = 2.5) +
                     labs(title =  first(.x$FullID))))

-检查

my_df2$plots[[1]]

【讨论】:

    猜你喜欢
    • 2020-01-02
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 2019-12-22
    • 2021-11-21
    相关资源
    最近更新 更多