如何在ggplot2中使用填充美学来绘制两组的相对比例? [英] How can I plot the relative proportions of two groups using a fill aesthetic in ggplot2?

查看:160
本文介绍了如何在ggplot2中使用填充美学来绘制两组的相对比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我在这里问这个问题是因为关于这个主题的其他答案似乎不正确( ex1 ex2 ,以及 CV meta )。 .. density .. 与概念相关,但与比例不同( ex4 EX5 )。所以正确的答案似乎并不涉及密度。



例子:

  set.seed(1200)
test < - data.frame(
test1 = factor(sample(letters [1:2],100,replace = TRUE,prob = c .75)),ordered = TRUE,levels = letters [1:2]),
test2 = factor(样本(字母[3:8],100,replace = TRUE),ordered = TRUE,levels =字母[3:8])

ggplot(test,aes(test2))+ geom_bar(aes(y = ..density ..,group = test1,fill = test1),position =dodge )
#例如,绘制的数据显示level axc略微超过.15,但手动计算显示的值为.138
counts< - with(test,table(test1 ,test2))
counts / matrix(rowSums(counts),nrow = 2,ncol = 6)

答案似乎产生的结果是正确度假村到不使用ggplot2(在ggplot2之外计算它)或者需求的解决方案res:使用面板而不是填充美学。

编辑:挖掘stat_bin会导致最终调用的函数是bin,只有通过了xes中的值。在不重写stat_bin(或创建另一个stat_)的情况下,在上面引用的答案中应用的破解可以推广到缺少组aes 时的填充aes ,其中包含以下针对y aes的代码: y = ..count ../ sapply(fill,FUN = function(x)sum(count [fill == x]))。这只是用填充替代PANEL(StatBin结尾处的隐藏列)。据推测,其他隐藏的变量可能会得到相同的处理。 解决方案

这是一个可敬的黑客,但它似乎做你想做的。 ...

  ggplot(test,aes(test2))+ geom_bar(aes(y = ..count ../ rep( c(sum(.. count .. [1:6]),sum(.. count .. [7:12])),each = 6),
group = test1,fill = test1),position =dodge)+
scale_y_continuous(name =proportion)


How can I plot the relative proportions of two groups using a fill aesthetic in ggplot2?

I am asking this question here because several other answers on this topic seem incorrect (ex1, ex2, and ex3), but Cross Validated seems to have functionally banned R specific questions (CV meta). ..density.. is conceptually related to, but distinct from proportions (ex4 and ex5). So the correct answer does not seem to involve density.

Example:

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)

The answer that seems to yield an output that is correct resorts to a solution that doesn't use ggplot2 (calculating it outside of ggplot2) or requires that a panel be used rather than a fill aesthetic.

Edit: Digging into stat_bin yields that the function ultimately called is bin, but bin only gets passed the values in the x aes. Without rewriting stat_bin (or making another stat_) the hack that was applied in the above referenced answer can be generalized to the fill aes in the absence of the group aes with the following code for the y aes: y = ..count../sapply(fill, FUN=function(x) sum(count[fill == x])). This just replaces PANEL (the hidden column that is present at the end of StatBin) with fill). Presumably other hidden variables could get the same treatment.

解决方案

This is an aweful hack, but it seems to do what you want...

ggplot(test, aes(test2)) + geom_bar(aes(y = ..count../rep(c(sum(..count..[1:6]), sum(..count..[7:12])), each=6), 
                                    group=test1, fill=test1) ,position="dodge") + 
                                      scale_y_continuous(name="proportion")

这篇关于如何在ggplot2中使用填充美学来绘制两组的相对比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆