如何使用geom_text在堆积条形图中显示值? [英] How to display value in a stacked bar chart by using geom_text?

查看:810
本文介绍了如何使用geom_text在堆积条形图中显示值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示堆叠栏中的百分比数字。但是,一个组的比例非常低。两个值彼此重叠。我改为'postion ='身份证'。它仍然不会工作.....任何想法?

I would like to display the percentage figures in the stacked bar. However, one group has a really low percentage. Two values are overlapping each other. I change to 'postion='identity'. It still wont work.....any thoughts?

x4.can.m <- structure(list(canopy = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("0%", "1 to 84%", 
"85% +"), class = "factor"), YearQuarter = structure(c(1L, 1L, 
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("2011-09-01", 
"2011-12-01", "2012-03-01", "2012-06-01", "2012-09-01"), class = "factor"), 
    value = c(0.51, 0.01, 0.48, 0.52, 0.01, 0.47, 0.53, 0.01, 
    0.47, 0.57, 0.01, 0.41, 0.61, 0.01, 0.38)), .Names = c("canopy", 
"YearQuarter", "value"), row.names = c(NA, -15L), class = "data.frame")


x4.can.bar <- ggplot(data=x4.can.m, aes(x=factor(YearQuarter), y=value,fill=canopy)) + geom_bar(stat="identity",position = "stack",ymax=100)

x4.can.bar+scale_y_continuous(formatter='percent')+
 labs(y="Percentage",x="Year Quarter") + 
 geom_text(aes(label =paste(round(value*100,0),"%",sep="")),size = 3, hjust = 0.5, vjust = 4,position ="identity")


推荐答案

一个解决方案是将堆栈栏更改为闪避一个

one solution is to change the stack bar to a dodge one

x4.can.bar <- ggplot(data=x4.can.m, aes(x=factor(YearQuarter), y=value,fill=canopy)) + 
                    geom_bar(stat="identity",position = "dodge",ymax=100) +
             geom_text(aes(label =paste(round(value*100,0),"%",sep=""),ymax=0), 
                       position=position_dodge(width=0.9), vjust=-0.25)
x4.can.bar

这篇关于如何使用geom_text在堆积条形图中显示值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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