使用小平面在ggplot中注释一次绘图外部区域 [英] Annotate outside plot area once in ggplot with facets
问题描述
我想在多面ggplot中的绘图区域之外添加注释.我可以获得所需的注释,但是每个方面都会重复该注释.如何使该注释仅显示一次?
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 annotation to appear only once?
例如,要在左上角注释一次"XX",我可以使用:
E.g., to annotate "XX" once in the top left hand corner I can use:
library("ggplot2")
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
facet_grid(.~cyl ) +
annotate("text", x = -20, y = 36, label = "XX") +
coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off")
但这实际上将其注释在每个构面的左上方.
However this actually annotates it to the top left of each facet.
如何使它只出现一次?
推荐答案
您可以使用labs()
中的tag
在图形上放置单个标签标签.
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")
不过,该默认设置为左上",这可能不是您想要的.您可以使用主题元素plot.tag.position
来移动它,或者作为坐标(在绘图空间中介于0和1之间)或像"topright"
这样的字符串.
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))
这篇关于使用小平面在ggplot中注释一次绘图外部区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!