1

I would like to have the annotation on the very first facet of the following ggplot. right now, the code draws annotation on all facets. Anyway forward would be appreciate.

library(ggplot2)
library(lubridate)

set.seed(123)

DF <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2005-12-31"), by = "1 month"),
                  Ob = runif(60,1,5), L95 =runif(60, 0,4), U95 = runif(60,2,7), Sim = runif(60,1,5)) %>% 
      pivot_longer(names_to = "Variable", values_to = "Value", -Date)

ggplot(data = DF, aes(x = Date))+
  geom_line(aes(y = Value))+
  facet_wrap(~ Variable, scales = "free_y", nrow = 4)+
  geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
  annotate(geom = "text", x = as.Date("2002-01-01"), y = 3, label = "Calibration")+
  annotate(geom = "text", x = as.Date("2005-06-01"), y = 3, label = "Validation")

enter image description here

Hydro
  • 1,057
  • 12
  • 25

2 Answers2

1

You can try something like this wrapping data coordinates for the value on top of facets using geom_text():

library(ggplot2)
library(lubridate)
#Code
ggplot(data = DF, aes(x = Date))+
  geom_line(aes(y = Value,group=Variable))+
  facet_wrap(~ Variable, scales = "free_y", nrow = 4)+
  geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
  geom_text(data=data.frame(Variable='L95',Date=as.Date("2002-01-01"),
                            label="Calibration",Value=max(DF$Value)),
            aes(y=Value,label=label))+
  geom_text(data=data.frame(Variable='L95',Date=as.Date("2005-06-01"),
                            label="Validation",Value=max(DF$Value)),
            aes(y=Value,label=label))

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84
  • thanks @Duck, i want the `annotation` on the first `facet` not on all `facets`. right now the code draws `annotation` on all four `facets`. The `calibration` and `validation` text should be displayed on the very top `facet`. – Hydro Nov 23 '20 at 01:10
  • @Hydro Hi dear. Deeply sorry for misunderstanding the task. I thought you wanted labels on top of each facet. Now, I have updated the post. Hoping that can be useful for you! – Duck Nov 23 '20 at 13:17
1

You can do this with geom_text. Create a separate dataframe for plotting.

library(ggplot2)

text_data <- data.frame(x = as.Date(c("2002-01-01", "2005-06-01")), 
                        y = 3.5, Variable = sort(unique(DF$Variable))[1], 
                        label = c("Calibration", "Validation"))

ggplot(data = DF, aes(x = Date))+
  geom_line(aes(y = Value))+
  facet_wrap(~ Variable, scales = "free_y", nrow = 4)+
  geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30) + 
  geom_text(data = text_data, aes(x, y, label = label), color = 'blue')

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Hi @Ronak, can you please look into this question? https://stackoverflow.com/questions/64970528/annotation-on-only-the-first-facet-of-ggplot-in-r. Thanks – Hydro Nov 23 '20 at 14:40