I have a dataset with two variables (Tb and Ta) over time (Date). Looks like this:
` Date Tb Ta Light
1 2015-02-15 01:13:00 36.103 22.751 nightime
2 2015-02-15 01:55:00 36.103 22.626 nightime
3 2015-02-15 02:37:00 35.605 22.626 nightime
4 2015-02-15 03:19:00 35.605 22.751 nightime
5 2015-02-15 04:01:00 36.103 23.001 nightime
6 2015-02-15 04:43:00 35.605 22.876 nightime`
I am trying to make a plot with different shading for levels in the factor 'Light'. So all points with 'nightime' in 'Light' would be shaded in grey while the 'daytime' would be white. Something like this:
Is there a way to get geom_rect()
to work with a factor level? I need all points coded as 'nightime' shaded with a grey background ...
I tried the following based on Using ggplot2 in R, how do I make the background of a graph different colours in different regions?
ggplot() + geom_rect(data=tbdf, (aes(xmin=Date,
xmax=Date, ymin=min(Tb), ymax=max(Tb), fill=Light))) +
geom_line(data = tbdf, aes(x = Date, y = Tb)) +
geom_line(data = tbdf, aes(x = Date, y = Ta), colour='grey') +
xlab('Date') +
ylab('Temperature (°C)')
and it ends up with a legend for Light but still the usual grey shading:
Any suggestions?