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

ggplot secu axis不适用于在版本3.1中将轴提升到幂次的转换

  •  3
  • divibisan  · 技术社区  · 6 年前

    ggplot2 绘制log2转换数据,但也希望将未转换的等效值显示为次轴。因为数据已经被log2转换了,所以我应该能够通过将2提高到这个幂来反转转换( ~2^. ). 这在版本3.0.0中运行得非常好,但升级到3.1.0后,此代码不会产生任何错误,但会生成一个没有任何意义的次轴:

    df = data.frame(x = rnorm(100),
                    y = rnorm(100))
    
    ggplot(df, aes(x,y)) +
          geom_point() +
          scale_y_continuous(sec.axis = sec_axis(trans=(~2^.)))
    

    enter image description here

    ~. , ~. + 10 ~ . * 10 ~2^. 给我带来任何问题。


    我敢肯定,这是有关升级到3.1版,因为在根据 release notes ,出现了一个与使用 sec_axis 使用日志转换的数据,但我不知道发生了什么变化,以及为什么它的行为方式是现在这样。是否有人对此了解更多,或者除了降级到3.0之外还有其他解决方法?

    > sessionInfo()
    R version 3.5.1 (2018-07-02)
    Platform: x86_64-apple-darwin15.6.0 (64-bit)
    Running under: macOS High Sierra 10.13.6
    
    Matrix products: default
    BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
    LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
    
    locale:
    [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
    [1] ggplot2_3.1.0
    
    loaded via a namespace (and not attached):
     [1] Rcpp_0.12.19     withr_2.1.2      assertthat_0.2.0 crayon_1.3.4     dplyr_0.7.7      R6_2.3.0         grid_3.5.1      
     [8] plyr_1.8.4       gtable_0.2.0     magrittr_1.5     scales_1.0.0     pillar_1.3.0     rlang_0.3.0.1    lazyeval_0.2.1  
    [15] rstudioapi_0.7   bindrcpp_0.2.2   labeling_0.3     tools_3.5.1      glue_1.3.0       purrr_0.2.5      munsell_0.5.0   
    [22] yaml_2.2.0       compiler_3.5.1   pkgconfig_2.0.2  colorspace_1.3-2 tidyselect_0.2.5 bindr_0.1.1      tibble_1.4.2  
    
    1 回复  |  直到 6 年前
        1
  •  6
  •   Marcus Campbell Artem Sokolov    6 年前

    正如评论中提到的,这是一个带有 ggplot2

    目前,一个简单的解决方法是预先定义所需的中断位置,它们将发生在什么位置 breaks 参数对于正确定位是必要的。

    请尝试以下代码:

    library("ggplot2")
    
    set.seed(7)
    # Create some log (base 2) transformed data
    df = data.frame(x = log2(runif(100, min = 0, max = 10)),
                    y = log2(runif(100, min = 0, max = 10)))
    
    # A vector of your desired break positions in untransformed space
    sec_breaks <- c(0.4,0.5,1,2,4,8,10)
    # Transform break positions
    scaled_breaks <- log2(sec_breaks)
    
    ggplot(df, aes(x,y)) +
      geom_point() +
      scale_y_continuous(sec.axis = sec_axis(trans = (~.),
                                             breaks = scaled_breaks,
                                             labels = sprintf("%.1f", sec_breaks)))
    

    enter image description here

        2
  •  1
  •   divibisan    5 年前

    此错误已修复。在最新版本的ggplot(3.2.1)中,以下代码现在可以正确地呈现次轴:

    df = data.frame(x = rnorm(100),
                    y = rnorm(100))
    
    ggplot(df, aes(x,y)) +
          geom_point() +
          scale_y_continuous(sec.axis = sec_axis(trans=(~2^.)))
    

    enter image description here