ggplot2:geom_bar,计算多方面的百分比 [英] ggplot2: geom_bar, computing facet-wise percentage
问题描述
我想将 geo_bar
与构面一起使用,获取百分比而不是绝对计数,但是百分比应该相对于每个构面,而不是相对于整体计数.
对此进行了很多讨论(
当x变量为连续变量时,默认情况下,看起来 stat_count
会计算构面中所有数据的百分比.但是,如果x变量是分类变量,则 stat_count
会分别计算每个x级别内的百分比.看看下面的例子会发生什么:
将 val_num
添加为组外观会导致在每个x级别内计算百分比,而不是对构面中的所有值进行计算.
ggplot(df)+stat_count(aes(x = val_num,y = .. prop ..,group = val_num))+facet_grid(group〜.)
将 val_num
转换为一个因子同样会导致在每个x级别内计算百分比,而不是对构面中的所有值进行计算.
ggplot(df)+stat_count(aes(x = factor(val_num),y = .. prop ..))+facet_grid(group〜.)
I would like to use the geo_bar
with facets, obtaining percentage instead of absolute counts, but percentage should be relative to each facet, not relative to the overall count.
This has been discussed a lot (example), suggesting to use geom_bar(aes(y = (..count..)/sum(..count..)))
. This won't work with facets (i.e. will give total count). A better solution has been suggested,
using stat_count(mapping = aes(x=x_val, y=..prop..))
instead.
This seems to work if x
is numeric, but not if x
is character: all bars are 100%! Why? Am I doing something wrong? Thanks!
library(tidyverse)
df <- data_frame(val_num = c(rep(1, 60), rep(2, 40), rep(1, 30), rep(2, 70)),
val_cat = ifelse(val_num==1, "cat", "mouse"),
group=rep(c("A", "B"), each=100))
#works with numeric
ggplot(df) + stat_count(mapping = aes(x=val_num, y=..prop..)) + facet_grid(group~.)
# does not work?
ggplot(df) + stat_count(mapping = aes(x=val_cat, y=..prop..)) + facet_grid(group~.)
Adding group=group
tells ggplot to calculate proportions by group
, rather than the default, which would be separately for each level of val_cat
.
ggplot(df) +
stat_count(aes(x=val_cat, y=..prop.., group=group)) +
facet_grid(group~.)
When the x-variable is continuous, it looks like stat_count
by default calculates percentages over all data in the facet. However, when the x-variable is categorical, stat_count
calculates percentages separately within each x level. See what happens with the following examples:
Adding val_num
as the group aesthetic causes percentages to be calculated within each x level instead of over all values in a facet.
ggplot(df) +
stat_count(aes(x=val_num, y=..prop.., group=val_num)) +
facet_grid(group~.)
Turning val_num
into a factor likewise causes percentages to be calculated within each x level instead of over all values in a facet.
ggplot(df) +
stat_count(aes(x=factor(val_num), y=..prop..)) +
facet_grid(group~.)
这篇关于ggplot2:geom_bar,计算多方面的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!