I realize there already are multiple instances of this question, but none of them really provided the answer for me. So I've got this (already melted) data frame:
df <-data.frame(
Var1 = c("Inschrijvingen", "BSA", "Inschrijvingen", "BSA"),
Var2 = c("Totaal","Totaal", "OD_en_MD", "OD_en_MD"),
Value = c(262, 190, 81, 69)
)
Note that this is only a small part of the data frame and that I've got lots of similar data frames. I made stacked bar charts the following way:
ggplot(df, aes(Var2, as.numeric(as.character(value)), fill=Var1))+
geom_bar(position="identity", stat="identity") +
scale_alpha_manual(values=c(.6,.8)) +
ggtitle(names(df)) + labs(x="", y="Aantal") +
scale_colour_brewer(palette = "Set2") +
scale_fill_discrete("BSA Resultaten", labels=c("BSA niet behaald", "BSA behaald"))
Which gives me the following bar chart:

Now I would like to add percentages to the blue parts of the bar chart. The red part is the total amount of subscribers and the blue part is the amount that made it through. So in my example these percentages should become
df$Value[2]*100/df$Value[1]
df$Value[4]*100/df$Value[3]
Since I've got loads of these data frames, I don't really want to do it manually. I've seen examples on stackoverflow where the text and percentage calculations have been both implemented in ggplot and where the percentages were calculated before using ggplot, but I'm afraid my data preparation isn't that good to do this that easily.
Things I've tried:
#ddply, to add a column with percentages:
ddply(df2, .(Var2), transform, percent=value*100/value)
The problem here is, of course, my percent-calculation. How do I make ddply select and multiply the right values? Would this be the right way in the first place?
#Calculating percentages before melting the data frame, which gives me the (molten) data frame:
df2 <- data.frame(
Var1 =c("Inschrijvingen", "BSA","Percentage","Inschrijvingen",
"BSA","Percentage"),
Var2 =c("Totaal","Totaal","Totaal","OD_en_MD","OD_en_MD","OD_en_MD"),
Value = c(262,190,72.5,81,69,85.2)
)
The problem here is that I don't know how to get this into ggplot without the percentages being plotted. I guess I should separate the values Percentage from Var1, but I haven't been able to manage that.
Any help would be greatly appreciated!