如何使用ggbreak在带有分割轴的ggplot上保持轴配置?

s5a0g9ez  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(205)

当我使用ggbreak分割x轴时,图可以正确显示。但是,如果我指定了y轴的比例,y轴就不会像没有指定时那样显示。我该如何解决这个问题?

library(ggplot2)
library(ggbreak)

DF <- data.frame(x = 1:40,
                 y = c(rnorm(n = 10,
                             mean = 1000,
                             sd = 250),
                       rnorm(n = 10,
                             mean = 200,
                             sd = 10),
                       rnorm(n = 10,
                             mean = 1000,
                             sd = 250),
                       rnorm(n = 10,
                             mean = 200,
                             sd = 10)))

# Without log axis

ggplot(data = DF,
       aes(x = x,
           y = y)) +
  geom_point() +
  scale_x_break(breaks = c(10, 20),
                scales = "free")

注意,这里y轴被打印两次,这与所期望的不同。

# With log axis

ggplot(data = DF,
       aes(x = x,
           y = y)) +
  geom_point() +
  scale_y_log10(name = "y-axis",
                limits = c(0.1, 10000),
                breaks = c(0.1, 1, 10, 100, 1000, 10000),
                labels = c(expression(10^-1),
                           expression(10^0),
                           expression(10^1),
                           expression(10^2),
                           expression(10^3),
                           expression(10^4))) +
  scale_x_break(breaks = c(10, 20),
                scales = "free")

4sup72z8

4sup72z81#

虽然这看起来像是一个bug,但解决方法是显式指定一个重复的轴,删除breakslabelsname

library(ggplot2)
library(ggbreak)

set.seed(123)

ggplot(
  data = DF,
  aes(
    x = x,
    y = y
  )
) +
  geom_point() +
  scale_y_log10(
    name = "y-axis",
    limits = c(0.1, 10000),
    breaks = c(0.1, 1, 10, 100, 1000, 10000),
    labels = c(
      expression(10^-1),
      expression(10^0),
      expression(10^1),
      expression(10^2),
      expression(10^3),
      expression(10^4)
    ),
    sec.axis = dup_axis(breaks = NULL, labels = NULL, name = NULL)
  ) +
  scale_x_break(breaks = c(10, 20), scales = "free")

相关问题