如何循环 df 子集和 ggplot?
How to loop df subsetting and ggplot?
我想在循环中包含子设置以使用 R 生成多个 ggplots 。我尝试采用其他帖子中建议的解决方案,但 none 奏效了。
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))
}
但是 returns
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)
但是 returns
Error in ggplot(var, aes(x = char, y = stat)) :
object 'n1' not found.
我尝试采用其他一些解决方案,但仍然无效。你有什么建议吗?
我根据此博客中的信息构建了一个解决方案: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].
我想在循环中包含子设置以使用 R 生成多个 ggplots 。我尝试采用其他帖子中建议的解决方案,但 none 奏效了。
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))
}
但是 returns
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)
但是 returns
Error in ggplot(var, aes(x = char, y = stat)) :
object 'n1' not found.
我尝试采用其他一些解决方案,但仍然无效。你有什么建议吗?
我根据此博客中的信息构建了一个解决方案: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].