Skip to content

Instantly share code, notes, and snippets.

@wjhopper
Created May 9, 2017 16:53
Show Gist options
  • Save wjhopper/e409d08dd8b32673f3cceb0ead74f151 to your computer and use it in GitHub Desktop.
Save wjhopper/e409d08dd8b32673f3cceb0ead74f151 to your computer and use it in GitHub Desktop.
Log-Scale Axis with ggplot2
library(ggplot2)
library(scales)
library(gridExtra)
set.seed(20)
n <- 10
y <- rexp(n, 2)
dat <- data.frame(
xval = rep(c("A","B"), n/2),
yval = y,
log_yval = log(y)
)
sp <- ggplot(dat, aes(xval, yval)) +
geom_point() +
ggtitle("Raw Data")
sp_log_manual <- ggplot(dat, aes(xval, log_yval)) +
geom_point() +
scale_y_continuous(breaks = seq(-3,1),
labels = round(exp(seq(-3,1)),2)
) +
ggtitle("scale_y_continuous(labels = round(exp(seq(-3,1)),2))")
sp_log_manual
sp_log_trans <- ggplot(dat, aes(xval, yval)) +
geom_point() +
scale_y_continuous(trans=log_trans(),
breaks = round(exp(seq(-3,1)),2)) +
ggtitle("scale_y_continuous(trans=log_trans())")
sp_coord_trans <- ggplot(dat, aes(xval, yval)) +
geom_point() +
coord_trans(y="log") +
ggtitle("coord_trans(ytrans='log')")
grid.arrange(sp, sp_log_manual, sp_log_trans, sp_coord_trans, ncol=2, nrow=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment