英文:
Add a "total" group when plotting data by group
问题
希望这不是重复的(如果是的话,对不起)。我有一个来自调查的数据框,其中每一行代表一个个体,该个体有一个性别和一个分数。所以我可以用这段代码来绘制按性别分数的分布。
library(tidyverse)
d <- data.frame(
sex = sample(c("男性", "女性"), 200, replace = TRUE),
score = sample(1:10, 200, replace = TRUE)
)
d %>%
ggplot(aes(x=sex, y=score)) +
geom_violin() +
geom_boxplot(width=0.1, color="dimgray") +
scale_fill_viridis_d() +
coord_flip() +
labs(x="性别", y="分数", title="性别分数")
现在,我想在这两个下方添加一个第三个箱线图-小提琴图,它将显示所有个体的分布。当我用aes(x="All", y=score)
替换aes(x=sex, y=score)
时,我可以做到这一点,但我想自动化它并将其放在其他两个下面。
换句话说,我想要一个这个"Ensemble"列的图形版本。
> library(questionr)
> cprop(table(quant.cut(d$score, nbclass = 4), d$sex))
女性 男性 总体
[1,3) 22.7 17.5 20.0
[3,5) 23.7 23.3 23.5
[5,8) 25.8 31.1 28.5
[8,10] 27.8 28.2 28.0
总计 100.0 100.0 100.0
我确信有一个简单的解决方案,但我只是找不到它(如果听起来“太明显”,对不起)。
英文:
I hope this is not a duplicate (sorry if it is). I have a dataframe coming from a survey where each row represent an individual, who has a sex and a score. So I can plot the distribution of scores by sex with this code.
library(tidyverse)
d <- data.frame(
sex = sample(c("Male", "Female"), 200, replace = TRUE),
score = sample(1:10, 200, replace = TRUE)
)
d %>%
ggplot(aes(x=sex, y=score)) +
geom_violin() +
geom_boxplot(width=0.1, color="dimgray") +
scale_fill_viridis_d() +
coord_flip() +
labs(x="Sex", y="Score", title="Score by sex")
Now, I would like to add a third boxplot-violin graph below these two, which would show the distribution among all individuals.
I can do this graph when I replace aes(x=sex, y=score)
with aes(x="All", y=score)
, but I would like to automate it and have it below the two others.
To put it another way, I would like a graphical version of this "Ensemble" column.
> library(questionr)
> cprop(table(quant.cut(d$score, nbclass = 4), d$sex))
Female Male Ensemble
[1,3) 22.7 17.5 20.0
[3,5) 23.7 23.3 23.5
[5,8) 25.8 31.1 28.5
[8,10] 27.8 28.2 28.0
Total 100.0 100.0 100.0
I'm sure there is a simple solution for this but I just can't find it (sorry if it sounds "too obvious").
答案1
得分: 2
以下是您要翻译的代码部分:
一个解决方案:
具有显著分数差异的示例数据(以展示它的工作原理)
```R
d <- data.frame(
sex = gl(2, 100, labels = c("男性", "女性")),
score = c(rnorm(100, 10), rnorm(100, 20))
)
library(dplyr)
library(ggplot2)
d %>%
ggplot(aes(x=sex, y=score)) +
geom_violin() +
geom_violin(aes(x = 'total')) +
geom_boxplot(width=0.1, color="深灰色") +
geom_boxplot(aes(x = 'total'), width=0.1, color="深灰色") +
scale_fill_viridis_d() +
coord_flip() +
labs(x="性别", y="分数", title="性别分数")
[1]: https://i.stack.imgur.com/2mog7.png
英文:
one solution:
example data with dramatic score difference (to show it works)
d <- data.frame(
sex = gl(2, 100, labels = c("Male", "Female")),
score = c(rnorm(100, 10), rnorm(100, 20))
)
library(dplyr)
library(ggplot2)
d %>%
ggplot(aes(x=sex, y=score)) +
geom_violin() +
geom_violin(aes(x = 'total')) +
geom_boxplot(width=0.1, color="dimgray") +
geom_boxplot(aes(x = 'total'), width=0.1, color="dimgray") +
scale_fill_viridis_d() +
coord_flip() +
labs(x="Sex", y="Score", title="Score by sex")
答案2
得分: 1
复制 d 并将性别替换为“全部”
library(dplyr)
library(ggplot2)
d_all <- d
d_all$sex <- "全部"
bind_rows(d, d_all) %>%
ggplot(aes(x=sex, y=score)) +
geom_violin() +
geom_boxplot(width=0.1, color="灰色") +
scale_fill_viridis_d() +
coord_flip() +
labs(x="性别", y="分数", title="性别和全部的分数")
英文:
Create a copy of d and replace sex with ALL
library(dplyr)
library(ggplot2)
d_all <- d
d_all$sex <- "All"
bind_rows(d, d_all) %>%
ggplot(aes(x=sex, y=score)) +
geom_violin() +
geom_boxplot(width=0.1, color="dimgray") +
scale_fill_viridis_d() +
coord_flip() +
labs(x="Sex", y="Score", title="Score by sex and All")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论