Search code examples
rggplot2textlabelgeom

How do I change the geom_text to currency when it is labeling a bar graph?


I have a bar graph and I have labels at the top of each bar to show the amount.

How do I change this to be in currency format?

Here is an example:

df <- tribble(~county, ~amount,
              "A", 200000, 
              "B", 1000000, 
              "C", 500000, 
              "D", 250000)

df %>%
  ggplot(aes(x = county, y = amount)) +
  geom_col() +
  scale_y_continuous(labels = label_dollar()) +
  geom_text(aes(label = amount), vjust = -.5)

Solution

  • Use label_dollar()(..):

    df <- tribble(~county, ~amount,
                  "A", 200000, 
                  "B", 1000000, 
                  "C", 500000, 
                  "D", 250000)
    
    df %>%
      ggplot(aes(x = county, y = amount)) +
      geom_col() +
      scale_y_continuous(labels = label_dollar()) +
      geom_text(aes(label = label_dollar()(amount)), vjust = -.5)
    

    ggplot2 barplot