首页 > 解决方案 > 在 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(),只是我发现它是让我最接近我所追求的功能。

任何帮助将不胜感激,谢谢!

标签: rggplot2

解决方案


这是一种核心具有以下功能的方法。

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。


推荐阅读