DS4PS / ddmp-uw-class-spring-2019

Data-Driven Management & Policy Course at UW, Spring 2019
https://ds4ps.github.io/ddmp-uw-class-spring-2019/
4 stars 3 forks source link

Issue adding treatment line (x-intercept is a date) #13

Open leigh32michael opened 5 years ago

leigh32michael commented 5 years ago

Hi there, I'm running into a persistent issue around plotting trendlines or treatment lines when the Date is the x-axis.

For example, I'd like to add a vertical line at April 8, as this marks the pre-treatment/post-treatment period. However, when I enter the code below, the treatment line doesn't show up. I'm assuming it's something to do with the date format, but appreciate any advice you have. I've also uploaded my data set if that's helpful. Thanks!

df$Date <- as.Date( df$Date, '%m/%d') ggplot(data = df, aes(x = Date, y= MilesWalked)) + geom_bar(fill="darkseagreen", stat="identity") + geom_vline(xintercept=04/08,linetype="dashed", color="darkgrey") + geom_text(x = 04/15, y= 1.1, label="Treatment: Set alarm for 6:15am", color = "darkgrey") + expand_limits(y=c(0, 1.15)) + stat_smooth( aes( y = MilesWalked, x = Date), inherit.aes = FALSE, color = "darksalmon") + xlab("Date") + ylab("Distance Walked (miles)") + ggtitle("Distance Walked Before 7am") + theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "darkgrey") ) + theme(plot.title = element_text(hjust = 0.5))

MAGALLANESJoseManuel commented 5 years ago

Writing 04/08 may not be a good idea. Try as.numeric(DATEWANTED)

As Eric recommended, Google these questions...

https://stackoverflow.com/questions/5388832/how-to-get-a-vertical-geom-vline-to-an-x-axis-of-class-date

leigh32michael commented 5 years ago

Thanks, Professor. I did take Eric's advice and googled the suggestion (extensively!). I looked at the source you have here, tried it, and it did not make sense to me. I've since figured this out on my own. I thought that the class board was a fine space for us to air questions for you both. Sorry about that.

ejvanholm commented 5 years ago

Yes, these boards reach both of us. Sorry for not responding earlier, weekends get a little challenging for me and communication.

I am curious though, how did you end up solving this issue? One idea I might recommend trying is adding a column to your data for the number of days (1, 2, 3, ...n) And then just use the dates for the labels on the x-axis. That way you could use x=14 or whatever when creating your vertical line.