I have the following ggplot code.
mpg %>%
ggplot2::ggplot(ggplot2::aes(x = as.character(class))) +
ggplot2::geom_bar(fill = '#00798c') +
ggplot2::geom_text(stat = "count", ggplot2::aes(label = after_stat(count)), position = ggplot2::position_dodge(width = 0.8), vjust = -0.3)
How can I make the geom_text() annotations bold and also have a thousands comma separator?
I have been looking at the documentation and not really sure how I can do this?
I tried using scales::comma(count)
nested inside the after_stat()
but I got an error. No idea how to bold annotated text after reading the documentation.
Not sure what's your issue. scales::comma
works fine for me. To get bold labels add fontface="bold"
:
Note: As the values are too small I multiplied by 1000 so that we get a thousands separator.
library(ggplot2)
ggplot(mpg, aes(x = class)) +
geom_bar(fill = '#00798c') +
geom_text(stat = "count", aes(label = after_stat(scales::comma(1000 * count))),
position = position_dodge(width = 0.8),
vjust = -.3, fontface = "bold")
scales::comma()
is now superseded by scales::label_number()
.
The syntax is a bit different. There is no x
argument as before. The call to label_number()
creates a function. Then the after_stat
is fed to this function.
Using the example by stefan, it would be:
library(ggplot2)
ggplot(mpg, aes(x = class)) +
geom_bar(fill = '#00798c') +
geom_text(stat = "count",
aes(label = scales::label_number(scale = 1000, big.mark = ",")(after_stat(count))),
position = position_dodge(width = 0.8),
vjust = -.3, fontface = "bold")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With