Trouble with horizontal and vertical lines accepting class "Date" in ggplot?
ggplot2, r
Solution
There are two thinks you need to change in your code.
First, as for making the `time` column in `mdfr` you use `as.POSIXct()` the same should be done with `today` - both variables should have the same format.
today <- as.POSIXct(Sys.Date(), "%m/%d/%Y")
Second, use `as.numeric()` inside the `geom_vline()` around `today`.
+ geom_vline(aes(xintercept=as.numeric(today)))
Problem
I'm trying to make a Gantt chart in ggplot based on the generous code offered by user Didzis Elferts. I'm trying to add a vertical line showing today's date, but the `geom_vline` layer in the `ggplot2` package simply returns `Error: Discrete value supplied to continuous scale`. Here is my code: ``` today <- as.Date(Sys.Date(), "%m/%d/%Y") library(scales) ggplot(mdfr, aes(time,name, colour = is.critical)) + geom_line(size = 6) + xlab("") + ylab("")+ labs(title="Sample Project Progress")+ theme_bw()+ scale_x_datetime(breaks=date_breaks("1 year"))+ geom_vline(aes(xintercept=today)) ``` The plot without the `geom_vline` command looks like this : Any reason why `geom_vline` wouldn't work for the "Date" character? EDIT: Reproducible code used to generate plot: ``` ### GANTT CHART 1 ###############3 tasks <- c("Meetings", "Client Calls", "Design", "Bidding", "Construction") dfr <- data.frame( name = factor(tasks, levels = tasks), start.date = c("07/08/2013", "07/08/2013", "07/23/2013", "08/30/2013", "9/30/2013"), end.date = c("07/12/2013", "07/13/2013", "08/15/2013", "09/12/2013", "12/01/2013"), is.critical = c(TRUE, FALSE, TRUE, TRUE, TRUE)) mdfr <- melt(dfr, measure.vars = c("start.date", "end.date")) mdfr$time <- as.POSIXct(strptime(mdfr$value,"%m/%d/%Y")) ```