r - Trouble with horizontal and vertical lines accepting class "Date" in ggplot? -
i'm trying make gantt chart in ggplot based on generous code offered user didzis elferts. i'm trying add vertical line showing today's date, geom_vline
layer in ggplot2
package returns error: discrete value supplied continuous scale
. here 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 geom_vline
command looks :
any reason why geom_vline
wouldn't work "date" character?
edit: reproducible code used 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"))
there 2 thinks need change in code.
first, making time
column in mdfr
use as.posixct()
same should done today
- both variables should have same format.
today <- as.posixct(sys.date(), "%m/%d/%y")
second, use as.numeric()
inside geom_vline()
around today
.
+ geom_vline(aes(xintercept=as.numeric(today)))
Comments
Post a Comment