Adding an annotation in a graph with ggplot2 in R

Solution for Adding an annotation in a graph with ggplot2 in R
is Given Below:

I have a df with information about the rolling average of deaths from a disease per day, from 2020-04-11 to 2021-07-01 (YYYY-MM-DD).

head(df)
        DATE  DEATHS_RA
1 2020-04-11   1.666667
2 2020-04-12   2.166667
3 2020-04-13   2.333333
4 2020-04-14   2.500000
5 2020-04-15   2.666667
6 2020-04-16   2.833333

With this code I get this basic graph.

try1 <- ggplot(df) +
  aes(x = DATE, y = DEATHS_RA) +
  geom_line(size = 1, colour = "#871709") +
  theme_minimal()+
  labs(x="",y="")

enter image description here

But I want to know if I can add an annotation like the following image (I extracted it from the internet).
enter image description here

I tried with the following code that I was looking for in different tutorials about ggplot2 but the result is the same as in the previous graph, as if R doesn’t recognize what I write in geom_label.

try1+
geom_label(label="Look at this!", x=2021-05-01,y=10,
    hjust = 0, 
    vjust = 0.5, 
    lineheight = 0.8,
    colour = "#555555", 
    fill = "white", 
    label.size = NA, 
    size = 6
  )

Your DATE is of class Date, which makes the line plot possible. so if you want to annotate, you need to input it as class Date as well:

df = structure(list(DATE = structure(c(18363, 18364, 18365, 18366, 
18367, 18368), class = "Date"), DEATHS_RA = c(1.666667, 2.166667, 
2.333333, 2.5, 2.666667, 2.833333)), row.names = c("1", "2", 
"3", "4", "5", "6"), class = "data.frame")

We create an annotation dataframe to contain your labels, it will be useful later:

ann = data.frame(DATE = as.Date("2020-04-12"),
DEATHS_RA=2.16,label = "Look at this!")

it looks like this:

try1+
geom_label(data = ann,aes(label=label))

enter image description here

To use an arrow, we need ggrepel:

library(ggrepel)
try1+
geom_label_repel(data = ann,aes(label=label),box.padding = 5)

enter image description here

I think you probably need to wrap label, y and x into aes()… it might be easier with geom_text though. You might also have issues with your x – 2021-05-01 = 2015? You might want to wrap it in as.Date().

The way to make the second plot is probably to have a geom_point with a larger size under the point(s) of interest. The arrow is then done with geom_curve and the text probably with geom_text