英文:
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 "sex_vic" 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, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
2L, 2L, 2L, 2L,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 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))
英文:
You could simply summarize the data frame and change the label names:
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 = "sex", y = "No of people") +
ggtitle("Sex of victims") +
theme(axis.text.y.left = element_text(hjust = 0.5))
答案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)
英文:
You could do something like this:
lab_vec <- sur %>%
count(sex_vic) %>%
mutate(labels = paste0(sex_vic,"\n(n=",n, "/",sum(n), ", ", round(n/sum(n)*100,2), "%)")) %>%
pull(labels)
Then add scale_y_discrete(labels = lab_vec)
onto the end of your ggplot
call:
sur %>%
ggplot(aes(y = sex_vic)) +
geom_bar() +
labs(x = "sex", y = "No of people") +
ggtitle("Sex of victims") +
scale_y_discrete(labels = lab_vec)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论