2

I have the following code that produce a ggplot that has text (i.e., "calibration") on both facets. I want the text be appeared on the first facet only. I tried a few things but didn't succeed. Any help would be appreciated.

library(ggplot2)
library(lubridate)

set.seed(123)

DF1 <- 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),
                  Loc = rep("Upstream", 60))

DF2 <- 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),
                  Loc = rep("Downstream", 60))

DF <- dplyr::bind_rows(DF1,DF2)

DF$Loc <- factor(DF$Loc, levels = c("Upstream","Downstream"))


ggplot(DF, aes(x = Date))+
  geom_ribbon(aes(ymin = L95, ymax = U95), fill = "grey30", alpha = 0.4)+
  geom_line(aes(y = Ob, color = "blue"), size = 1 )+
  geom_line(aes(y = Sim, color = "black"), size =  1, linetype = "dashed")+
  geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
  facet_wrap(~ Loc, ncol = 1, scales = "free_y")+ 
  theme_bw()+
  annotate(geom = "text", x = as.Date("2002-01-01"), y = 4, label = "Calibration")

enter image description here

Hydro
  • 1,057
  • 12
  • 25

1 Answers1

3

Try this trick:

library(ggplot2)
#Code
ggplot(DF, aes(x = Date))+
  geom_ribbon(aes(ymin = L95, ymax = U95), fill = "grey30", alpha = 0.4)+
  geom_line(aes(y = Ob, color = "blue"), size = 1 )+
  geom_line(aes(y = Sim, color = "black"), size =  1, linetype = "dashed")+
  geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
  facet_wrap(~ Loc, ncol = 1, scales = "free_y")+ 
  theme_bw()+
  geom_text(data=data.frame(Date=as.Date("2002-01-01"),y=4,
                            label = "Calibration",Loc='Upstream'),
            aes(y=y,label=label))

Output:

enter image description here

You can also use Loc=unique(DF$Loc)[1] in the geom_text() side. It will produce same output.

Duck
  • 39,058
  • 13
  • 42
  • 84
  • That's what i want- However, when I ran the code, it changes the positioning of the `facets`. that means the upstream `facet` is now on the second row and the Downstream on the first row. – Hydro Nov 23 '20 at 15:05
  • @Hydro Try this slight change on your data `DF$Loc <- factor(DF$Loc, levels = c("Upstream","Downstream"),ordered=T)` and re run all the code. Let me know if the issue persists! – Duck Nov 23 '20 at 15:07
  • same issue- the order of the `facets` get change, even with the changes you suggested. – Hydro Nov 23 '20 at 15:10
  • @Hydro Are we using the same data from question? – Duck Nov 23 '20 at 15:11
  • Ok- your second suggestions (using `Loc=unique(DF$Loc)[1]` in the `geom_text()`) worked. Yes, i was usng the same data. Many thanks. Still wondering why the first option is didn't work. – Hydro Nov 23 '20 at 15:13
  • @Hydro Fantastic. Maybe it is an issue with the R session, which R version do you have? – Duck Nov 23 '20 at 15:14