【问题标题】:How to add single annotation to overall ggplot plot and not to each facet如何将单个注释添加到整体 ggplot 图而不是每个方面
【发布时间】:2021-03-03 13:59:05
【问题描述】:

我有一个带有两个方面的 ggplot,每个方面都包含相同的自定义添加参考线。我想为每条线添加一个注释标签,并根据整体图上的位置指定每个标签的位置。

我尝试使用annotate 添加标签,但这会为每个单独的方面添加标签。

当涉及构面时,如何在“全局”整体绘图区域上指定单个标签的位置(类似于下面示例中 legend.positionxy 的行为方式)?

library(ggplot2)

p <- mtcars %>% 
  ggplot(aes(x = mpg, y = disp, colour = am)) + 
  geom_point() + 
  geom_vline(aes(xintercept = 15), 
             linetype = "dotted",
             colour = "grey20") + 
  geom_vline(aes(xintercept = 25), 
             linetype = "dotted",
             colour = "grey20") + 
  facet_wrap(~vs, nrow = 2)

# desired behaviour is to position labels using x and y of overall plot area, as per positioning of legend 
p <- p + 
  # x and y refer to positions on overall plot here, not to values of variables within individual facets
  theme(legend.position = c(x = 0.9, y = 0.5))

# failed attempt adds labels to each facet
p <- p + 
  # x and y refer to individual facets/values of x and y variables here
  annotate("text", x = 15 , y = 0.5,
           label = "This label\nshould be on midpoint of y", 
           colour = "grey50") +
  annotate("text", x = 25 , y = 0.75,
           label = "This label\nshould be 3/4 up plot", 
           colour = "grey50")
 
# show plot
p

谢谢!

【问题讨论】:

    标签: r ggplot2 data-visualization data-annotations facet


    【解决方案1】:

    您可以使用grid::grid.text() 在画布中放置特定标签。多次使用它来实现你想要的。当然,也要调整标签的位置。

    请看下面的代码。

    library(ggplot2)
    library(magrittr)
    library(grid)
    
    p <- mtcars %>% 
      ggplot(aes(x = mpg, y = disp, colour = am)) + 
      geom_point() + 
      geom_vline(aes(xintercept = 15), 
                 linetype = "dotted",
                 colour = "grey20") + 
      geom_vline(aes(xintercept = 25), 
                 linetype = "dotted",
                 colour = "grey20") + 
      facet_wrap(~vs, nrow = 2)
    
    p
    grid.text("text1", x = 0.5, y = 0.5)
    grid.text("text2", x = 0.5, y = 0.75)
    

    这是你想要的吗?

    reprex package (v1.0.0) 于 2021 年 3 月 3 日创建

    【讨论】:

    • 这是一个不错的简单解决方案。很高兴知道。谢谢!
    【解决方案2】:

    请注意,任何此类注释都会有些麻烦,并且需要仔细调整标签的位置。我认为最方便的应该是使用拼凑或牛图进行自定义注释。 Cowplot offers specific functionality for plot labelling.

    library(tidyverse)
    library(cowplot)
    
    p <- mtcars %>% 
      ggplot(aes(x = mpg, y = disp, colour = am)) + 
      geom_point() + 
      geom_vline(aes(xintercept = 15), 
                 linetype = "dotted",
                 colour = "grey20") + 
      geom_vline(aes(xintercept = 25), 
                 linetype = "dotted",
                 colour = "grey20") + 
      facet_wrap(~vs, nrow = 2) +
      theme(legend.position = c(x = 0.9, y = 0.5))
    
    # desired behaviour is to position labels using x and y of overall plot area, as per positioning of legend 
    ggdraw(p) + draw_plot_label(x = c(.2,.6), y = c(.6,.6), 
                           label =  c("This label\nshould be on midpoint of y",  "This label\nshould be 3/4 up plot"),
                           hjust = 0, size = 9)
    

    reprex package (v1.0.0) 于 2021 年 3 月 3 日创建

    【讨论】:

    • 我没有考虑过使用cowplot 或patchwork。我已经在使用拼凑而成的地块,所以这是一个很好的建议/解决方案,谢谢。
    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多