我想要一个这样的情节,除了每个方面总和为100%。此时组M为0.05 + 0.25 = 0.30而不是0.20 + 0.80 = 1.00。ggplot:两组的相对频率
df <- rbind(
data.frame(gender=c(rep('M',5)), outcome=c(rep('1',4),'0')),
data.frame(gender=c(rep('F',10)), outcome=c(rep('1',7),rep('0',3)))
)
df
ggplot(df, aes(outcome)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
facet_wrap(~gender, nrow=2, ncol=1)
(使用Y = ..density ..给出更坏的结果。)
这里提供的答案是正确的方法。 ..density ..方法提供了许多其他答案, http://stackoverflow.com/questions/10064080/plot-relative-frequencies-with-dodged-bar-plots-in-ggplot2,http:// stackoverflow。 com/questions/17368223/ggplot2-multi-group-histogram-with-in-group-proportionortions-even-frequency,http://stackoverflow.com/questions/3695497/ggplot-showing-instead-of-counts-分类变量图表产生不同程度不正确的值。 – russellpierce