Reputation: 2613
This code
library(ggplot2)
test=read.table(text=
"group fillcd percent
1 1 73.2
1 2 73.8
1 3 78.6
2 1 78.1
2 2 95.6
2 3 95.5
", header=TRUE, sep="" )
test$fill <- factor(test$fillcd, labels=c("XX", "EE", "BB"))
test$text=paste(test$percent,"%")
ggplot(data=test,
aes(group, percent, fill=fill)) +
geom_bar(stat="identity",position="dodge")+
coord_flip()+
geom_text(data = test, aes(y = percent, x = group, label = text ))
produces the following graph:
How can I obtain the midpoints of the bars in order to place a label there?
Upvotes: 3
Views: 727
Reputation: 66852
I'm not sure if you mean horizontal or vertical midpoint, but maybe the following example will help:
ggplot(data=test,
aes(group, percent, fill=fill)) +
geom_bar(stat="identity",position=position_dodge(width = 0.9))+
coord_flip()+
geom_text(data = test, aes(y = percent/2, x = group, label = text ),
position = position_dodge(width = 0.9))
The key is position_dodge
.
Upvotes: 6