How to add text to a bar chart in ggplot

I realize there are already several examples of this question, but none of them gave me an answer. So, I have this (already melted) dataframe:

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 portion of the dataframe and I have many similar dataframes. I was doing stacked histograms like this:

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 histogram:

Received Bar Chart

Now I would like to add percentages to the blue portions of the histogram. The red part is the total number of followers, and the blue part is the amount that went through it. Therefore, in my example, these percentages should become

df$Value[2]*100/df$Value[1]
df$Value[4]*100/df$Value[3]

      

Since I have a lot of these data frames, I really don't want to do it manually. I've seen examples on stackoverflow where text and percentage calculations were implemented in ggplot

and where percentages were calculated prior to use ggplot

, but I'm afraid my data preparation is not that good at this, easy.

Things I've tried:

#ddply, to add a column with percentages:
ddply(df2, .(Var2), transform, percent=value*100/value)

      

The problem here, of course, is my percentage calculation. How do I make ddply to select and multiply the correct values? Will 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 do this ggplot

without sketching out the percentages. I guess I should separate the values Percentage

from Var1

, but I couldn't do it.

Any help would be greatly appreciated!

+3


source to share


1 answer


library(dplyr)
df <- df %>%
  group_by(Var2) %>%
  mutate(Max = max(Value), Min = min(Value), Per = round(Min*100/Max, 2))%>%
  arrange(Var2)

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"))+
  annotate("text", x = 1:length(unique(df$Var2)), y=rep(min((unique(df$Max)-unique(df$Min))),2), label = unique(df$Per))

      



0


source







All Articles