【发布时间】: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) -
我在您的数据中找不到“应变”列