Annotate outside plot area once in ggplot with facets

后端 未结 4 1124
粉色の甜心
粉色の甜心 2021-01-18 17:25

I want to add an annotation outside the plotting area in a faceted ggplot. I can get the annotation that I want, but it\'s repeated for each facet. How can I get this annota

相关标签:
4条回答
  • 2021-01-18 17:40

    It's in fact very easy, just have a vector of labels, where the ones you don't want to plot are the empty string "".

    library("ggplot2")
    
    ggplot(mtcars, aes(x = hp, y = mpg)) +
      geom_point() +
      annotate("text", x = -20, y = 36, label = c("XX", "", "")) +
      facet_grid(.~cyl ) + 
      coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off")
    

    0 讨论(0)
  • 2021-01-18 17:44

    You can put a single tag label on a graph using tag in labs().

    ggplot(mtcars, aes(x = hp, y = mpg)) +
         geom_point() +
         facet_grid(.~cyl ) + 
         labs(tag = "XX") +
         coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off")
    

    This defaults to "top left", though, which may not be what you want. You can move it around with the theme element plot.tag.position, either as coordinates (between 0 and 1 to be in plot space) or as a string like "topright".

    ggplot(mtcars, aes(x = hp, y = mpg)) +
         geom_point() +
         facet_grid(.~cyl ) + 
         labs(tag = "XX") +
         coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off") +
         theme(plot.tag.position = c(.01, .95))
    

    0 讨论(0)
  • 2021-01-18 17:55

    With geom_text:

    dummy <- data.frame(cyl = c(4), l = c("XX"), stringsAsFactors = F)
    
    ggplot(mtcars, aes(x = hp, y = mpg)) +
      geom_point() +
      geom_text(data=dummy, aes(label=l), x = -20, y = 36) +
      facet_grid(.~cyl ) + 
      coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off")
    

    0 讨论(0)
  • 2021-01-18 17:56

    Alternatively, the package cowplot has the handy annotation function draw_label(). When used in combination with ggdraw(), can annotate anywhere on the canvas/sheet with the coordinates ranging from 0 to 1 (relative to the entire canvas). The function cowplot::draw_label() uses ggplot2::annotation_custom() under the hood.

    library(ggplot2)
    library(cowplot)
    #> Warning: package 'cowplot' was built under R version 3.5.2
    #> 
    #> Attaching package: 'cowplot'
    #> The following object is masked from 'package:ggplot2':
    #> 
    #>     ggsave
    
    # Revert to default theme; see https://stackoverflow.com/a/41096936/5193830
    theme_set(theme_grey())
    
    p <- ggplot(mtcars, aes(x = hp, y = mpg)) +
      geom_point() +
      facet_grid(. ~ cyl)
    
    ggdraw(p) + draw_label("XX", x = 0.02, y = 0.97)
    

    Created on 2019-01-14 by the reprex package (v0.2.1)

    0 讨论(0)
提交回复
热议问题