您可以使用stat_summary()
计算位置y
值的总和depth
并使用geom="text"
添加标签。使用总和是因为您的条形显示了总和depth
每个值cut
value.
正如@joran建议的那样,最好使用stat_summary()
代替geom_bar()
显示 y 值的总和,因为stat="identity"
由于条形图过度绘制而产生问题,如果存在负值,则条形图将从图的负部分开始并以正部分结束 - 结果将不是值的实际总和。
ggplot(diamonds[1:100,], aes(x = cut, y = depth)) +
facet_wrap(~ color) +
stat_summary(fun.y = sum, geom="bar", fill = "blue", aes(label=cut, vjust = 0)) +
stat_summary(fun.y = sum, geom="text", aes(label=cut), vjust = 0)
您还可以预先计算深度值和您可以使用的总和geom_bar()
with stat="identity"
and geom_text()
.
library(plyr)
diamonds2<-ddply(diamonds,.(cut,color),summarise,depth=sum(depth))
ggplot(diamonds2,aes(x=cut,y=depth))+
geom_bar(stat="identity",fill="blue")+
geom_text(aes(label=cut),vjust=0,angle=45,hjust=0)+
facet_wrap(~color)