Search code examples
rggplot2labeldigitsgeom-text

using geom_text to create data labels for bar chart


I am practicing with the "diamonds" dataset in ggplot 2. I am trying to make a bar chart plot, to demonstrate the average price per color category:

This is the code I wrote:

ggplot(data=diamonds)+
  geom_bar(mapping=aes(x=color,y=price),
           fun="mean",stat="summary",fill="yellow")+
  geom_text(
    aes(label=scales::comma(
      round(mean(price),digits=3)
      ),
        x=color,y=price),
    stat="summary",size=3,
    vjust=-1)

The visualization being generated by this code is shown as follow: enter image description here The problems that I have encountered are regarding the data labels:

  1. the data labels in each bar are the same. this is obviously not correct;
  2. why wouldn't my function "digitals=3" work? Why could not change how many digits to show in the data labels?

I have searched online for possible answers, but could not find a good solution


Solution

  • The first issue is that using mean(price) you compute the overall mean of price. To get the mean per color which is computed by stat_summary and which you display as bars you have to use label = after_stat(y). Here y is the variable which contains the values computed by stat_summary under the hood. This value is automatically mapped on the y aesthetic. However, to use it for the label we have to wrap in after_stat to access the computed value.

    Second, the issue is that you wrapped round in scales::comma which will round the already rounded value a second time with a default accuracy=1. To fix that I would suggest to use the accuracy argument of scales::comma, e.g. set it to .001 to round to three decimals.

    library(ggplot2)
    
    ggplot(data = diamonds, aes(x = color, y = price)) +
      geom_bar(
        fun = "mean", stat = "summary", fill = "yellow"
      ) +
      geom_text(
        aes(
          label = after_stat(scales::comma(y, accuracy = .001))
        ),
        fun = "mean", 
        stat = "summary", 
        size = 3,
        vjust = -1
      )