如何使用ggbreak在使用ggplot创建的带有分裂轴的图上保持轴配置?

huangapple go评论66阅读模式
英文:

How to keep the axis configuration on a plot made with ggplot with split axes using ggbreak?

问题

当使用 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)))
    
    # 无对数轴
    
    ggplot(data = DF,
           aes(x = x,
               y = y)) +
      geom_point() +
      scale_x_break(breaks = c(10, 20),
                    scales = "free")

注意,这里 y 轴被打印两次,不符合预期。

    # 有对数轴
    
    ggplot(data = DF,
           aes(x = x,
               y = y)) +
      geom_point() +
      scale_y_log10(name = "y 轴",
                    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")
英文:

When I split the x-axis using ggbreak, the plot is displayed correctly. However, if I specify the scale of the y-axis, the y-axis is not displayed as it would be without the specification. How can I solve this?

library(ggplot2)
library(ggbreak)

DF &lt;- 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 = &quot;free&quot;)

如何使用ggbreak在使用ggplot创建的带有分裂轴的图上保持轴配置?

Note that here the y-axis is printed twice, unlike as desired.

# With log axis

ggplot(data = DF,
       aes(x = x,
           y = y)) +
  geom_point() +
  scale_y_log10(name = &quot;y-axis&quot;,
                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 = &quot;free&quot;)

如何使用ggbreak在使用ggplot创建的带有分裂轴的图上保持轴配置?

答案1

得分: 1

Here is the translated code snippet:

尽管这看起来像是一个错误,但一个解决方法是明确指定一个重复的轴,然后删除 breakslabelsname

library(ggplot2)
library(ggbreak)

set.seed(123)

ggplot(
  data = DF,
  aes(
    x = x,
    y = y
  )
) +
  geom_point() +
  scale_y_log10(
    name = "y轴",
    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")

如何使用ggbreak在使用ggplot创建的带有分裂轴的图上保持轴配置?

英文:

While this looks like a bug to me, a workaround would be to explicitly specify a duplicated axis for which you remove the breaks, labels and the name:

library(ggplot2)
library(ggbreak)

set.seed(123)

ggplot(
  data = DF,
  aes(
    x = x,
    y = y
  )
) +
  geom_point() +
  scale_y_log10(
    name = &quot;y-axis&quot;,
    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 = &quot;free&quot;)

如何使用ggbreak在使用ggplot创建的带有分裂轴的图上保持轴配置?

huangapple
  • 本文由 发表于 2023年4月17日 22:22:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定