Skip to content

Instantly share code, notes, and snippets.

@randrescastaneda
Last active August 27, 2020 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randrescastaneda/df4a80e9007b945a3ce5275a269bd18b to your computer and use it in GitHub Desktop.
Save randrescastaneda/df4a80e9007b945a3ce5275a269bd18b to your computer and use it in GitHub Desktop.
label large units in charts, source: https://stackoverflow.com/a/52602625/11472481
# labels for charts
addUnits <- function(n, d = 0) {
labels <- ifelse(n < 1000, n, # less than thousands
ifelse(n < 1e6, paste0(round(n/1e3, d), 'k'), # in thousands
ifelse(n < 1e9, paste0(round(n/1e6, d), 'M'), # in millions
ifelse(n < 1e12, paste0(round(n/1e9, d), 'B'), # in billions
'too big!')
)
)
)
return(labels)
}
# you use it here
ggplot(df,
aes(x = x, y = y)) +
geom_list() +
scale_y_continuous(labels = addUnits)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment