library(scales) squish_trans <- function(from, to, factor) { trans <- function(x) { if (any(is.na(x))) return(x) # get indices for the relevant regions isq <- x > from & x < to ito <- x >= to # apply transformation x[isq] <- from + (x[isq] - from)/factor x[ito] <- from + (to - from)/factor + (x[ito] - to) return(x) } inv <- function(x) { if (any(is.na(x))) return(x) # get indices for the relevant regions isq <- x > from & x < from + (to - from)/factor ito <- x >= from + (to - from)/factor # apply transformation x[isq] <- from + (x[isq] - from) * factor x[ito] <- to + (x[ito] - (from + (to - from)/factor)) return(x) } # return the transformation return(trans_new("squished", trans, inv)) } The first line in trans() and inv() handles the case when the transformation is called with x = c(NA, NA). (It seems that this did not happen with the version of ggplot2 when I originally wrote this question. Unfortunately, I don't know with which version this startet.)




