1

I can't seem to find a solution to my issue. I am plotting physicochemical properties of water over time. To make it easier to look at, I would like to change the background color of each summer, the graph spans on 3 years. This is my ggplot code at the moment:

ggplot(aes(x = Date_terrain, y = MSS, color = Num_cours, group = Num_cours), data = data.MSS) + 
  geom_point() + 
  geom_line() + 
  scale_x_date(date_breaks = "6 month", 
               labels=date_format("%d-%m-%Y"),
               limits = as.Date(c('2016-06-01','2018-10-01')))

It works fine, albeit it isn't refined, but I can't seem to figure out how to do change. I tried with geom_rects but was only getting errors.

I'd like the background of each June 21st to September 22nd period grayed out.

Can let me know what function would be best for it?

Thank you very much.

dput:

structure(list(Date_terrain = structure(c(3L, 5L, 7L, 6L, 11L, 
4L, 7L, 6L, 10L, 4L, 2L, 8L, 10L, 4L, 2L, 6L, 11L, 4L, 2L, 6L, 
11L, 4L, 2L, 6L, 10L, 4L, 2L, 6L, 11L, 4L, 2L, 9L, 10L, 4L, 2L, 
8L, 9L, 9L, 8L, 8L, 1L, 1L, 1L), .Label = c("", "04-06-17", "04-08-16", 
"07-09-16", "09-09-16", "11-07-18", "14-07-17", "18-07-18", "19-07-18", 
"20-07-16", "21-07-16"), class = "factor"), MSS = c(0L, 1L, 9L, 
-6L, 2L, 1L, 9L, -6L, 6L, 2L, 11L, -6L, 6L, 1L, 4L, 8L, 1L, 1L, 
6L, -6L, 1L, 1L, 7L, 8L, 2L, 1L, 5L, -7L, 1L, 3L, 8L, -8L, 1L, 
3L, 12L, -3L, -6L, -7L, -4L, -6L, NA, NA, NA)), row.names = c(NA, 
43L), class = "data.frame")

Current graph:

Current graph

Community
  • 1
  • 1
Bulbuzor
  • 39
  • 1
  • 3

1 Answers1

0

Instead of geom_rect(), try:

... + 
annotate("rect",
  xmin=as.Date("2016-06-01"), #adjust date accordingly
  xmax=as.Date("2018-10-01"), #adjust date accordingly
  ymin=-Inf,
  ymax=Inf,
  fill="#FF000033") #set a transparent color so you can see the plot "behind" the rectangle
Josh Kay
  • 66
  • 3
  • The OP is trying to get multiple `rect`s, not just one; `annotate` only does one at a time – camille Aug 10 '18 at 16:23
  • One option for multiple years would be a loop. for( i in unique(data$year)){ plot = plot + annotate("rect", xmin = as.Date( paste0(i,"-09-21")), xmax = ..., ...) } – Josh Kay Aug 10 '18 at 16:26
  • 1
    You're not incorrect, but that defeats much of the purpose of `ggplot`! The "grammar of graphics" idea is that you're creating visual elements dynamically based on variables, rather than tacking fixed elements together – camille Aug 10 '18 at 16:32
  • 100% agreed - but during the interim between facing errors and finding the *best* solution, this will at least keep OP moving along.. – Josh Kay Aug 10 '18 at 16:36
  • Thank you very much Josh, it ishelpful and works great, I just added one annotate per year which will do the trick for now. However I have a hard time to convert a hex to the transparent one (tried changing or adding the first two letters to FF but it just makes a new color). Instead, is there a way I can plot the gridlines after adding the annotates? Works great for the points and lines when I put them after, but unsure how to plot the gridlines after the initial aes. Tried adding + theme(panel.grid= element_line(color = "grey80")) and variations but it still goes behind the solid block – Bulbuzor Aug 10 '18 at 17:27
  • You can use existing packages/functions to adjust the transparency of your selected color. See link below for some solutions. https://stackoverflow.com/questions/8047668/transparent-equivalent-of-given-color – Josh Kay Aug 10 '18 at 18:16