代码之家  ›  专栏  ›  技术社区  ›  Reactormonk

在一个轴上应用两个变换

  •  3
  • Reactormonk  · 技术社区  · 13 年前

    我发现 coord_trans ,但我想申请 log10 reverse 到我的x轴。我试着应用两个变换

    ggplot(table) + aes(color=Vowel, x=F1, y=F2) + geom_point() + coord_trans(x="log10", y="log10") + coord_trans(x="reverse", y="reverse")
    

    但只应用了第一个。所以我试着把它们联系起来

    ggplot(table) + aes(color=Vowel, x=F2, y=F1) + geom_point() + coord_trans(x=c("log10", "reverse"), y=c("log10", "reverse"))
    

    这给了我一个明显的错误。

    'c("log10_trans", "reverse_trans")' is not a function, character or symbol
    

    我该如何将它们锁住?

    3 回复  |  直到 13 年前
        1
  •  4
  •   Richie Cotton Joris Meys    13 年前

    可以使用定义新的转换 trans_new

    library(scales)
    log10_rev_trans <- trans_new(
      "log10_rev",
      function(x) log10(rev(x)),
      function(x) rev(10 ^ (x)),
      log_breaks(10),
      domain = c(1e-100, Inf)
    )
    
    p <- ggplot(mtcars, aes(wt, mpg)) +
       geom_point()   
    
    p + coord_trans(y = log10_rev_trans)
    
        2
  •  1
  •   Maiasaura    13 年前

    一种快速而简单的方法是将其中一种转换直接应用于数据,并将另一种转换与绘图函数一起使用。

    例如。

    ggplot(iris, aes(log10(Sepal.Length), log10(Sepal.Width), colour = Species)) + 
    geom_point() + coord_trans(x="reverse", y="reverse")
    

    注意:反向变换不适用于虹膜数据,但你已经明白了。

        3
  •  0
  •   steveo'america    8 年前

    我在这里闲逛,寻找一个“天平的合成”函数。我想人们也许可以写这样一件事:

    # 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)