public
Created

ggplot2 reverse log coordinate transform

  • Download Gist
revlog.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
## 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")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.