如何在x轴为日期的ggplot2中为水平线添加标签
解决ggplot日期轴水平线标签问题
你的核心问题是给水平线加标签时,未正确转换x轴的日期类型,导致文本无法正常显示。以下是两种可行的修改方案:
方案1:使用annotate()添加标签
annotate()需要将x参数转换为Date类型,不能直接传入字符串。同时可通过vjust/hjust调整标签位置:
# 先定义阈值变量(若未提前定义) h <- 9.9 h2 <- 12.97 hob_m_cs1_sort %>% group_by(date) %>% summarise(min = min(tmin_mean, na.rm = TRUE), max = max(tmax_mean, na.rm = TRUE), avg = mean(tmean,na.rm = TRUE)) %>% gather(metric, value, -date) %>% ggplot(aes(x = date, y = value, group = metric, color = metric)) + labs(color='Temperature') + ggtitle ("Hakalau Monthly Temperatures: Pua 'Akala, 1510 m") + theme(plot.title = element_text(hjust = 0.5)) + xlab("Date") + ylab ("Temperature ( ºC )") + scale_y_continuous(limits = c(2.5, 22.5), breaks = seq(5, 25, by = 5)) + scale_x_date(date_breaks = "2 months", date_labels = "%b %Y") + theme_ipsum() + theme(axis.text.x=element_text(angle=60, hjust=1)) + geom_line(aes(color = metric)) + geom_hline(aes(yintercept=h, linetype = "Culex development"), colour= 'darkorange1') + geom_hline(aes(yintercept=h2, linetype = "Avian malaria development"), colour= 'red') + # 添加水平线标签 annotate(geom = "text", x = as.Date("2021-02-01"), y = h, label = as.character(h), vjust = -0.5, color = "darkorange1") + annotate(geom = "text", x = as.Date("2021-02-01"), y = h2, label = as.character(h2), vjust = -0.5, color = "red") + scale_linetype_manual(name = "Temperature Thresholds", values = c(2, 2), guide = guide_legend(override.aes = list(color = c("red", "darkorange1")))) + scale_color_manual(values = c("steelblue1", "navyblue", "darkturquoise"), breaks=c('max', 'avg', 'min'), labels=c('Max', 'Avg', 'Min'))
方案2:使用geom_text()添加标签
若选用geom_text(),需避免继承全局的x/y映射,直接指定固定值并转换日期类型:
h <- 9.9 h2 <- 12.97 hob_m_cs1_sort %>% group_by(date) %>% summarise(min = min(tmin_mean, na.rm = TRUE), max = max(tmax_mean, na.rm = TRUE), avg = mean(tmean,na.rm = TRUE)) %>% gather(metric, value, -date) %>% ggplot(aes(x = date, y = value, group = metric, color = metric)) + labs(color='Temperature') + ggtitle ("Hakalau Monthly Temperatures: Pua 'Akala, 1510 m") + theme(plot.title = element_text(hjust = 0.5)) + xlab("Date") + ylab ("Temperature ( ºC )") + scale_y_continuous(limits = c(2.5, 22.5), breaks = seq(5, 25, by = 5)) + scale_x_date(date_breaks = "2 months", date_labels = "%b %Y") + theme_ipsum() + theme(axis.text.x=element_text(angle=60, hjust=1)) + geom_line(aes(color = metric)) + geom_hline(aes(yintercept=h, linetype = "Culex development"), colour= 'darkorange1') + geom_hline(aes(yintercept=h2, linetype = "Avian malaria development"), colour= 'red') + # 添加水平线标签 geom_text(x = as.Date("2021-02-01"), y = h, label = as.character(h), vjust = -0.5, color = "darkorange1", inherit.aes = FALSE) + geom_text(x = as.Date("2021-02-01"), y = h2, label = as.character(h2), vjust = -0.5, color = "red", inherit.aes = FALSE) + scale_linetype_manual(name = "Temperature Thresholds", values = c(2, 2), guide = guide_legend(override.aes = list(color = c("red", "darkorange1")))) + scale_color_manual(values = c("steelblue1", "navyblue", "darkturquoise"), breaks=c('max', 'avg', 'min'), labels=c('Max', 'Avg', 'Min'))
关键注意点
- 必须用
as.Date()将日期字符串转换为Date类型,否则ggplot无法识别x轴坐标; inherit.aes = FALSE用于阻止geom_text()继承全局的x/y映射,避免报错;- 可调整
vjust/hjust参数修改标签位置,如vjust=-0.5让标签位于水平线上方; - 若想将标签放在图表右侧,可替换x值为
max(hob_m_cs1_sort$date),自动对齐时间序列末尾。
内容的提问来源于stack exchange,提问作者steph
相关产品推荐
相关产品推荐

