ggplot2 position =闪避给出错误的标签

时间:2014-03-20 20:57:37

标签: r ggplot2 position

我正在尝试使用ggplot2中的标签绘制一个简单的条形图。但是,当我使用position = dodge时,它会在结果图形中放入错误的标签,例如。卡车的17.6%而不是77.7%。我的数据和代码如下。

library(ggplot2)

mode <- factor(c("Truck", "Rail","Water","Air","Other"), levels=c("Truck", "Rail","Water","Air","Other"))
Year <- factor(c("2011","2011","2011","2011","2011","2040","2040","2040","2040","2040"))
share <- c(0.709946085, 0.175582806, 0.11392987, 0.000534132, 0.00000710797, 0.777162621, 0.133121584, 0.088818658, 0.000880041, 0.000017097)
modeshares <- data.frame(Year, mode, share)

theme_set(theme_grey(base_size = 18)) 

modeshares$lab <- as.character(round(100 * share,1))
modeshares$lab <- paste(modeshares$lab,"%",sep="")

ggplot(data=modeshares, aes(x=mode, y=share*100, fill=Year, ymax=(share*100))) + geom_bar(stat="identity", position="dodge") + labs(y="Percent",x="Mode") +geom_text(label=modeshares$lab,position=position_dodge(width=1),vjust=-0.5)

结果图如下所示。 Modeshares

非常感谢有关如何确保显示正确标签值的任何见解。

谢谢!

0 个答案:

没有答案