2017-07-17 138 views
0

我在x轴(教育水平)上有一个条形图,我的独立变量和y轴上我的因变量的计数(信用卡债务违约)。barplot的重叠百分比,同时保持在y轴上的计数

ggplot(cleancc, aes(x=factor(Education), fill = factor(DefaultOct05))) + geom_bar()

enter image description here

我想保持的一切,不过是简单地显示在酒吧各个击破的百分比。例如,栏2的蓝色部分是23.7%。

回答

0

由于我没有你的数据集,我不能尝试,但检查出有stat_bin此选项():

ggplot(cleancc, aes(x=factor(Education), fill = factor(DefaultOct05))) + 
geom_bar() + 
stat_bin(geom = "text", 
     aes(label = paste(round((..count..)/sum(..count..)*100), "%")), 
     vjust = 5) 
+0

谢谢你,这有很大帮助。但stat_bin()发生了错误。我用stat_count()代替。唯一的问题是百分比是在总数据集上。我正在寻找显示每个酒吧如何分解的百分比。例如(蓝色和红色应该总计为每个栏的100%)。 – Eitan

+0

正如他们已经告诉你的,你的问题与链接中建议的页面类似。尝试以下方式,从链接页面调整:'summary = cleancc%>%group_by(Education,DefaultOct05)%>% tally%>% group_by(Education)%>% mutate(pct = n/sum ), n.pos = cumsum(N) - 0.5 * N) ggplot(摘要,AES(X =教育,Y = N,填充= DefaultOct05))+ geom_bar(STAT = “同一性”)+ geom_text (aes(label = paste0(sprintf(“%1.1f”,pct * 100),“%”),y = n.pos), color =“white”)' – valz