r - 在 ggplot2 log10 轴上以十的整数幂中断
问题描述
有时使用 can(如果范围足够小)将轴转换ggplot2
为 log10会产生不漂亮的中断,以十的非整数幂。scales::trans_breaks()
是否有一种通用方法可以将这些中断设置为仅在 10^x 处发生,其中 x 都是整数,并且理想情况下是连续的(例如 10^1、10^2、10^3)?
这是我的意思的一个例子。
library(ggplot2)
# dummy data
df <- data.frame(fct = rep(c("A", "B", "C"), each = 3),
x = rep(1:3, 3),
y = 10^seq(from = -4, to = 1, length.out = 9))
p <- ggplot(df, aes(x, y)) +
geom_point() +
facet_wrap(~ fct, scales = "free_y") # faceted to try and emphasise that it's general purpose, rather than specific to a particular axis range
不想要的结果 - y 轴中断是 10 的非整数幂(例如 10^2.8)
p + scale_y_log10(
breaks = scales::trans_breaks("log10", function(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x))
)
n
我可以通过将参数调整为 来实现此特定示例的所需结果scales::trans_breaks()
,如下所示。但这不是一种通用的解决方案,无需根据具体情况进行任何调整即可应用。
p + scale_y_log10(
breaks = scales::trans_breaks("log10", function(x) 10^x, n = 1),
labels = scales::trans_format("log10", scales::math_format(10^.x))
)
应该补充一点,我不喜欢使用scales::trans_breaks()
,只是我发现它是让我最接近我所追求的功能。
任何帮助将不胜感激,谢谢!
解决方案
这是一种核心具有以下功能的方法。
breaks = function(x) {
brks <- extended_breaks(Q = c(1, 5))(log10(x))
10^(brks[brks %% 1 == 0])
}
它给出extended_breaks()
了一组狭窄的“好数字”,然后过滤掉非整数。
这为我们提供了以下示例案例:
library(ggplot2)
library(scales)
#> Warning: package 'scales' was built under R version 4.0.3
# dummy data
df <- data.frame(fct = rep(c("A", "B", "C"), each = 3),
x = rep(1:3, 3),
y = 10^seq(from = -4, to = 1, length.out = 9))
ggplot(df, aes(x, y)) +
geom_point() +
facet_wrap(~ fct, scales = "free_y") +
scale_y_continuous(
trans = "log10",
breaks = function(x) {
brks <- extended_breaks(Q = c(1, 5))(log10(x))
10^(brks[brks %% 1 == 0])
},
labels = math_format(format = log10)
)
由reprex 包(v0.3.0)于 2021-01-19 创建
我还没有在许多其他可能很困难的范围内对此进行测试,但它应该比将所需中断的数量设置为 1 更好地概括。困难的范围可能只是介于两者之间 - 但不包括 - 10 的幂。例如 11 -99 或 101-999。
推荐阅读
- opencv - Opencv plus unity(资产存储插件)
- json - Avro 架构不符合我的 json 数据?
- android - 从浏览器打开 Intent url
- react-native - 混合 JavaScript 移动开发 - Apache Cordova vs Capacitor with Ionic vs NativeScript vs React Native
- ios - SwiftUI 创建列表
- javascript - 如果通过 jquery 动态添加,我的 Html Toggle/Switch 无法正确呈现
- php - Redis 比 MySQL 慢
- r - 如何删除此 for 循环
- c - AVX2 和 SSE2 的位向量运算
- python - 如何在python中对图像的某个部分进行切片?