列与组成比例的并排条形图(相对频率条形图) [英] Side-by-side bar chart with columns proportional by group (relative frequency bar chart)
本文介绍了列与组成比例的并排条形图(相对频率条形图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
数据集
gender <- c('Male', 'Male', 'Male', 'Female', 'Female', 'Female', 'Male', 'Male', 'Male', 'Female', 'Female', 'Female', 'Female', 'Female', 'Male', 'Female', 'Female', 'Male', 'Female', 'Female')
answer <- c('Yes', 'No', 'Yes', 'Yes', 'No', 'No', 'No', 'No', 'No', 'No', 'No', 'Yes', 'No', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'Yes')
df <- data.frame(gender, answer)
偏向女性:
df %>% ggplot(aes(gender, fill = gender)) + geom_bar()
我的任务是建立一个图表,以便轻松找出两个性别中哪个更可能说'是'
。
My task is to build a graph that makes it easy to figure out which of the two genders is more likely to say 'Yes'
.
但是,考虑到偏见,我不能做
But, given the bias, I cannot just do
df %>% ggplot(aes(x = answer, fill = gender)) + geom_bar(position = 'dodge')
甚至
df %>% ggplot(aes(x = answer, y = ..count../sum(..count..), fill = gender)) +
geom_bar(position = 'dodge')
为减轻这种偏见,我需要将每个计数分别除以男性或女性的总数,以使'Female'
条加起来为 1
以及'Male'
。像这样:
To alleviate the bias I need to divide each of the counts by the total number of males or females respectively so that the 'Female'
bars add up to 1
as well as the 'Male'
ones. Like so:
df.total <- df %>% count(gender)
male.total <- (df.total %>% filter(gender == 'Male'))$n
female.total <- (df.total %>% filter(gender == 'Female'))$n
df %>% count(answer, gender) %>%
mutate(freq = n/if_else(gender == 'Male', male.total, female.total)) %>%
ggplot(aes(x = answer, y = freq, fill = gender)) +
geom_bar(stat="identity", position = 'dodge')
这会绘制出完全不同的图。
Which draws a completely different picture.
问题:
- 是否可以仅使用
dplyr
和ggplot2
来简化前一段代码? - 是否有其他库可以更好地完成技巧?
- 以上类型吗的图表具有常规名称?
- Is there a way to simplify the former piece of code using only
dplyr
andggplot2
? - Are there any other libraries that can do the trick better?
- Does the above type of chart have a conventional name?
谢谢。
推荐答案
问题1:
df %>%
count(gender, answer) %>%
group_by(gender) %>%
mutate(freq = n/sum(n)) %>%
ggplot(aes(x = answer, y = freq, fill = gender)) +
geom_bar(stat="identity", position = 'dodge')
问题2:
与其他软件包相比,您可以用更少的行数来完成它。
You can probably do it in fewer lines with other packages.
问题3 :
相对频率条形图。
这篇关于列与组成比例的并排条形图(相对频率条形图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文