ggplot - change background color for 3 out of 12 months [duplicate]
Clash Royale CLAN TAG#URR8PPP
ggplot - change background color for 3 out of 12 months [duplicate]
This question already has an answer here:
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_rect
s but was only getting errors.
geom_rect
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:
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
dput(data.MSS)
geom_rect
Added dput to original thread.
– Bulbuzor
Aug 10 at 17:55
1 Answer
1
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
The OP is trying to get multiple
rect
s, not just one; annotate
only does one at a time– camille
Aug 10 at 16:23
rect
annotate
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 at 16:26
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 at 16:32
ggplot
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 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 at 17:27
can you put
dput(data.MSS)
so we can reproduce your graph? You can probably usegeom_rect
to put a shaded colour behind the lines.– Calum You
Aug 10 at 15:59