我如何用ggplot绘制一个堆叠的酒吧? [英] How do I plot a stacked bar with ggplot?

查看:146
本文介绍了我如何用ggplot绘制一个堆叠的酒吧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览页面上提供的示例,但出于某种原因无法找到正确的做法。



我有这样的数据:

 集团成员百分比
[1,]1A60
[2,]1A20
[3, ]1A20
[4,]1B80
[5,]1B5
[6 ,1B5
[7,]1B5
[8,]2C50
[ 9,]2C50
[10,]2D25
[11,]2D25
[12,]2D25
[13,]2D20
[14,]2D5

可以使用以下命令创建:

  a = c(1,1,1,1,1,1,1,2,2,2,2,2,2,2)
b = c(A , A, A, B, B, B, B, C, C, d, d, d, d, d)
c = c(60,20,20,80,5,5,5,50,50,25,25,25,20,5)
dat = data.frame(Group = a,Member = b,百分比= c)
ggplot(dat,aes(x = Member,y = Percentage))+ geom_bar(stat =identity,position =dodge,fill =white,color =黑色)

最后一行给出了以下图表:



我真正想要的是将一个组中的每个小节连接到一个单独的小节,并将百分比表示为同一小节的分数(每个小组中的每个成员用一个小节绘制,每个小节的百分比分别为他们的颜色)。所以我使用了以下内容:

  ggplot(dat,aes(x = Member,y = Percentage))+ geom_bar(stat = 身份,颜色=白色)

并获得了此结果:





但现在我无法正确地获得颜色。我想要的东西完全像下面给出的东西,但我无法理解如何做到这一点。如何做到这一点的任何建议?



解决方案

好了,终于搞定了!欢呼!这是完整的代码,如果其他人感兴趣:

  a = c(1,1,1,1,1,1, (A,A,A,B,B,B,B ,C,C,D,D,D,D,D)
c = c(60,20,20,80,5,5,5, 50,50,25,25,25,20,5)
dat = data.frame(Group = a,Member = b,Percentage = c)
ggplot(dat,aes(x = Member, y =百分比,fill =百分比))+ geom_bar(stat =identity,color =white)



以及

  ggplot(dat,aes(x = Member, y =百分比,fill =百分比))+ geom_bar(stat =identity,color =white)


I have been going through the examples provided on this page, but for some reason unable to find the right way of doing this.

I have some data like this:

      Group Member Percentage
 [1,] "1"   "A"    "60"      
 [2,] "1"   "A"    "20"      
 [3,] "1"   "A"    "20"      
 [4,] "1"   "B"    "80"      
 [5,] "1"   "B"    "5"       
 [6,] "1"   "B"    "5"       
 [7,] "1"   "B"    "5"       
 [8,] "2"   "C"    "50"      
 [9,] "2"   "C"    "50"      
[10,] "2"   "D"    "25"      
[11,] "2"   "D"    "25"      
[12,] "2"   "D"    "25"      
[13,] "2"   "D"    "20"      
[14,] "2"   "D"    "5"   

and can be created with the following commands:

a = c(1,1,1,1,1,1,1,2,2,2,2,2,2,2)
b = c("A","A","A","B","B","B","B","C","C","D","D","D","D","D")
c = c(60,20,20,80,5,5,5,50,50,25,25,25,20,5)
dat = data.frame(Group=a, Member=b, Percentage=c)
ggplot(dat, aes(x=Member, y=Percentage)) + geom_bar(stat="identity", position="dodge", fill="white", colour="black")

The last line gives me the following plot:

What I am really looking for is to concatenate each of the bars in one group to one single bar and represent the percentages as fraction of the same bar (where each member from each group is plotted with one bar with each bar having the percentages as their colors). So I used the following:

ggplot(dat, aes(x=Member, y=Percentage)) + geom_bar(stat="identity", colour="white")

and obtained this:

But now I can't get the colors properly. I want something exactly like the one give below but I am not able to understand how to do this. Any suggestions on how to do this?

解决方案

Ok finally got it! Hurray! Here's the complete code if someone else is interested:

a = c(1,1,1,1,1,1,1,2,2,2,2,2,2,2)
b = c("A","A","A","B","B","B","B","C","C","D","D","D","D","D")
c = c(60,20,20,80,5,5,5,50,50,25,25,25,20,5)
dat = data.frame(Group=a, Member=b, Percentage=c)
ggplot(dat, aes(x=Member, y=Percentage, fill=Percentage)) + geom_bar(stat="identity", colour="white")

and with what was suggested by @joran (Thanks a lot!) in the comments:

ggplot(dat, aes(x=Member, y=Percentage, fill=Percentage)) + geom_bar(stat="identity", colour="white")

这篇关于我如何用ggplot绘制一个堆叠的酒吧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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