Annotate Text on Time Series Data

I am trying to add some annotated text on time series data, in simpler terms something that looks like

Red Line: _INSERT CURRENT VALUE OF MOST RECENT DATE_ 
Black Line: _INSERT CURRENT VALUE OF MOST RECENT DATE_ 
Dashed Line: 0.014

I tried annotating some text in my ggplot using the following code, but nothing appeared on my plot so I'm not sure why it wasn't working. This is what I was using

  geom_text(x=2020-04-20, y=0.60, label="Testing Text") +

Hi, I assume your x variable is of class Date? Try turning it into a date firs, e.g. as.Date("2020-04-20").

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

You can use either paste() or glue::glue() to vcombine strings with values. This snippet should work. (Unfortunately, I can't try it since you did not share your data.)

CFR_labels <-
  CFR_Final %>%
  filter(Date == max(Date))

CFR_Final %>%
  ggplot(aes(x = Date, y = CFR_canada)) +
  geom_text(data = CRF_labels, x=Date, y=0.60, label=glue::glue("Calculated CFR: {whateveryourcolumnisnamed}")) +
  geom_text(data = CRF_labels, x=Data, y=0.50, label=glue::glue("Estimated CFR: {whateveryourcolumnisnamed}")) +
  ...

You can also include the filtering step inside your ggplot code if you prefer that:

CFR_Final %>%
  ggplot(aes(x = Date, y = CFR_canada)) +
  geom_text(data = CFR_Final %>% filter(Date == max(Date)), x=Date, y=0.60, label=glue::glue("Calculated CFR: {whateveryourcolumnisnamed}")) +
  geom_text(data = CFR_Final %>% filter(Date == max(Date)), x=Data, y=0.50, label=glue::glue("Estimated CFR: {whateveryourcolumnisnamed}")) +
  ...

@Z3tt Thank you this is what I was looking for, I have a followup question, if I wanted to show the most recent value, for the most recent date in the time series beside the text, is this possible? and what would the correct syntax be. This is the block of code I am using for the ggpplot.

I'd like it beside the labels of "calculated CFR" and "Estimated CFR"

CFR_Final %>%
  ggplot(aes(x = Date, y = CFR_canada)) +
  geom_text(x=as.Date("2020-05-21"), y=0.60, label="Calculated CFR: ") +
  geom_text(x=as.Date("2020-05-21"), y=0.50, label="Estimated CFR: ") +
  geom_text(x=as.Date("2020-05-21"), y=0.40, label="Control CFR: 1.4%") +
  scale_y_continuous(labels = function(x) paste0(x*100, "%")) +
  geom_hline(aes(yintercept=0.014), colour="#990000", linetype="dashed") + #This is our control line of current CFR 
  geom_line(color = "Black") +
  geom_line(y = CFR_estimate_temp, color = "Red") + 
  scale_x_date(breaks = seq.Date(from = as.Date("2020-03-07"), 
                                 to = as.Date("2020-06-25"), by = 25)) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "Black")) +
  labs(title = "Case Fatality Rate (CFR) in Canada", subtitle = "Case Fatality Rate (Over-Estimate in Black & Calculated In Red)",
       x = "Date", y = "CFR In Percent", caption = "Data Collected From Johns Hopkins Center for Systems Science & Engineering")
ggsave("CFR_Over_And_Calculated.png", plot = last_plot())