在y轴上添加计数和百分比。

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

add count and percentage in the y-axis

问题

如何在类别名称下方的括号中显示观察计数、总计数和百分比,变量 "sex_vic" 是一个分类变量。

我的代码:

  1. sur %>%
  2. ggplot(aes(y = sex_vic)) +
  3. geom_bar() +
  4. labs(x = "性别", y = "人数") +
  5. ggtitle("受害者的性别")

我的数据:

  1. structure(list(sex_vic = structure(c(1L, 2L, 2L, 1L, 2L, 2L,
  2. 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
  3. 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
  4. 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L,
  5. 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
  6. 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
  7. 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L,
  8. 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L,
  9. 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
  10. 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
  11. 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L,
  12. 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
  13. 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L,
  14. 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L,
  15. 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
  16. 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L,
  17. 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L,
  18. 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
  19. 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L,
  20. 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L,
  21. 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
  22. 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L,
  23. 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L), levels = c("Female",
  24. <details>
  25. <summary>英文:</summary>
  26. How to show the observed count, total count and percentage in the y-axis in brackets below the category name. The variable &quot;sex_vic&quot; is a categorical variable.
  27. [![something like this][1]][1]
  28. My code:

sur %>%
ggplot(aes(y = sex_vic)) +
geom_bar() +
labs(x = "sex", y = "No of people") +
ggtitle("Sex of victims")

  1. My data:

structure(list(sex_vic = structure(c(1L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 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2L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L), levels = c("Female",
"Male"), class = "factor")), row.names = c(NA, -355L), class = "data.frame")

  1. [1]: https://i.stack.imgur.com/a2BLT.png
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. 你可以简单总结数据框并更改标签名称:
  6. ```r
  7. library(tidyverse)
  8. sur %>%
  9. count(sex_vic) %>%
  10. mutate(label = paste0(sex_vic, '\n(', n, '/', sum(n), ', ',
  11. scales::percent(n / sum(n), 0.01), ')')) %>%
  12. ggplot(aes(x = n, y = label)) +
  13. geom_col() +
  14. labs(x = "性别", y = "人数") +
  15. ggtitle("受害者性别") +
  16. theme(axis.text.y.left = element_text(hjust = 0.5))

在y轴上添加计数和百分比。

英文:

You could simply summarize the data frame and change the label names:

  1. library(tidyverse)
  2. sur %&gt;%
  3. count(sex_vic) %&gt;%
  4. mutate(label = paste0(sex_vic, &#39;\n(&#39;, n, &#39;/&#39;, sum(n), &#39;, &#39;,
  5. scales::percent(n / sum(n), 0.01), &#39;)&#39;)) %&gt;%
  6. ggplot(aes(x = n, y = label)) +
  7. geom_col() +
  8. labs(x = &quot;sex&quot;, y = &quot;No of people&quot;) +
  9. ggtitle(&quot;Sex of victims&quot;) +
  10. theme(axis.text.y.left = element_text(hjust = 0.5))

在y轴上添加计数和百分比。

答案2

得分: 0

你可以这样做:

  1. lab_vec <- sur %>%
  2. count(sex_vic) %>%
  3. mutate(labels = paste0(sex_vic, "\n(n=", n, "/", sum(n), ", ", round(n/sum(n)*100,2), "%)")) %>%
  4. pull(labels)

然后在你的 ggplot 调用的末尾添加 scale_y_discrete(labels = lab_vec)

  1. sur %>%
  2. ggplot(aes(y = sex_vic)) +
  3. geom_bar() +
  4. labs(x = "性别", y = "人数") +
  5. ggtitle("受害者性别") +
  6. scale_y_discrete(labels = lab_vec)

在y轴上添加计数和百分比。

英文:

You could do something like this:

  1. lab_vec &lt;- sur %&gt;%
  2. count(sex_vic) %&gt;%
  3. mutate(labels = paste0(sex_vic,&quot;\n(n=&quot;,n, &quot;/&quot;,sum(n), &quot;, &quot;, round(n/sum(n)*100,2), &quot;%)&quot;)) %&gt;%
  4. pull(labels)

Then add scale_y_discrete(labels = lab_vec) onto the end of your ggplot call:

  1. sur %&gt;%
  2. ggplot(aes(y = sex_vic)) +
  3. geom_bar() +
  4. labs(x = &quot;sex&quot;, y = &quot;No of people&quot;) +
  5. ggtitle(&quot;Sex of victims&quot;) +
  6. scale_y_discrete(labels = lab_vec)

在y轴上添加计数和百分比。

huangapple
  • 本文由 发表于 2023年7月13日 21:16:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679804.html
匿名

发表评论

匿名网友

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

确定