我想要一個這樣的情節,除了每個方面總和爲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