添加一个“总计”组,当按组绘制数据时。

huangapple go评论74阅读模式
英文:

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 &lt;- data.frame(
    sex = sample(c(&quot;Male&quot;, &quot;Female&quot;), 200, replace = TRUE),
    score = sample(1:10, 200, replace = TRUE)
)

d %&gt;%
    ggplot(aes(x=sex, y=score)) +
    geom_violin() +
    geom_boxplot(width=0.1, color=&quot;dimgray&quot;) +
    scale_fill_viridis_d() +
    coord_flip() +
    labs(x=&quot;Sex&quot;, y=&quot;Score&quot;, title=&quot;Score by sex&quot;)

添加一个“总计”组,当按组绘制数据时。

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=&quot;All&quot;, 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.

&gt; library(questionr)
&gt; 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 &lt;- data.frame(
    sex = gl(2, 100, labels = c(&quot;Male&quot;, &quot;Female&quot;)),
    score = c(rnorm(100, 10), rnorm(100, 20))
)
library(dplyr)
library(ggplot2)

d %&gt;%
    ggplot(aes(x=sex, y=score)) +
    geom_violin() +
    geom_violin(aes(x = &#39;total&#39;)) +
    geom_boxplot(width=0.1, color=&quot;dimgray&quot;) +
    geom_boxplot(aes(x = &#39;total&#39;), width=0.1, color=&quot;dimgray&quot;) +
    scale_fill_viridis_d() +
    coord_flip() +
    labs(x=&quot;Sex&quot;, y=&quot;Score&quot;, title=&quot;Score by sex&quot;)

添加一个“总计”组,当按组绘制数据时。

答案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 &lt;- d
d_all$sex &lt;- &quot;All&quot;

bind_rows(d, d_all) %&gt;%
  ggplot(aes(x=sex, y=score)) +
  geom_violin() +
  geom_boxplot(width=0.1, color=&quot;dimgray&quot;) +
  scale_fill_viridis_d() +
  coord_flip() +
  labs(x=&quot;Sex&quot;, y=&quot;Score&quot;, title=&quot;Score by sex and All&quot;)

添加一个“总计”组,当按组绘制数据时。

huangapple
  • 本文由 发表于 2023年6月27日 17:27:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定