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

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

add count and percentage in the y-axis

问题

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

我的代码:

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

我的数据:

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, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 
1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 
2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 
1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 
1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 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",

<details>
<summary>英文:</summary>

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. 

[![something like this][1]][1]

My code:

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


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

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

英文:

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

library(tidyverse)

sur %&gt;%
  count(sex_vic) %&gt;%
  mutate(label = paste0(sex_vic, &#39;\n(&#39;, n, &#39;/&#39;, sum(n), &#39;, &#39;, 
                        scales::percent(n / sum(n), 0.01), &#39;)&#39;)) %&gt;%
  ggplot(aes(x = n, y = label)) +
  geom_col() +
  labs(x = &quot;sex&quot;, y = &quot;No of people&quot;) +
  ggtitle(&quot;Sex of victims&quot;) +
  theme(axis.text.y.left = element_text(hjust = 0.5))

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

答案2

得分: 0

你可以这样做:

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

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

sur %>%
ggplot(aes(y = sex_vic)) +
geom_bar() +
labs(x = "性别", y = "人数") +
ggtitle("受害者性别") + 
scale_y_discrete(labels = lab_vec)

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

英文:

You could do something like this:

lab_vec &lt;- sur %&gt;%
count(sex_vic) %&gt;%
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;%
pull(labels)

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

sur %&gt;%
ggplot(aes(y = sex_vic)) +
geom_bar() +
labs(x = &quot;sex&quot;, y = &quot;No of people&quot;) +
ggtitle(&quot;Sex of victims&quot;) + 
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:

确定