Skip to content

Instantly share code, notes, and snippets.

@danlewer
Last active April 21, 2021 09:57
Show Gist options
  • Save danlewer/8ec82abcbeb9e5b1ad08dab02a0091a2 to your computer and use it in GitHub Desktop.
Save danlewer/8ec82abcbeb9e5b1ad08dab02a0091a2 to your computer and use it in GitHub Desktop.
# enter a maximum value in your data and generate tick-points for a plot axis
yax <- function(x, tickabove = F, ntick = 5) {
l <- c(c(1, 2, 4, 5, 25) %o% 10^(0:8))
d <- l[which.min(abs(x/ntick - l))]
d <- 0:(ntick+1) * d
i <- findInterval(x, d)
if (tickabove) {i <- i + 1}
d[seq_len(i)]
}
# example
x <- lapply(rep(100, 4), rnorm, mean = 5, sd = 1.5)
y <- lapply(rep(100, 4), rnorm, mean = 4, sd = 1)
y <- mapply(`^`, x = y, y = 1:4, SIMPLIFY = F)
yaxes <- lapply(y, function(x) yax(max(x)))
par(mfrow = c(2, 2))
for(i in 1:4) {
plot(x[[i]], y[[i]], axes = F, xlab = 'x', ylab = 'y', ylim = c(0, max(y[[i]])))
axis(1)
axis(2, yaxes[[i]], las = 2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment