相关疑难解决方法(0)

ggplot:两组的相对频率

我想要一个这样的情节,除了每个方面总和达到100%.现在组M是0.05 + 0.25 = 0.30而不是0.20 + 0.80 = 1.00.

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) 
Run Code Online (Sandbox Code Playgroud)

(使用y = ..density ..会产生更糟糕的结果.)

r ggplot2

6
推荐指数
2
解决办法
8445
查看次数

如何在ggplot2中使用填充美学来绘制两组的相对比例?

如何在ggplot2中使用填充美学来绘制两组的相对比例?

我在这里问这个问题,因为关于这个主题的其他几个答案似乎不正确(ex1,ex2ex3),但是Cross Validated似乎在功能上禁止了R特定问题(CV meta). ..density..在概念上与比例相关,但与比例不同(ex4ex5).所以正确答案似乎不涉及密度.

例:

set.seed(1200)
test <- data.frame(
  test1 = factor(sample(letters[1:2], 100, replace = TRUE,prob=c(.25,.75)),ordered=TRUE,levels=letters[1:2]), 
  test2 = factor(sample(letters[3:8], 100, replace = TRUE),ordered=TRUE,levels=letters[3:8])
)
ggplot(test, aes(test2)) + geom_bar(aes(y = ..density.., group=test1, fill=test1) ,position="dodge")
#For example, the plotted data shows level a x c as being slightly in excess of .15, but a manual calculation shows a value of .138
counts <- with(test,table(test1,test2))
counts/matrix(rowSums(counts),nrow=2,ncol=6)
Run Code Online (Sandbox Code Playgroud)

似乎产生正确输出的答案适用于不使用ggplot2(在ggplot2之外计算它)的解决方案,或者需要使用面板而不是填充美学. …

statistics visualization r data-visualization ggplot2

5
推荐指数
1
解决办法
2642
查看次数