I am trying to set up a plot object and add data to it later. I am going to set up the two years of an election cycle and add in vertical lines at the point where key events happened. But my attempt to put vertical lines on the plot at six month intervals has instead put lines the y axis.
dateline = seq(as.Date("2011/1/1"), by = "month", length.out = 24)
values = rnorm(24)
events = data.frame(dateline, values)
ggplot(events, aes(x=dateline, y=values)) +
labs(x="2012 Election Cycle")
You should use a continuous scale. This works for me:
ggplot(events, aes(x=dateline, y=values)) + scale_x_date(date_breaks="6 months")
More generally, simply:
ggplot(events, aes(x=dateline, y=values))
Will work in your case because the date range is such that
ggplot automatically picks 6 months as the range of labels to show (this is why the y continuous scale example works for you; the y thing isn't doing anything, it is the removal of the
scale_x_discrete bit that helps out).