geom_text与躲闪的barplot [英] geom_text with dodged barplot

查看:657
本文介绍了geom_text与躲闪的barplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试添加文字,如



如何在正确的位置添加文本(在每个栏的中间)?
Thanks!

解决方案

您可以尝试

<$ p $ ($)= $ $ $ $ $ $ g $($)$ g $ position =dodge,stat =identity)+
geom_text(aes(y = s / 2,label = paste(round(s,3),%)),position = position_dodge(.9 ))+
scale_x_discrete(labels = function(x)format(as.Date(x),%m /%y))+
xlab(m)


I try to add text as in Position geom_text on dodged barplot But its not work with my simple data

data=data.frame(s=c(10,13,17,8),
                pr=c("a","b","a","b"),
                m=c(rep(as.Date('01.01.2015','%d.%m.%Y'),2), rep(as.Date('01.02.2015','%d.%m.%Y'),2)))

And ggplot

ggplot(data = data 
       ,aes(x = m, y = s,fill=pr ,ymax = max(s)*1.1))+
  geom_bar(position = "dodge",stat="identity")+
  geom_text(aes(y=s/2,label=paste(round(s,3),"%")),position = position_dodge(width=1))+
  scale_x_date(labels = date_format("%m/%y"),breaks = date_breaks("months"))

i get

How to add text in right position( in the middle of each bar)? Thanks!

解决方案

You could try

ggplot(data = data, aes(x = as.factor(m), y = s,fill=pr ,ymax = max(s)*1.1)) + 
  geom_bar(position = "dodge", stat="identity") + 
  geom_text(aes(y=s/2,label=paste(round(s,3),"%")),position = position_dodge(.9)) + 
  scale_x_discrete(labels = function(x) format(as.Date(x), "%m/%y")) + 
  xlab("m")

这篇关于geom_text与躲闪的barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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