Histogram with “negative” logarithmic scale in R

前端 未结 4 2004
名媛妹妹
名媛妹妹 2020-12-08 12:40

I have a dataset with some outliers, such as the following

x <- rnorm(1000,0,20)
x <- c(x, 500, -500)

If we plot this on a linear x a

4条回答
  •  难免孤独
    2020-12-08 12:48

    I am not sure I understand your goal, but when you want a log-like transformation yet have zeroes or negative values, the inverse hyperbolic sine transformation asinh() is often a good option. It is log-like for large values and is defined for all real values. See Rob Hyndman's blog and this question on stats.stackexchange.com for discussion, details, and other options.

    If this is an acceptable approach, you can create a custom scale for ggplot. The code below demonstrates how to create and use a custom scale (with custom breaks), along with a visualization of the asinh() transformation.

    library(ggplot2)
    library(scales)
    
    limits <- 100
    step <- 0.005
    demo <- data.frame(x=seq(from=-1*limits,to=limits,by=step))
    
    asinh_trans <- function(){
      trans_new(name = 'asinh', transform = function(x) asinh(x), 
                inverse = function(x) sinh(x))
    }
    
    ggplot(demo,aes(x,x))+geom_point(size=2)+
         scale_y_continuous(trans = 'asinh',breaks=c(-100,-50,-10,-1,0,1,10,50,100))+
         theme_bw()
    

    enter image description here

    ggplot(demo,aes(x,x))+geom_point(size=2)+
         scale_x_continuous(trans = 'asinh',breaks=c(0,1,10,50,100))+
         scale_y_log10(breaks=c(0,1,10,50,100))+ # zero won't plot
         xlab("asinh() scale")+ylab("log10 scale")+
         theme_bw()
    

    enter image description here

提交回复
热议问题