Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
leigh32michael opened this issue Jun 8, 2019 · 3 comments
Open

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

leigh32michael opened this issue Jun 8, 2019 · 3 comments

Comments

@leigh32michael
Copy link

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants