如何将自定义标签添加到ggplot?

How to add customized labels to ggplot?

我正在尝试将另一列(年份)的标签添加到我的 ggplot 图表中,但出现以下错误:

Error: geom_text_repel requires the following missing aesthetics: label

我的数据:thirdgraph2

year      pdor         juni
2016      1991-06-26   13305.0
2019      1991-06-16   13598.0
2017      1991-06-17   13944.5
2018      1991-06-17   15653.5
2015      1991-07-08   17143.0

这是我使用的代码:

  ggplot(thirdgraph2, aes(juni, pdor, label=rownames(thirdgraph2$year))) + 
         geom_point() + 
         theme_bw() + 
         labs(y="Calculated date of reproduction", 
         x="Accumulated GDD until 17th June") + 
         geom_text_repel()

所以我喜欢标签是年份而不是默认的 1-5。有人知道方法吗?

这个有用吗?:

library(tibble)
library(ggplot2)
library(ggrepel)
library(dplyr)


ggplot(thirdgraph2, aes(juni, pdor, label=year)) + 
  geom_point() + 
  theme_bw() + 
  labs(y="Calculated date of reproduction", 
       x="Accumulated GDD until 17th June") + 
  geom_text_repel()

reprex package (v2.0.0)

于 2021-07-08 创建

数据

thirdgraph2 <- tribble(
  ~"year",      ~"pdor",      ~"juni",
2016,      "1991-06-26", 13305.0,
2019,      "1991-06-16", 13598.0,
2017,      "1991-06-17", 13944.5,
2018,      "1991-06-17", 15653.5,
2015,      "1991-07-08", 17143.0)

#assuming you want the dates as dates: 

thirdgraph2 <- 
  thirdgraph2 %>% 
  mutate(pdor = as.Date(pdor, format = "%Y-%m-%d"))