geom_boxplot: Put "character" labels on a continuous x axis with rectangles (geom_rect) as background
<p>I would like to create a boxplot for a time series, where I could color the backgroudn with rectangles (<code>geom_rect</code>), and specify the labels as character. </p>

<p>I can plot the boxplot, however as I need a continuous scale for specifying the rectangles, I am not able to plot the labels as character</p>

<p>Here I put the code I have written:</p>

<p><strong>1: Create a dataset</strong></p>

<pre><code># load packages
library (dplyr)
library (ggplot2)
Order &lt;- rep (seq (1:24), 100)
Values &lt;- runif (2400, 0, 1)
Season &lt;- rep ( (rep (c( rep("Spring", 3), rep ("Summer", 3), rep ("Autumn", 3), rep ("Winter", 3)),2)), 100)
Date &lt;- rep( (c("D-70", "J-71", "F-71", "M-71", "Ap-71", "May-71", "Jun-71", "Jul-71", "Au-71", "S-71", "O-71", "N-71",
          "D-71", "J-72", "F-72", "M-72", "Ap-72", "May-72", "Jun-72", "Jul-72", "Au-72", "S-72", "O-72", "N-72")), 100)
df &lt;- data.frame (Order, Values, Season, Date) ; head (df)
</code></pre>

<p><strong>Define the periods that I want to color with rectangles</strong></p>

<pre><code># Indicate the seasons for the X axis
Seasons_plot &lt;- df %&gt;% distinct(Date) %&gt;% arrange (Order) # Select the position of each season for plotting it
start_spring &lt;- which(Seasons_plot$Season == "Spring") ; end_spring &lt;- start_spring + 1
start_summer &lt;- which(Seasons_plot$Season == "Summer") ; end_summer &lt;- start_summer + 1
start_autumn &lt;- which(Seasons_plot$Season == "Autumn") ; end_autumn &lt;- start_autumn + 1
start_winter &lt;- which(Seasons_plot$Season == "Winter") ; end_winter &lt;- start_winter + 1

n &lt;- max (length(start_spring), length(start_summer), length(start_autumn), length(start_winter),
          length(end_spring), length(end_summer), length(end_autumn), length(end_winter))

length(start_spring) &lt;- n ; length(start_summer) &lt;- n ; length(start_autumn) &lt;- n; length(start_winter) &lt;- n
length(end_spring) &lt;- n; length(end_summer) &lt;- n; length(end_autumn) &lt;- n; length(end_winter) &lt;- n

#Indicate the size (y axis) of the rectangle
ymin_season &lt;- rep(0, n)
ymax_season &lt;- rep(1, n)

# Create a dataframe for indicating the position of the rectangles
shade &lt;- data.frame (start_spring, end_spring, 
                     start_summer, end_summer, 
                     start_autumn, end_autumn, 
                     start_winter, end_winter, 
                     ymin_season, ymax_season)
</code></pre>

<p><strong>Select the labels to plot in order to facilitate visualization (e.g. 1 each 4)</strong></p>

<pre><code>labels &lt;- Seasons_plot$Date 
labels
# Select the number of the labels to plot
labels_selected &lt;- seq(from=1, to=length(labels), by=4)
labels_plot &lt;- c()
for (i in 1:length(labels)){
  if (i %in% labels_selected){
    labels_plot[i] &lt;-  as.character (labels [i])
  } else {
    labels_plot[i] &lt;- c(" ")
  }
}
labels_plot
</code></pre>

<p><strong>Plot the boxplot</strong></p>

<pre><code>library (plyr)
ggplot (df) +
  geom_boxplot(aes(Order, Values, group=round_any(Order, 1)), outlier.colour = NA) +
  geom_rect (data=shade, aes(xmin=start_spring, xmax=end_spring, ymin=ymin_season, ymax=ymax_season), fill="green", alpha=0.05) +
  geom_rect (data=shade, aes(xmin=start_summer, xmax=end_summer, ymin=ymin_season, ymax=ymax_season), fill="blue", alpha=0.05) +
  geom_rect (data=shade, aes(xmin=start_autumn, xmax=end_autumn, ymin=ymin_season, ymax=ymax_season), fill="orange", alpha=0.05) +
  geom_rect (data=shade, aes(xmin=start_winter, xmax=end_winter, ymin=ymin_season, ymax=ymax_season), fill="white", alpha=0.05) +
  scale_x_discrete (labels=labels_plot)
detach("package:plyr", unload=TRUE)
</code></pre>

<p>Do you know how I can put the labels as character in this plot? or how I could mix two plots (one for the rectangles and the other one for the boxplot with the labels)?</p>

<p>Thanks in advance, </p>