ggplot sec_axis不适用于在3.1版中将轴提升为幂的转换

时间:2018-11-06 06:25:10

标签: r ggplot2

我正在使用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  

2 个答案:

答案 0 :(得分:6)

如评论中所述,当前是.SetBasePath(context.FunctionAppDirectory) 版本3.1.0的错误。

目前,针对您的情况的一种简单的解决方法是,仅预定义所需的中断位置,这些位置将在未转换的空间中发生,然后执行所需的转换。必须使用ggplot2参数才能正确定位。

尝试以下代码:

breaks

哪个给出了这个图:

enter image description here

答案 1 :(得分:0)

此错误已修复。在最新版本的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