每组不能绘制超过一个箱线图。你忘了 aes(group = ...) 了吗?
Can't draw more than one boxplot per group. Did you forget aes(group = ...)?
我正在使用以下代码在 ggplot2 版本 3.3.0 中创建箱线图:
set.seed(240193)
df1 <- data.frame(person=rep(c("a","b"),each = 10),
score=c(rnorm(10,8,1),rnorm(10,6,1.5)))
df1 <- df1 %>%
group_by(person) %>%
mutate(d_ymin = min(score),
d_ymax = max(score),
d_lower = quantile(score, 0.25),
d_middle = median(score),
d_upper = quantile(score, 0.75))
p1 <- ggplot(df1) +
geom_boxplot(aes(x = person,
ymin = d_ymin,
lower = d_lower,
middle = d_middle,
upper = d_upper,
ymax = d_ymax,
fill = person), stat = "identity")
p2 <- ggplot(df1)+
geom_boxplot(aes(x=person,y=score), width=0.1)
由于错误 "Can't draw more than one boxplot per group. Did you forget aes(group = ...)?",无法创建 p1
"Run rlang::last_error()
to see where the error occurred"。 p2
没问题。
我注意到之前 post 将此问题描述为与 ggplot2 版本 3.2.0 的更新相关。据我所知,关键点是一种群体审美,它对每个应该绘制的箱线图都有独特的价值。所以技巧是 aes(group = interaction(x, group)
正如马吕斯回答的那样。但是,post 中的示例有两个变量,因此可以使用 interaction
函数。在我的例子中,我只有一个分组变量,person
。这意味着数据结构自然地为每个箱形图赋予了 fill
美学中的唯一值。将 fill
更改为 group
也无济于事。
既然每个箱形图都已经被赋予了独特的群体美感,那么现在代码有什么问题。
谢谢。
如果您要提供箱线图的统计数据,它应该是每组一个值,所以不要做 mutate()
,请尝试 summarize()
:
set.seed(240193)
df1 <- data.frame(person=rep(c("a","b"),each = 10),
score=c(rnorm(10,8,1),rnorm(10,6,1.5)))
df1 <- df1 %>%
group_by(person) %>%
summarize(d_ymin = min(score),
d_ymax = max(score),
d_lower = quantile(score, 0.25),
d_middle = median(score),
d_upper = quantile(score, 0.75))
# A tibble: 2 x 6
person d_ymin d_ymax d_lower d_middle d_upper
<fct> <dbl> <dbl> <dbl> <dbl> <dbl>
1 a 5.47 8.93 7.47 8.11 8.23
2 b 2.43 9.89 4.97 6.32 6.86
然后剧情:
p1 <- ggplot(df1) +
geom_boxplot(aes(x = person,
ymin = d_ymin,
lower = d_lower,
middle = d_middle,
upper = d_upper,
ymax = d_ymax,
fill = person), stat = "identity")
我正在使用以下代码在 ggplot2 版本 3.3.0 中创建箱线图:
set.seed(240193)
df1 <- data.frame(person=rep(c("a","b"),each = 10),
score=c(rnorm(10,8,1),rnorm(10,6,1.5)))
df1 <- df1 %>%
group_by(person) %>%
mutate(d_ymin = min(score),
d_ymax = max(score),
d_lower = quantile(score, 0.25),
d_middle = median(score),
d_upper = quantile(score, 0.75))
p1 <- ggplot(df1) +
geom_boxplot(aes(x = person,
ymin = d_ymin,
lower = d_lower,
middle = d_middle,
upper = d_upper,
ymax = d_ymax,
fill = person), stat = "identity")
p2 <- ggplot(df1)+
geom_boxplot(aes(x=person,y=score), width=0.1)
由于错误 "Can't draw more than one boxplot per group. Did you forget aes(group = ...)?",无法创建 p1
"Run rlang::last_error()
to see where the error occurred"。 p2
没问题。
我注意到之前 post 将此问题描述为与 ggplot2 版本 3.2.0 的更新相关。据我所知,关键点是一种群体审美,它对每个应该绘制的箱线图都有独特的价值。所以技巧是 aes(group = interaction(x, group)
正如马吕斯回答的那样。但是,post 中的示例有两个变量,因此可以使用 interaction
函数。在我的例子中,我只有一个分组变量,person
。这意味着数据结构自然地为每个箱形图赋予了 fill
美学中的唯一值。将 fill
更改为 group
也无济于事。
既然每个箱形图都已经被赋予了独特的群体美感,那么现在代码有什么问题。
谢谢。
如果您要提供箱线图的统计数据,它应该是每组一个值,所以不要做 mutate()
,请尝试 summarize()
:
set.seed(240193)
df1 <- data.frame(person=rep(c("a","b"),each = 10),
score=c(rnorm(10,8,1),rnorm(10,6,1.5)))
df1 <- df1 %>%
group_by(person) %>%
summarize(d_ymin = min(score),
d_ymax = max(score),
d_lower = quantile(score, 0.25),
d_middle = median(score),
d_upper = quantile(score, 0.75))
# A tibble: 2 x 6
person d_ymin d_ymax d_lower d_middle d_upper
<fct> <dbl> <dbl> <dbl> <dbl> <dbl>
1 a 5.47 8.93 7.47 8.11 8.23
2 b 2.43 9.89 4.97 6.32 6.86
然后剧情:
p1 <- ggplot(df1) +
geom_boxplot(aes(x = person,
ymin = d_ymin,
lower = d_lower,
middle = d_middle,
upper = d_upper,
ymax = d_ymax,
fill = person), stat = "identity")