I'm having problems ploting vertical lines with ggplot in R.
I want to draw a vertical line each Sunday on my time serie:
VisitDate VisitMonth VisitYear City Weekday VisitWeek Code_CxF Centre Location
1 2014-05-02 05 2014 Barcelona 05Friday 2014-04-28 CxF_BCN CaixaForum Barcelona Catalunya
2 2014-05-03 05 2014 Barcelona 06Saturday 2014-04-28 CxF_BCN CaixaForum Barcelona Catalunya
3 2014-05-04 05 2014 Barcelona 07Sunday 2014-04-28 CxF_BCN CaixaForum Barcelona Catalunya
4 2014-05-06 05 2014 Barcelona 02Tuesday 2014-05-05 CxF_BCN CaixaForum Barcelona Catalunya
5 2014-05-07 05 2014 Barcelona 03Wednesday 2014-05-05 CxF_BCN CaixaForum Barcelona Catalunya
6 2014-05-08 05 2014 Barcelona 04Thursday 2014-05-05 CxF_BCN CaixaForum Barcelona Catalunya
I have tried the following expression:
ggplot(data = MUSEUS_PLOT, aes(x = VisitDate, y = Visitors.x)) +
geom_line(size=0.25, aes(x = VisitDate, y = Visitors.x, colour = "CxF")) +
geom_line(aes(y = Visitors.y, colour = "MNAC")) +
labs(title = "MONTJUICH May 2014", x = "Day", y = "Visitors") +
scale_colour_manual(name="Legend",values=c(CxF="darkcyan", MNAC="darkorange2")) +
geom_vline(aes(xintercept = which(weekdays(MUSEUS_PLOT$VisitDate) == "Sunday")))
But it is not working:
Error in data.frame(xintercept = c(3L, 9L, 15L, 21L), PANEL = c(1L, 1L, :
arguments imply differing number of rows: 4, 26
Some ideas?
Create a new data table with the sundays data:
MUSEUS_PLOT_SUNDAYS <- MUSEUS_PLOT[weekdays(MUSEUS_PLOT$VisitDate) == "Sunday"]
And change the geom_vline for this:
geom_vline(data = MUSEUS_PLOT_SUNDAYS,aes(xintercept = as.numeric(VisitDate)),colour = "black")