[英]ggplot2: geom_bar with group, position_dodge and fill
[英]Issue with geom_bar + position_dodge
我想用条形图绘制误差线和文本。 为了绘制出美观且易读的图,我希望在geom_text元素的左边(对于左边的栏)和右边(对于右边的栏)显示geom_text
。 我会使用position_dodge
来做到这一点,但是如果不对这些小节进行分组的话,这不会产生任何影响。
这是我的代码:
df1 <- data.frame(e=c(0.02,0.02), na=c("A","B"), value=c(0.25, 0.75))
ggplot(df1, aes(x=factor(na), y=value, fill=factor(na)))+
geom_bar(stat="identity", width=0.2)+
geom_errorbar(aes(ymin = value-e, ymax = value+e), width = 0.01, size=0.5,
alpha=.6)+
geom_text(aes(label=paste0(round(value*100,0),"%"), vjust=-0.5),
size=5)
这将产生相同的情节:
ggplot(df1, aes(x=factor(na), y=value, fill=factor(na)))+
geom_bar(stat="identity", width=0.2)+
geom_errorbar(aes(ymin = value-e, ymax = value+e), width = 0.01, size=0.5,
alpha=.6, position=position_dodge(width=0.2))+
geom_text(aes(label=paste0(round(value*100,0),"%"), vjust=-0.5),
size=5)
如何左右移动错误栏?
谢谢
您可以使用x美感来指定误差线的x位置。 尽管通常的处理习惯是使误差线居中,并移动文本(最好垂直放置)
ggplot(df1, aes(x=factor(na), y=value, fill=factor(na)))+
geom_bar(stat="identity", width=0.2)+
geom_errorbar(aes(x = as.integer(factor(na)) +c(-0.1,0.1), ymin = value-e, ymax = value+e), width = 0.01, size=0.5,
alpha=.6)+
geom_text(aes(label=paste0(round(value*100,0),"%"), vjust=-0.5),
size=5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.