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

Time:03-18

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)

CodePudding user response:

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

  • Related