加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

如何使用geom_text在堆积条形图中显示值?

发布时间:2020-12-14 21:15:06 所属栏目:资源 来源:网络整理
导读:我想显示堆积条中的百分比数字.但是,一组的百分比非常低.两个值彼此重叠.我改为’postion =’identity’.它仍然无法工作…..任何想法? x4.can.m - structure(list(canopy = structure(c(1L,2L,3L,1L,3L),.Label = c("0%","1 to 84%","85% +"),class = "fact
我想显示堆积条中的百分比数字.但是,一组的百分比非常低.两个值彼此重叠.我改为’postion =’identity’.它仍然无法工作…..任何想法?
x4.can.m <- structure(list(canopy = structure(c(1L,2L,3L,1L,3L),.Label = c("0%","1 to 84%","85% +"),class = "factor"),YearQuarter = structure(c(1L,4L,5L,5L),.Label = c("2011-09-01","2011-12-01","2012-03-01","2012-06-01","2012-09-01"),value = c(0.51,0.01,0.48,0.52,0.47,0.53,0.57,0.41,0.61,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")

解决方法

一种解决方案是将堆栈栏更改为闪避
x4.can.bar <- ggplot(data=x4.can.m,fill=canopy)) + 
                    geom_bar(stat="identity",position = "dodge",ymax=100) +
             geom_text(aes(label =paste(round(value*100,sep=""),ymax=0),position=position_dodge(width=0.9),vjust=-0.25)
x4.can.bar

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读