Skip to content

Instantly share code, notes, and snippets.

@samclifford
Created November 20, 2020 09:53
Show Gist options
  • Save samclifford/58ac043f800623f94377df6d4ebde5f2 to your computer and use it in GitHub Desktop.
Save samclifford/58ac043f800623f94377df6d4ebde5f2 to your computer and use it in GitHub Desktop.
Trim trailing zeros from percentages
trim_percent <- function(x, ...){
require(magrittr)
require(scales)
require(stringr)
scales::percent(x, ...) %>%
stringr::str_replace(string = ., pattern = "0+\\%", replacement = "\\%") %>%
stringr::str_replace(string = ., pattern = "\\.\\%", replacement = "\\%")
}
@samclifford
Copy link
Author

mtcars %>%
    group_by(cyl) %>%
    mutate(mpg = mpg - min(mpg),
           mpg = mpg + 0.001,
           mpg = mpg/max(mpg)) %>%
    ggplot(data = ., aes(x = wt, y = mpg/100)) +
    geom_point() +
    scale_y_continuous(labels = trim_percent, trans = "log10")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment