I'm trying to add percentage labels in a stacked bar chart. What can I add to my geom_bar to show the percentage labels inside stacked bars?
This is my data:
myresults=data.frame(
manipulation=rep(c(-20,-10,0,10,20,-20,-10,0,10,20,-20,-10,0,10,20)),
variable=rep(c("a","a","a","a","a","f","f","f","f","f","l","l","l","l","l")),
value=c(73,83,76,75,78,261,301,344,451,599,866,816,780,674,523))
This is my bar chart, without percentage labels.
I have little knowledge in this. I googled "gglot stacked bar percentage label" and found that adding percentage labels could be done with "+ geom_text(stat="count")".
But when I added + geom_text(stat="count") to my ggplot geom_bar, R said "Error: stat_count() must not be used with a y aesthetic." I tried to figure out what a y aesthetic is, but it hasn't been very successful.
This is what I did:
mydata <- ggplot(myresults, aes(x=manipulation, y=value, fill=variable))
mydata + geom_bar(stat="identity", position="fill", colour="black") + scale_fill_grey() + scale_y_continuous(labels=scales::percent) + theme_bw(base_family="Cambria") + labs(x="Manipulation", y=NULL, fill="Result") + theme(legend.direction="vertical", legend.position="right")