【发布时间】:2021-03-03 13:59:05
【问题描述】:
我有一个带有两个方面的 ggplot,每个方面都包含相同的自定义添加参考线。我想为每条线添加一个注释标签,并根据整体图上的位置指定每个标签的位置。
我尝试使用annotate 添加标签,但这会为每个单独的方面添加标签。
当涉及构面时,如何在“全局”整体绘图区域上指定单个标签的位置(类似于下面示例中 legend.position 的 x 和 y 的行为方式)?
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