Created
March 31, 2012 17:32
-
-
Save JoFrhwld/2266961 to your computer and use it in GitHub Desktop.
ggplot2 reverse log coordinate transform
This file contains 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
## ggplot2 and mgcv for the plot | |
library(ggplot2) | |
library(mgcv) | |
## scales packages to define revlog | |
library(scales) | |
revlog_trans <- function(base = exp(1)){ | |
## Define the desired transformation. | |
trans <- function(x){ | |
-log(x, base) | |
} | |
## Define the reverse of the desired transformation | |
inv <- function(x){ | |
base^(-x) | |
} | |
## Creates the transformation | |
trans_new(paste("revlog-", base, sep = ""), | |
trans, ## The transformation function (can be defined using anonymous functions) | |
inv, ## The reverse of the transformation | |
log_breaks(base = base), ## default way to define the scale breaks | |
domain = c(1e-100, Inf) ## The domain over which the transformation is valued | |
) | |
} | |
ggplot(philly.bw.count, aes(month.date, ndays/freq, color = race)) + | |
geom_point()+ | |
stat_smooth(method = gam, formula = y ~ s(x, bs = "cs"))+ | |
scale_y_continuous(name = "1 murder every X days", | |
breaks = c(0.5,1,2,7,14,21,31), | |
trans = revlog_trans(base = 2))+ ## use the transformation we just created | |
xlab("Date (by month)")+ | |
expand_limits(y = 0.5)+ | |
theme_bw()+ | |
scale_color_brewer(palette = "Set1") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment