从嵌套数据中提取名称以用作purrr:map(~ggplot调用)中的打印标签

从嵌套数据中提取名称以用作purrr:map(~ggplot调用)中的打印标签,r,ggplot2,purrr,R,Ggplot2,Purrr,我正在尝试使用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", 19

我正在尝试使用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))
我不知道该如何做的是在地图中降低嵌套的额外级别(~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))
  ))
my_df3%
突变(plots2=map2(
.x=数据,
.y=映射chr(数据$FullID),
~ggplot(.x,aes(x=xvar,y=yvar))+#
theme_classic()+主题(legend.key.width=unit(2,“线”)、legend.justion=c(1,1)、legend.position=c(1,1))+
geom_smooth(方法=“黄土”,se=假,aes(组=主题,颜色=主题,线型=主题))+
几何点(aes(填充=主体,形状=主体,大小=2.5)+
实验室(标题=唯一(.y))
))

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

也可以使用
unite
创建
FullID
(注意,
dplyr
函数中不需要
$
)。在
nest/arrange
之后,在OP的代码中,
map2
与一个输入参数一起使用,如
map\u chr(data$FullID)
。要使
map
起作用,它需要一个函数(
.f
)要应用,它不存在。此外,由于我们正在从
列表
列“数据”中的一列提取信息。我们不需要
map2
,但一个
map
和更高版本可以在
实验室
中提取列信息

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

问题是
map\u chr(data$FullID)
我在您的数据中找不到“应变”列
#  Either of these will successfully extract a list of FullIDs
map(my_df3$data, "FullID")

my_df3$data %>% 
  map("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))
  ))
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]]