如何向具有多个 y 轴的图形添加单个注释
How to add single annotation to graph with multiple y axes
我希望添加垂直线和标签以引起对 ggplot2 图表 x 轴上特定日期的注意。线条和标签将注意力集中在同一日期的多个变量上,这些变量相互叠加。我的问题是我只希望每个标签显示一次,但我的尝试是将标签放在 'each' y 轴图上。
我模拟了一些虚拟数据来说明。为避免疑义,我希望实现的是在第一条垂直线上带有文本 "A" 的单个标签,在第二条和第三条线上与 "B" 和 "C" 类似,目前我为每行获取三个标签。
library(ggplot2)
library(reshape2)
library(dplyr)
library(scales)
set.seed(1234)
PetSightings = data.frame(DateRange = seq(as.Date("2016/1/1"), by = "day", length.out = 60),
cats = round(rnorm(60, 10, 3), 0), dogs = round(rnorm(60, 20, 10), 0),
parrots = round(rnorm(30, 5, 2)))
PetSightingsMelt = melt(PetSightings, id.vars = "DateRange")
ggplot(PetSightingsMelt, aes(x = DateRange, y = value)) +
ggtitle("Pet Sightings") +
labs(x = "Date", y = "Values") +
geom_bar(stat = "identity", aes(colour = variable)) +
geom_vline(xintercept = as.numeric(PetSightings$DateRange[15]), linetype = "longdash", alpha = 0.5) +
geom_vline(xintercept = as.numeric(PetSightings$DateRange[45]), linetype = "longdash", alpha = 0.5) +
geom_vline(xintercept = as.numeric(PetSightings$DateRange[55]), linetype = "longdash", alpha = 0.5) +
annotate("text", x = PetSightings$DateRange[15], y = 25, label = "A", size = 6) +
annotate("text", x = PetSightings$DateRange[45], y = 25, label = "B", size = 6) +
annotate("text", x = PetSightings$DateRange[55], y = 25, label = "C", size = 6) +
facet_grid(variable ~ ., scales = "free_y") +
theme(legend.position = "none",
axis.text.x = element_text(angle = 60, vjust = 0.5, size = 6),
plot.title = element_text(size = 20),
axis.text.x = element_text(size = 10),
axis.title.x = element_text(face = "bold", size = 12),
axis.title.y = element_text(face = "bold", size = 12),
axis.text.y = element_text(size = 8),
strip.text.y = element_text(size = 10, angle = 0)) +
scale_x_date(labels = date_format("%Y-%m"), date_breaks = "1 month") +
scale_y_continuous(labels = comma)
看来我们不能使用annotate
来做到这一点。因此,我们可以创建一个数据框来将特定的 geom_text
放到绘图上。假设我们创建数据框 ann_label
如下:
ann_label <- data.frame(DateRange = PetSightings$DateRange[c(15, 45, 55)], value = 25, label = LETTERS[1:3],
variable = factor("cats", level = c("cats", "dogs", "parrots")))
然后,您可以将 geom_text
添加到您的 ggplot
对象:
+ geom_text(data = ann_label, aes(label = label), size = 6)
结果:
我希望添加垂直线和标签以引起对 ggplot2 图表 x 轴上特定日期的注意。线条和标签将注意力集中在同一日期的多个变量上,这些变量相互叠加。我的问题是我只希望每个标签显示一次,但我的尝试是将标签放在 'each' y 轴图上。
我模拟了一些虚拟数据来说明。为避免疑义,我希望实现的是在第一条垂直线上带有文本 "A" 的单个标签,在第二条和第三条线上与 "B" 和 "C" 类似,目前我为每行获取三个标签。
library(ggplot2)
library(reshape2)
library(dplyr)
library(scales)
set.seed(1234)
PetSightings = data.frame(DateRange = seq(as.Date("2016/1/1"), by = "day", length.out = 60),
cats = round(rnorm(60, 10, 3), 0), dogs = round(rnorm(60, 20, 10), 0),
parrots = round(rnorm(30, 5, 2)))
PetSightingsMelt = melt(PetSightings, id.vars = "DateRange")
ggplot(PetSightingsMelt, aes(x = DateRange, y = value)) +
ggtitle("Pet Sightings") +
labs(x = "Date", y = "Values") +
geom_bar(stat = "identity", aes(colour = variable)) +
geom_vline(xintercept = as.numeric(PetSightings$DateRange[15]), linetype = "longdash", alpha = 0.5) +
geom_vline(xintercept = as.numeric(PetSightings$DateRange[45]), linetype = "longdash", alpha = 0.5) +
geom_vline(xintercept = as.numeric(PetSightings$DateRange[55]), linetype = "longdash", alpha = 0.5) +
annotate("text", x = PetSightings$DateRange[15], y = 25, label = "A", size = 6) +
annotate("text", x = PetSightings$DateRange[45], y = 25, label = "B", size = 6) +
annotate("text", x = PetSightings$DateRange[55], y = 25, label = "C", size = 6) +
facet_grid(variable ~ ., scales = "free_y") +
theme(legend.position = "none",
axis.text.x = element_text(angle = 60, vjust = 0.5, size = 6),
plot.title = element_text(size = 20),
axis.text.x = element_text(size = 10),
axis.title.x = element_text(face = "bold", size = 12),
axis.title.y = element_text(face = "bold", size = 12),
axis.text.y = element_text(size = 8),
strip.text.y = element_text(size = 10, angle = 0)) +
scale_x_date(labels = date_format("%Y-%m"), date_breaks = "1 month") +
scale_y_continuous(labels = comma)
看来我们不能使用annotate
来做到这一点。因此,我们可以创建一个数据框来将特定的 geom_text
放到绘图上。假设我们创建数据框 ann_label
如下:
ann_label <- data.frame(DateRange = PetSightings$DateRange[c(15, 45, 55)], value = 25, label = LETTERS[1:3],
variable = factor("cats", level = c("cats", "dogs", "parrots")))
然后,您可以将 geom_text
添加到您的 ggplot
对象:
+ geom_text(data = ann_label, aes(label = label), size = 6)
结果: