R 堆积百分比条形图,带有二元因子和标签的百分比(使用 ggplot) [英] R stacked percentage bar plot with percentage of binary factor and labels (with ggplot)
问题描述
我想制作一个看起来像这样的图形:
I want to produce a graphic that looks something like this:
我的原始数据集如下所示:
My original data set looks something like this:
> bb[sample(nrow(bb), 20), ]
IMG QUANT FIX
25663 1 1 0
7936 2 2 0
23586 3 2 0
23017 2 2 1
31363 1 3 1
7886 2 2 0
23819 3 3 1
29838 2 2 1
8169 2 3 1
9870 2 3 0
31440 2 1 0
35564 3 1 0
24066 1 2 0
12020 3 2 0
6742 3 2 0
6189 2 3 0
26692 2 3 0
1387 3 2 0
31839 2 3 1
28637 3 2 0
所以想法是条形显示 FIX = 1
每个因子 QUANT
和每个因素 IMG
.
So the idea is that the bars display where FIX = 1
per factor QUANT
and per
factor IMG
.
我使用 plyr
library(plyr)
bb.perc <- ddply(bb,.(QUANT,IMG),summarise,FIX.PROP = sum(FIX) / length(FIX))
它几乎做了正确的事情:
QUANT IMG FIX.PROP
1 1 1 0.52439024
2 1 2 0.19085366
3 1 3 0.13658537
4 2 1 0.20414201
5 2 2 0.53964497
6 2 3 0.09585799
7 3 1 0.29000000
8 3 2 0.13000000
9 3 3 0.40705882
但是现在如果我制作图表,它不考虑 FIX==0
的情况,即所有条具有相同的高度,即 100%,这不是我想要的.请注意单个 QUANT 子帧的总和不是 100%:
But now if I make a graph, it doesn't account for the FIX==0
cases, i.e. all bars have the same height, namely 100%, which isn't what I want. Note how the individual QUANT subframes don't add up to 100%:
> sum(bb.perc[1:3,]$FIX.PROP)
[1] 0.8518293
> sum(bb.perc[4:6,]$FIX.PROP)
[1] 0.839645
> sum(bb.perc[7:9,]$FIX.PROP)
[1] 0.8270588
我能用 R 做的最好的事情就是显示计数:
The best I could do with R is to display counts:
# Take only the positive samples
bb.pos <- bb[bb$FIX == 1,]
# Plot the counts
ggplot(bb,aes(factor(QUANT),fill=factor(IMG))) + geom_bar() +
scale_y_continuous(labels=percent)
结果是:这也不是我想要的:
And results in: This is also not what I want:
- 百分比规模相差甚远.我需要一种方法将 100% 点传递给
percent
函数,但我不知道如何. - 它缺少标签.
- The percentage scale is way off. I need a way to pass the 100% point to the
percent
function, but I have no idea how. - It lacks the labels.
已经有很多关于 SO 的类似问题,但我似乎缺乏足够的智力(或对 R 的理解)来推断从他们到我的特定问题的解决方案.
There are a great deal of similar questions on SO already, but I seem to lack the sufficient amount of intelligence (or understanding of R) to extrapolate from them to a solution to my particular problem.
感谢您的指点!
Sven Hohenstein 已经提供了一个答案,但我自己也是这样做的:
Sven Hohenstein provided an answer already, but here's how I ended up doing it myself as well:
> ggplot(bb.perc,aes(x=factor(QUANT),y=FIX.PROP,label=paste(round(FIX.PROP*100),
"%"),fill=factor(IMG)))+ geom_bar(stat="identity") + geom_text(position="stack",
aes(ymax=1),vjust=5) + scale_y_continuous(labels = percent)
使用我使用 plyr
进一步定义的 bb.perc
.这个有优点是百分比是按列本地计算的,而不是全球.
Using the bb.perc
that I defined further up using plyr
. This one has the
advantage that the percentages are computed locally per column, and not
globally.
感谢大家的帮助.以下两个问题及其各自的答案对我做对有很大帮助:
Thanks everyone for the help. The following two questions and their respective answers helped me greatly in getting it right:
我最初做错的是将 position = "fill"
参数传递给geom_bar()
,由于某种原因使所有条具有相同的高度!
What I did wrong initially, was pass the position = "fill"
parameter to
geom_bar()
, which for some reason made all the bars have the same height!
推荐答案
这是一种生成情节的方法:
This is a way to generate the plot:
ggplot(bb[bb$FIX == 1, ],aes(x = factor(QUANT), fill = factor(IMG),
y = (..count..)/sum(..count..))) +
geom_bar() +
stat_bin(geom = "text",
aes(label = paste(round((..count..)/sum(..count..)*100), "%")),
vjust = 5) +
scale_y_continuous(labels = percent)
改变vjust
参数的值来调整标签的垂直位置.
Change the value of the vjust
parameter to adjust the vertical position of the labels.
这篇关于R 堆积百分比条形图,带有二元因子和标签的百分比(使用 ggplot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!