Typically to display axis values in different scales in ggplot2 we can use the following:
p + scale_y_continuous(labels = scales::unit_format(unit = "M", scale = 1e-6))
How can the same be done for geom_text labels? With commas we would do the following
p + geom_text(aes(y = y_val, x = x_val, label = scales::comma(value)))
Is there a function analogous to comma for millions (ex: 100M)?
Perhaps you could use paste
within geom_text
in a similar way to accomplish what you are asking?
library(ggplot2)
library(dplyr)
example_data <- tibble(
foo = c(1000000, 2000000, 3000000, 4000000, 5500000),
bar = c(1000000, 2000000, 3000000, 4000000, 5500000)
)
ggplot(example_data, aes(x = foo, y = bar)) +
geom_point() +
geom_text(aes(label = paste(round(foo / 1e6, 1), "M")))
Created on 2021-01-29 by the reprex package (v0.3.0)