I am building a linear series ruler using ggplot2, which uses geom_rect objects to highlight a specific time series event.
For purely aesthetic reasons, I'm interested in applying a gradient to the geom_rect object so that it fades to white / transparent as y increases.
I read other answers suggesting that geom_tile or geom_raster might offer a solution. I was not lucky with this ... for me geom_rect seems like an obvious choice, as I can indicate the beginning and end of the time series as a border. However, I hope that I am wrong. If anyone has any recommendations, this will be greatly appreciated. My attempt:
## READ DATA file = "Data.csv" timeSeries <- read.csv(file, header=TRUE) ## CONVERT DATA TO DATE CLASS timeSeries$Date <- as.Date(timeSeries$Date, "%d/%m/%y") timeSeries$Date <- as.Date(format(timeSeries$Date, "19%y-%m-%d")) ## SET GEOM_RECT DATA FRAME event <- c("Event1", "Event2", "Event3") startDate <- c("15/06/15", "12/07/17", "6/09/18") finishDate <- c("9/01/16", "18/11/17", "5/11/18") dates <- cbind(event, startDate, finishDate) dates <- as.data.frame(dates, rownames=NULL, stringsAsFactors=FALSE) dates$startDate <- as.Date(dates$startDate, "%d/%m/%y") dates$startDate <- as.Date(format(dates$startDate, "19%y-%m-%d")) dates$finishDate <- as.Date(dates$finishDate, "%d/%m/%y") dates$finishDate <- as.Date(format(dates$finishDate, "19%y-%m-%d")) ## PLOT USING GGPLOT plot <- ggplot(timeSeries) + geom_rect(data=dates, aes(xmin=startDate, xmax=finishDate, ymin=0, ymax=25), fill="blue", alpha=0.4) + geom_line(aes(x=Date, y=Event)) + scale_x_date(labels=date_format("19%y")) + ggtitle("") + xlab("Time Series") + ylab("Number") + theme_minimal() plot
The code above should produce this graph. Data can be downloaded from here .

r ggplot2
vengefulsealion
source share