Reputation: 61
I am trying to add percentages on top of my barplot().
I have been looking and found that this is possible with ggplot, but I am doing this with barplot() and I can't find a way to do it. I tried label.bars, but apparently it is not part of this function.
This is my code:
barplot(table(marriage$year, marriage$dummy) ,
beside=T, ylab="Frequency",
main="Distribution of Opposition and Support on Marriage Equality",
col= c("lightblue" , "lightgreen"),
names.arg=(c("1988", "2010")) ,
legend.text = c("Support", "Oppose"),
ylim = c(0, 1000))
Any ideas?
Thank you!
Upvotes: 1
Views: 1638
Reputation: 73262
Store the table
output in an object (here tab
) as well as the invisible barplot
output (here b
). Then put it in text
using proportions
. Pretty easy :)
b <- barplot(tab <- table(mtcars$vs, mtcars$am),
beside=T, ylab="Frequency",
main="Distribution of Opposition and Support on Marriage Equality",
col= c("lightblue" , "lightgreen"),
names.arg=(c("1988", "2010")) ,
legend.text = c("Support", "Oppose"),
ylim=c(0, 15))
text(x=b, y=tab + .8, labels=paste0(round(proportions(tab), 1), "%"))
Upvotes: 1