rgeom-col

R geom_col and geom_text label using dollar signs and commas


I am working with the following table:

structure(list(Entity = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c(" ", 
"FL", "MA", "WV"), class = "factor"), Area = c("Broward", "Broward", 
"Broward", "Broward", "Broward", "Broward"), `Store #` = c(1143, 
1023, 1067, 1030, 1091, 1019), Store = c("Boca Raton", "Deerfield Beach", 
"Delray Beach", "Fort Lauderdale", "Tamarac", "Boynton Beach"
), `Order Count` = c(18, 25, 26, 60, 34, 29), `Net Sales` = c(2420, 
3073, 3157, 6571, 3078, 2537), `Discount %` = c(0.202, 0.257, 
0.304, 0.242, 0.216, 0.22), AOT = c(134, 123, 121, 110, 91, 87
), ` ...9` = c(" ", " ", " ", " ", " ", " "), ` ...10` = c(" ", 
" ", " ", " ", " ", " ")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

I've created the following visualization:

example_table %>%
  group_by(Entity) %>%
  summarize(Avg_AOT = mean(AOT)) %>%
  ggplot(aes(x = Entity, y = Avg_AOT, fill = Entity)) +
  geom_col() +
  labs(title = "Title", subtitle = "Subtitle") +
  theme(plot.title = element_text(face = "bold")) +
  scale_fill_manual(values=met.brewer("Hiroshige", 6)) +
  geom_text(aes(label = round(Avg_AOT, 1)), position = position_dodge(0.6), vjust = -0.3) +
  scale_y_continuous(labels = label_dollar())
  

enter image description here

I've managed to make the y-axis label with the dollar sign symbol using scale_y_continuous(labels = label_dollar())

But how can I make it so the label above the bar chart also uses a dollar sign and a comma? (The comma doesn't apply in this case but it would have to apply in other cases, so I'm looking for a flexible solution with that, also.)


Solution

  • Just wrap the label aesthetic in scales::dollar

    example_table %>%
      group_by(Entity) %>%
      summarize(Avg_AOT = mean(AOT)) %>%
      ggplot(aes(x = Entity, y = Avg_AOT, fill = Entity)) +
      geom_col() +
      labs(title = "Title", subtitle = "Subtitle") +
      theme(plot.title = element_text(face = "bold")) +
      scale_fill_manual(values=MetBrewer::met.brewer("Hiroshige", 6)) +
      geom_text(aes(label = scales::dollar(round(Avg_AOT, 1))), 
                position = position_dodge(0.6), vjust = -0.3, size = 8) +
      scale_y_continuous(labels = scales::dollar_format())
    

    enter image description here