r - ggplot sec_axis 不适用于在 3.1 版中将轴提升到幂的转换
问题描述
我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^.)))
辅助轴与其他公式一起正常工作(~.
, ~. + 10
,并且~ . * 10
所有工作都按预期工作),只是~2^.
给我带来了任何问题。
我很确定这与升级到 3.1 版有关,因为根据发行说明,有一个专门与使用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
解决方案
正如评论中提到的,这目前是ggplot2
3.1.0 版的一个错误。
目前,针对您的情况,一个简单的解决方法是预先定义您想要的中断位置,它们将出现在未转换的空间中,然后执行所需的转换。使用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)))
这给出了这个情节:
推荐阅读
- c - 在第二条对角线下打印矩阵和数字
- node.js - 可以以某种方式减少/解决这些 npm 警告吗?
- angular - 如何在 ngx-bootstrap/datepicker 中设置选定的日期,该日期是在将图标单击到常见的 html 输入时启动的?
- swift - 引用实例方法需要等效性 (SWIFT)
- text - 用视觉模式选择多个文本点?
- reactjs - 如何使用 onClick 获取 props 并在 redux 中使用组件传递?
- reactjs - React 从 useEffect 中的 Redux 商店获取状态
- python - 如何使用散景创建比例(预定义边界)彩条?
- node.js - 使用 google api 库编译打字稿时出现“错误 TS2304:找不到名称‘Long’”
- python - 使用 mysqldb 接口更新