Search code examples
rggplot2bar-chartfrequency

How can I add labels to proportion barplot showing the amount (not the proportion)?


I created a proportion barplot:

df <- data.frame(speech = c("figurative", "literal", "figurative", "literal"),
             meaning = c("kill", "seek", "seek", "kill"), 
             amount = c(47, 2260, 588, 5639)) 
ggplot(df, aes(x=speech,y=amount, fill=meaning, group = meaning)) + geom_bar(stat = "identity", position="fill")

How can I add labels showing the amount (not the proportion)?


Solution

  • Is this what you mean? Adding labels based on the values for amount.

    df <- data.frame(speech = c("figurative", "literal", "figurative", "literal"),
                     meaning = c("kill", "seek", "seek", "kill"), 
                     amount = c(47, 2260, 588, 5639)) 
    
    ggplot(df, aes(x=speech,y=amount, fill=meaning, group = meaning)) + 
      geom_bar(stat = "identity", position="fill") +
      geom_text(aes(label = amount), position = position_fill(vjust = 0.5))
    

    enter image description here