Last active
December 10, 2021 16:46
-
-
Save noamross/9127f3c4c6015ddc63ebd8508f9f71ca to your computer and use it in GitHub Desktop.
Custom log-shift scale in ggplot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
dat <- data.frame(a = letters[1:10], b = exp(rnorm(10))) | |
ggplot(dat, aes(x = a, y = b)) + geom_col() + scale_y_log10() # Basic log scale transform | |
trans = scales::trans_new("logshift", \(x) log10(x*10), \(x) (10^x)/10) # Custom transformation | |
ggplot(dat, aes(x = a, y = b)) + geom_col() + scale_y_continuous(trans = trans) # Use custom transformation | |
ggplot(dat, aes(x = a, y = b)) + geom_col() + scale_y_continuous(trans = trans, breaks = c(0.25, 0.5, 1, 2, 4, 8)) # Add breaks | |
ggplot(dat, aes(x = a, y = b)) + | |
geom_col() + | |
scale_y_continuous( | |
trans = trans, | |
breaks = c(.1, .1, 1, 10, 100), # Add breaks | |
limits = c(.1, 100), | |
labels = signif, #See ?scales::comma for other labeling functions that take the `breaks` values and format them | |
expand = c(0,0)) + # Don't put padding beyond the limits of the scale | |
theme(panel.grid.minor = element_blank()) # Remove some of the grid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment