Barplot Data Labels

This is silly, but I can't for the life of me figure out how to add data labels to this simple plot. Nothing that I've tried in geom_text works, hoping someone can help out. Thanks.

ggplot(diamonds)+
geom_bar(aes(cut, y = ..prop.., group = 1))+
geom_text(???)

Roman - what do you mean by add data labels? Here is what you get without geom_text, but what specifically do you want to add?

library(tidyverse)
ggplot(diamonds) + geom_bar(aes(cut, y = ..prop.., group = 1))

Hey John - I just mean the actual labels on top of the bars that show the corresponding values on the y-axis.

I see now - okay, there might be a better way, but I just created a new data.frame diamonds_prop and used geom_text directly with this.

library(tidyverse)
(diamonds_prop <- diamonds %>% 
group_by(cut) %>% 
summarise(n = n()) %>% 
mutate(prop = n/sum(n)))
#> # A tibble: 5 x 3
#>   cut           n   prop
#>   <ord>     <int>  <dbl>
#> 1 Fair       1610 0.0298
#> 2 Good       4906 0.0910
#> 3 Very Good 12082 0.224 
#> 4 Premium   13791 0.256 
#> 5 Ideal     21551 0.400

ggplot(diamonds_prop) + geom_col(aes(cut, y = prop)) + geom_text(aes(x = cut, 
  y = prop + 0.05, label = round(prop, 2)))

Note - I added a 0.05 to y which can be adjusted to your liking.

1 Like

jrlewi's approach (creating/calculating a data frame, and using geom_col), is the the only one i know of.

To get a better understanding about ggplot2: Does anybody know a soloution to this problem without calculating the number of cases in each group from the values in the data? i. e., using geom_bar (values in the data) instead of geom_col (number of cases in each group)

I'm a little unclear on what you're asking here: do you want the count for the labels?

Sorry, mara, for not being clear enough. jrlewi's approach is working perfectly fine, and indeed, that's how I was doing it myself until now. What I am intrested in, is whether there is another - more convenient (less code) - way of doing it, by letting ggplot2 do the calculations for the proportions (and not doing it manually or with other functions).

You helped by giving me some time to think about it again, and try to figure out, how it's done with counts. I found an answer by hadley https://github.com/tidyverse/ggplot2/issues/1531 and adapted it to proportions.
This solved my problem.

library(ggplot2)

ggplot(diamonds, aes(factor(cut), y = ..prop.., group = 1)) +
  geom_bar() +
  geom_text(stat = "count", 
            aes(label = round(..prop.., 2), y = ..prop.. + 0.02))  

Btw: In my previous attempts to solve this problem, I forgot to change the stat argument in geom_text to "count" and therefore got the error "Error in FUN(X[[i]], ...) : object 'prop' not found"
I forgot that the default value for stats is "identity".

2 Likes