控制合并多个图表时的Y轴标题位置,使用patchwork包。

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

control y-axis title positions when combining several plots with patchwork package

问题

我想能够控制每个图中关于y轴的标题位置相对于y轴本身的位置,这样当我将不同长度的抗生素名称输入到顶部图中时,底部图中的y轴标题与y轴的距离保持一致,我不必在更改顶部图中的抗生素名称时手动更改底部图中的位置。 有人能帮忙吗?

第一张图显示默认值,没有任何调整。

第二张图显示y轴标题应该距离y轴标签的期望距离。

第三张图演示了在顶部图中改变抗生素名称长度时,需要更改校正值。我想尝试编写代码,使其自动发生。

英文:

I would like to be able to control the y-axis title position for each plot with respect to the y-axis itself, so that when I input differing antibiotic name lengths into the top plot, the y-axis title in the bottom plot is the same distance from the y-axis and I do not have to manually change this in the bottom plot everytime I change the antiobiotic names in the top plot. Anyone able to help?

The first plot shows the default without any adjustment.

The second plot shows the desired distance the y-axis title should be from the y-axis labels.

The third plot demonstates how varying the antibiotic name length in top plot means the correction needs to be changed. I would like to try to code this to happen automatically.

  1. library(tidyverse)
  2. library(patchwork)
  3. therapy_1 <- tibble(
  4. antibiotic = c("IV Piperacillin-tazobactam", "IV Piperacillin-tazobactam"),
  5. time_point = c("start", "end"),
  6. date = c("2023-04-10","2023-04-25")) %>%
  7. mutate(date = as_date(date))
  8. therapy_2 <-tibble(
  9. antibiotic = c("PO co-trimoxazole", "PO co-trimoxazole"),
  10. time_point = c("start", "end"),
  11. date = c("2023-04-10","2023-04-25")) %>%
  12. mutate(date = as_date(date))
  13. inflammatory_markers <- tibble(
  14. date = c("2023-04-10", "2023-04-12"),
  15. value = c("38.1", "37.8")) %>%
  16. mutate(date = as_date(date))
  17. p1 <- therapy_1 %>%
  18. ggplot(aes(x = date)) +
  19. geom_line(aes(y = antibiotic, colour = antibiotic)) +
  20. theme(legend.position = "none")
  21. p2 <- therapy_2 %>%
  22. ggplot(aes(x = date)) +
  23. geom_line(aes(y = antibiotic, colour = antibiotic)) +
  24. theme(legend.position = "none")
  25. default <- inflammatory_markers %>%
  26. ggplot(aes(x = date)) +
  27. geom_point(aes(y = value))
  28. desired <- inflammatory_markers %>%
  29. ggplot(aes(x = date)) +
  30. geom_point(aes(y = value)) +
  31. theme(axis.title.y = element_text(margin = margin(r = -160)))
  32. # default output
  33. p1 / default

控制合并多个图表时的Y轴标题位置,使用patchwork包。<!-- -->

  1. # desired output
  2. p1 / desired

控制合并多个图表时的Y轴标题位置,使用patchwork包。<!-- -->

  1. # when length of y-axis labels shortened
  2. p2 / desired

控制合并多个图表时的Y轴标题位置,使用patchwork包。<!-- -->

<sup>Created on 2023-05-08 with reprex v2.0.2</sup>

答案1

得分: 0

这实际上并不是完全简单的(使用补丁)。我建议的解决方案也不是完美的。我认为最简单的解决方法是使用自定义注释作为您的y轴标题,而不是真正的y轴标题。这并不完美,因为图的大小仍然会变化,而注释位置将取决于您的x比例。

  1. ## 在您的下部面板添加自定义注释。
  2. desired &lt;-
  3. inflammatory_markers %&gt;%
  4. ggplot(aes(x = date)) +
  5. geom_point(aes(y = value)) +
  6. ## 使用geom_text而不是annotate,因为您正在使用日期,并且要使用“nudge_x”,而annotate无法使用
  7. geom_text(
  8. data = data.frame(), aes(
  9. label = &quot;value&quot;,
  10. x = min(inflammatory_markers$date),
  11. y = 1.5
  12. ),
  13. angle = 90, nudge_x = -.4
  14. ) +
  15. ## 限制坐标并关闭裁剪
  16. coord_cartesian(xlim = range(inflammatory_markers$date), clip = &quot;off&quot;) +
  17. ## 删除y轴标签
  18. labs(y = NULL)
  1. p1 / desired

控制合并多个图表时的Y轴标题位置,使用patchwork包。<!-- -->

  1. p2 / desired

控制合并多个图表时的Y轴标题位置,使用patchwork包。<!-- -->

<sup>创建于2023-05-08,使用reprex v2.0.2</sup>

英文:

That's actually not exactly trivial (with patchwork). The solution which I am suggesting would also not be perfect. I think the easiest hack would be to use a custom annotation as your y axis title instead of a real y axis title. It is not perfect, because the size of the plots still vary and the annotation position will be located depending on your x scale.

  1. ## adding a custom annotation to your inferior panel.
  2. desired &lt;-
  3. inflammatory_markers %&gt;%
  4. ggplot(aes(x = date)) +
  5. geom_point(aes(y = value)) +
  6. ## annotate with geom_text and not annotate, because you are using dates
  7. ## and you want to use &quot;nudge_x&quot; which doesn&#39;t work with annotate
  8. geom_text(
  9. data = data.frame(), aes(
  10. label = &quot;value&quot;,
  11. x = min(inflammatory_markers$date),
  12. y = 1.5
  13. ),
  14. angle = 90, nudge_x = -.4
  15. ) +
  16. ## limit coordinates and turn clipping off
  17. coord_cartesian(xlim = range(inflammatory_markers$date), clip = &quot;off&quot;) +
  18. ## remove y label
  19. labs(y = NULL)
  20. # default output
  21. p1 / desired

控制合并多个图表时的Y轴标题位置,使用patchwork包。<!-- -->

  1. p2 / desired

控制合并多个图表时的Y轴标题位置,使用patchwork包。<!-- -->

<sup>Created on 2023-05-08 with reprex v2.0.2</sup>

答案2

得分: 0

不同到足以值得提供第二个答案(依我个人看法)。如果您愿意使用另一个包,您也可以使用 egg::ggarrange,它应该完全满足您的需求。

  1. library(egg)
  2. ggarrange(p1, desired)

控制合并多个图表时的Y轴标题位置,使用patchwork包。

  1. ggarrange(p2, desired)

控制合并多个图表时的Y轴标题位置,使用patchwork包。

创建于2023-05-08,使用 reprex v2.0.2

英文:

Different enough to merit a second answer (IMO). If you're happy to use another package, you can also use egg::ggarrange which should do exactly what you need.

  1. library(egg)
  2. ggarrange(p1, desired)

控制合并多个图表时的Y轴标题位置,使用patchwork包。<!-- -->

  1. ggarrange(p2, desired)

控制合并多个图表时的Y轴标题位置,使用patchwork包。<!-- -->

<sup>Created on 2023-05-08 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月6日 23:12:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189627.html
匿名

发表评论

匿名网友

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

确定