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

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

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

问题

当使用 ggbreak 分割 x 轴时,图表显示正确。但是,如果我指定 y 轴的刻度,y 轴不会显示为没有指定时的样子。我该如何解决这个问题?

  1. library(ggplot2)
  2. library(ggbreak)
  3. DF <- data.frame(x = 1:40,
  4. y = c(rnorm(n = 10,
  5. mean = 1000,
  6. sd = 250),
  7. rnorm(n = 10,
  8. mean = 200,
  9. sd = 10),
  10. rnorm(n = 10,
  11. mean = 1000,
  12. sd = 250),
  13. rnorm(n = 10,
  14. mean = 200,
  15. sd = 10)))
  16. # 无对数轴
  17. ggplot(data = DF,
  18. aes(x = x,
  19. y = y)) +
  20. geom_point() +
  21. scale_x_break(breaks = c(10, 20),
  22. scales = "free")

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

  1. # 有对数轴
  2. ggplot(data = DF,
  3. aes(x = x,
  4. y = y)) +
  5. geom_point() +
  6. scale_y_log10(name = "y 轴",
  7. limits = c(0.1, 10000),
  8. breaks = c(0.1, 1, 10, 100, 1000, 10000),
  9. labels = c(expression(10^-1),
  10. expression(10^0),
  11. expression(10^1),
  12. expression(10^2),
  13. expression(10^3),
  14. expression(10^4))) +
  15. scale_x_break(breaks = c(10, 20),
  16. 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?

  1. library(ggplot2)
  2. library(ggbreak)
  3. DF &lt;- data.frame(x = 1:40,
  4. y = c(rnorm(n = 10,
  5. mean = 1000,
  6. sd = 250),
  7. rnorm(n = 10,
  8. mean = 200,
  9. sd = 10),
  10. rnorm(n = 10,
  11. mean = 1000,
  12. sd = 250),
  13. rnorm(n = 10,
  14. mean = 200,
  15. sd = 10)))
  16. # Without log axis
  17. ggplot(data = DF,
  18. aes(x = x,
  19. y = y)) +
  20. geom_point() +
  21. scale_x_break(breaks = c(10, 20),
  22. scales = &quot;free&quot;)

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

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

  1. # With log axis
  2. ggplot(data = DF,
  3. aes(x = x,
  4. y = y)) +
  5. geom_point() +
  6. scale_y_log10(name = &quot;y-axis&quot;,
  7. limits = c(0.1, 10000),
  8. breaks = c(0.1, 1, 10, 100, 1000, 10000),
  9. labels = c(expression(10^-1),
  10. expression(10^0),
  11. expression(10^1),
  12. expression(10^2),
  13. expression(10^3),
  14. expression(10^4))) +
  15. scale_x_break(breaks = c(10, 20),
  16. scales = &quot;free&quot;)

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

答案1

得分: 1

Here is the translated code snippet:

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

  1. library(ggplot2)
  2. library(ggbreak)
  3. set.seed(123)
  4. ggplot(
  5. data = DF,
  6. aes(
  7. x = x,
  8. y = y
  9. )
  10. ) +
  11. geom_point() +
  12. scale_y_log10(
  13. name = "y轴",
  14. limits = c(0.1, 10000),
  15. breaks = c(0.1, 1, 10, 100, 1000, 10000),
  16. labels = c(
  17. expression(10^-1),
  18. expression(10^0),
  19. expression(10^1),
  20. expression(10^2),
  21. expression(10^3),
  22. expression(10^4)
  23. ),
  24. sec.axis = dup_axis(breaks = NULL, labels = NULL, name = NULL)
  25. ) +
  26. 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:

  1. library(ggplot2)
  2. library(ggbreak)
  3. set.seed(123)
  4. ggplot(
  5. data = DF,
  6. aes(
  7. x = x,
  8. y = y
  9. )
  10. ) +
  11. geom_point() +
  12. scale_y_log10(
  13. name = &quot;y-axis&quot;,
  14. limits = c(0.1, 10000),
  15. breaks = c(0.1, 1, 10, 100, 1000, 10000),
  16. labels = c(
  17. expression(10^-1),
  18. expression(10^0),
  19. expression(10^1),
  20. expression(10^2),
  21. expression(10^3),
  22. expression(10^4)
  23. ),
  24. sec.axis = dup_axis(breaks = NULL, labels = NULL, name = NULL)
  25. ) +
  26. 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:

确定