Apply two transformations on one axis

后端 未结 3 925
一生所求
一生所求 2021-01-21 02:16

I have found coord_trans, but I\'d like to apply log10 and reverse to my x-axis. I tried applying two transformation

ggplo         


        
3条回答
  •  独厮守ぢ
    2021-01-21 02:56

    I wandered in here looking for a 'composition of scales' function. I think one might be able to write such a thing as follows:

    # compose transforms a and b, applying b first, then a:
    `%::%`  <- function(atrans,btrans) {
      mytran <- scales::trans_new(name      = paste(btrans$name,'then',atrans$name),
                                  transform = function(x) { atrans$transform(btrans$transform(x)) },
                                  inverse   = function(y) { btrans$inverse(atrans$inverse(y)) },
                                  domain    = btrans$domain,  # this could use improvement...
                                  breaks    = btrans$breaks,  # not clear how this should work, tbh
                                  format    = btrans$format)
    
    }
    
    ph <- ggplot(mtcars, aes(wt, mpg)) +
      geom_point() +
      scale_y_continuous(trans=scales::reverse_trans() %::% scales::log10_trans())
    print(ph)
    

提交回复
热议问题