Skip to content

Instantly share code, notes, and snippets.

@JoFrhwld
Created March 31, 2012 17:32
Show Gist options
  • Save JoFrhwld/2266961 to your computer and use it in GitHub Desktop.
Save JoFrhwld/2266961 to your computer and use it in GitHub Desktop.
ggplot2 reverse log coordinate transform
## 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