如何在一个3×3的组合图中添加面板内标签?

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

patchwork: how to have within-panel tags for a 3x3 combined plot?

问题

与这个问题类似 https://stackoverflow.com/questions/76524971/patchwork-align-tags-inside-panels-for-side-by-side-plots/76525099#76525099,我需要将标签移到面板边框内,但现在是一个3x3的网格。

  1. library(devtools)
  2. library(ggplot2)
  3. library(patchwork)
  4. d1 <- runif(500)
  5. d2 <- rep(c("Treatment", "Control"), each = 250)
  6. d3 <- rbeta(500, shape1 = 100, shape2 = 3)
  7. d4 <- d3 + rnorm(500, mean = 0, sd = 0.1)
  8. plotData <- data.frame(d1, d2, d3, d4)
  9. str(plotData)

此代码用于确保每行显示一个合并的图例:

  1. p1 <- ggplot(data = plotData) + geom_boxplot(aes(x = d2, y = d1, fill = d2)) + theme(legend.position = "none")
  2. p2 <- ggplot(data = plotData) + geom_boxplot(aes(x = d2, y = d1, fill = d2)) + theme(legend.position = "none")
  3. p3 <- ggplot(data = plotData) + geom_boxplot(aes(x = d2, y = d1, fill = d2)) + theme(legend.position = "right")
  4. p4 <- ggplot(data = plotData) + geom_boxplot(aes(x = d2, y = d1, fill = d2)) + theme(legend.position = "none")
  5. p5 <- ggplot(data = plotData) + geom_boxplot(aes(x = d2, y = d1, fill = d2)) + theme(legend.position = "none")
  6. p6 <- ggplot(data = plotData) + geom_boxplot(aes(x = d2, y = d1, fill = d2)) + theme(legend.position = "right")
  7. p7 <- ggplot(data = plotData) + geom_boxplot(aes(x = d2, y = d1, fill = d2)) + theme(legend.position = "none")
  8. p8 <- ggplot(data = plotData) + geom_boxplot(aes(x = d2, y = d1, fill = d2)) + theme(legend.position = "none")
  9. p9 <- ggplot(data = plotData) + geom_boxplot(aes(x = d2, y = d1, fill = d2)) + theme(legend.position = "right")

此代码生成一个带有标签的组合图,位于默认位置:

  1. (((p1 + p2 + p3) /
  2. (p4 + p5 + p6) /
  3. (p7 + p8 + p9)) + guide_area()) & plot_annotation(tag_levels = "A")

添加 & plot_annotation(tag_levels = "A") + theme(legend.position = "right", plot.tag.position = c(.15, .95)) 将移除标签。

感谢您的访问。

英文:

Similar to this problem <https://stackoverflow.com/questions/76524971/patchwork-align-tags-inside-panels-for-side-by-side-plots/76525099#76525099>, I need to move tags inside panel borders, but now for a 3x3 grid.

  1. library(devtools)
  2. library(ggplot2)
  3. library(patchwork)
  4. d1 &lt;- runif(500)
  5. d2 &lt;- rep(c(&quot;Treatment&quot;,&quot;Control&quot;),each=250)
  6. d3 &lt;- rbeta(500,shape1=100,shape2=3)
  7. d4 &lt;- d3 + rnorm(500,mean=0,sd=0.1)
  8. plotData &lt;- data.frame(d1,d2,d3,d4)
  9. str(plotData)

This code is to make sure one combined legend is shown per row:

  1. p1 &lt;- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2)) + theme(legend.position = &quot;none&quot;)
  2. p2 &lt;- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2)) + theme(legend.position = &quot;none&quot;)
  3. p3 &lt;- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2)) + theme(legend.position = &quot;right&quot;)
  4. p4 &lt;- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2)) + theme(legend.position = &quot;none&quot;)
  5. p5 &lt;- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2)) + theme(legend.position = &quot;none&quot;)
  6. p6 &lt;- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2)) + theme(legend.position = &quot;right&quot;)
  7. p7 &lt;- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2)) + theme(legend.position = &quot;none&quot;)
  8. p8 &lt;- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2)) + theme(legend.position = &quot;none&quot;)
  9. p9 &lt;- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2)) + theme(legend.position = &quot;right&quot;)

This code produced a combined plot with tags, at default locations

  1. (((p1 + p2 + p3) /
  2. (p4 + p5 + p6) /
  3. (p7 + p8 + p9)) + guide_area()) &amp; plot_annotation(tag_levels = &quot;A&quot;)

如何在一个3×3的组合图中添加面板内标签?

Adding &amp; plot_annotation(tag_levels = &quot;A&quot;) + theme(legend.position = &quot;right&quot;, plot.tag.position = c(.15, .95)) removed the tags.

如何在一个3×3的组合图中添加面板内标签?

Thanks for stopping by.

答案1

得分: 1

也许有一种更直接的方法来实现您期望的结果,但一个选项是使用 annotateannotation_custom 来伪造您的内部标记,就像我在下面所做的那样,这意味着手动将 "标记" 添加为每个图的注释:

  1. library(ggplot2)
  2. library(patchwork)
  3. plot_list <- lapply(1:9, \(x) {
  4. legend_position <- if (x %% 3 == 0) {
  5. "right"
  6. } else {
  7. "none"
  8. }
  9. ggplot(data = plotData) +
  10. geom_boxplot(aes(x = d2, y = d1, fill = d2)) +
  11. theme(legend.position = legend_position)
  12. })
  13. add_tag <- function(label, x = 1, y = 1, padding.x = unit(5, "pt"), padding.y = padding.x, hjust = 1, vjust = 1, size = 13) {
  14. annotation_custom(
  15. grid::textGrob(
  16. x = unit(x, "npc") - padding.x,
  17. y = unit(y, "npc") - padding.y,
  18. hjust = hjust, vjust = vjust,
  19. label = label, gp = grid::gpar(fontsize = size)
  20. )
  21. )
  22. }
  23. # 默认位置:右上角
  24. lapply(
  25. seq_along(plot_list), \(x) plot_list[[x]] + add_tag(LETTERS[x])
  26. ) |>
  27. wrap_plots(ncol = 3)

如何在一个3×3的组合图中添加面板内标签?<!-- -->

  1. # 标记在 "左上角"
  2. lapply(
  3. seq_along(plot_list), \(x) plot_list[[x]] + add_tag(
  4. LETTERS[x],
  5. x = 0, y = 1,
  6. hjust = 0, vjust = 1,
  7. padding.x = -unit(5, "pt"), padding.y = unit(5, "pt")
  8. )
  9. ) |>
  10. wrap_plots(ncol = 3)

如何在一个3×3的组合图中添加面板内标签?<!-- -->

英文:

Perhaps there is a more straightforward approach to achieve your desired result but one option would be to fake your inner tags using annotate or annotation_custom as I do below which means to manually add the "tags" as annotations to each plot:

  1. library(ggplot2)
  2. library(patchwork)
  3. plot_list &lt;- lapply(1:9, \(x) {
  4. legend_position &lt;- if (x %% 3 == 0) {
  5. &quot;right&quot;
  6. } else {
  7. &quot;none&quot;
  8. }
  9. ggplot(data = plotData) +
  10. geom_boxplot(aes(x = d2, y = d1, fill = d2)) +
  11. theme(legend.position = legend_position)
  12. })
  13. add_tag &lt;- function(label, x = 1, y = 1, padding.x = unit(5, &quot;pt&quot;), padding.y = padding.x, hjust = 1, vjust = 1, size = 13) {
  14. annotation_custom(
  15. grid::textGrob(
  16. x = unit(x, &quot;npc&quot;) - padding.x,
  17. y = unit(y, &quot;npc&quot;) - padding.y,
  18. hjust = hjust, vjust = vjust,
  19. label = label, gp = grid::gpar(fontsize = size)
  20. )
  21. )
  22. }
  23. # Default: topright corner
  24. lapply(
  25. seq_along(plot_list), \(x) plot_list[[x]] + add_tag(LETTERS[x])
  26. ) |&gt;
  27. wrap_plots(ncol = 3)

如何在一个3×3的组合图中添加面板内标签?<!-- -->

  1. # Tags in &quot;topleft&quot; corner
  2. lapply(
  3. seq_along(plot_list), \(x) plot_list[[x]] + add_tag(
  4. LETTERS[x],
  5. x = 0, y = 1,
  6. hjust = 0, vjust = 1,
  7. padding.x = -unit(5, &quot;pt&quot;), padding.y = unit(5, &quot;pt&quot;)
  8. )
  9. ) |&gt;
  10. wrap_plots(ncol = 3)

如何在一个3×3的组合图中添加面板内标签?<!-- -->

答案2

得分: 1

感谢Stefan提供的提示。这段代码处理不同的子图。

  1. # 列出子图
  2. plot_list <- list(p1, p2, p3, p4, p5, p6, p7, p8, p9)
  3. # 定义一个添加标签到图表的函数
  4. add_tag <- function(label, x = 1, y = 1, padding.x = unit(5, "pt"), padding.y = padding.x, hjust = 1, vjust = 1, size = 13) {
  5. annotation_custom(
  6. grid::textGrob(
  7. x = unit(x, "npc") - padding.x,
  8. y = unit(y, "npc") - padding.y,
  9. hjust = hjust, vjust = vjust,
  10. label = label, gp = grid::gpar(fontsize = size)
  11. )
  12. )
  13. }
  14. # 向每个图表添加标签
  15. plot_list <- lapply(
  16. seq_along(plot_list),
  17. \(x) plot_list[[x]] + add_tag(LETTERS[x], x = .15, y = .96)
  18. )
  19. # 将图表组合成一个网格
  20. wrap_plots(plot_list, ncol = 3)
英文:

Many thanks to Stefan for the hints. This code handles unidentical subplots.

  1. # List the subplots
  2. plot_list &lt;- list(p1, p2, p3,p4, p5, p6, p7, p8, p9)
  3. # Define a function to add a tag to a plot
  4. add_tag &lt;- function(label, x = 1, y = 1, padding.x = unit(5, &quot;pt&quot;), padding.y = padding.x, hjust = 1, vjust = 1, size = 13) {
  5. annotation_custom(
  6. grid::textGrob(
  7. x = unit(x, &quot;npc&quot;) - padding.x,
  8. y = unit(y, &quot;npc&quot;) - padding.y,
  9. hjust = hjust, vjust = vjust,
  10. label = label, gp = grid::gpar(fontsize = size)
  11. )
  12. )
  13. }
  14. # Add a tag to each plot
  15. plot_list &lt;- lapply(
  16. seq_along(plot_list),
  17. \(x) plot_list[[x]] + add_tag(LETTERS[x], x = .15, y = .96)
  18. )
  19. # Combine the plots into a grid
  20. wrap_plots(plot_list, ncol = 3)

huangapple
  • 本文由 发表于 2023年6月22日 01:42:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525899.html
匿名

发表评论

匿名网友

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

确定