I would like to add percentage labels to a percentage barplot
I found solutions with position="fill"
(Add percentage labels to a stacked barplot) and also here (How to draw stacked bars in ggplot2 that show percentages based on group?), however, I would like to keep relative frequencies for every group.
Here is an example plot:
# library
library(ggplot2)
# data
df <- data.frame(group=c("A","A","A","A","B","B","B","C","C"),
anon=c("yes","no","no","no","yes","yes","no","no","no"))
# percentage barplot
ggplot(df, aes(group),fill=anon) +
geom_bar(aes(y = (..count..)/sum(..count..),fill=anon)) +
scale_y_continuous(labels=scales::percent) +
ylab("relative frequencies")
Created on 2020-04-19 by the reprex package (v0.3.0)
Now I would like to add percentage labels to each red and green portion of each bar,so that I get "relative-relative" (e.g. 25% for "yes" for group A) values.
How can this be done? Do I have to change my df for this or is this somehow possible within the ggplot
function