0

I am attempting to add labels from another column (year), to my ggplot graph, but its giving me the following error:

Error: geom_text_repel requires the following missing aesthetics: label

My data: 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

Here is the code I used:

  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()

So I like the labels to be the year instead of the default 1-5. Does anybody know a way?

M--
  • 25,431
  • 8
  • 61
  • 93
Nomnoom
  • 15
  • 7

1 Answers1

1

Does this work?:

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()

Created on 2021-07-08 by the reprex package (v2.0.0)

data

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"))

Peter
  • 11,500
  • 5
  • 21
  • 31
  • This was very helpful! For some reason I was stuck using the 'rownames', but this easy solution fixed it. Thank you very much – Nomnoom Jul 09 '21 at 14:08
  • I'm glad, If this answered your question it is customary to accept and upvote the answer; assuming this is possible. – Peter Jul 09 '21 at 14:58
  • Unfortunately I don't have enough points yet to accept and upvote :( – Nomnoom Jul 23 '21 at 22:43
  • I think you can always accept, you may need points to upvote! – Peter Jul 23 '21 at 23:06