I would like to colours the background of a ggplot
. It's almost described here: link, but there are some issued that make my idea just not work.
First, my data looks like this:
# airquality
rects = data.frame(start = seq(0, 200, 50), end = seq(50, 250, 50), level = c ("good", "moderate", "unhealthy for sensitive groups", " unhealty", "very unhealth"))
I would like to make a background for some airquality-data. This data has timestamps. So the x-axis would run from sys.Date()-90
to sys.Date()
. The y-axis has some levels of no2-concentration, that I'd like to give some colours. My approach is the following
datemin = as.Date(sys.Date() - 90)
datemax = as.Date(sys.Date())
ggplot() + geom_rect(data = rects, aes(xmin = datemin, xmax = datemax, ymin = start, ymax = end, fill = level), alpha = 0.5)
This gives the following plot:
So far so good. But what I would like is to give every box a specif colour. And I don't know how to do this.
Moreover, when I combine this with my time-series data it says: Error: Invalid input: time_trans works with objects of class POSIXct only
So my questions are: How to get a specific colour for each box. And how to assure the dates are aacepted?