Annotate outside plot area once in ggplot with facets

后端 未结 4 1123
粉色の甜心
粉色の甜心 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: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))
    

提交回复
热议问题