You may have leap year issues if your series spans one or more of
them, but a simple fix is to use the yday() function from lubridate
which returns the day of the year. This isn't an ideal solution with
respect to the date labels, but here is one way to operationalize
Doug's suggestion in your example after getting rid of the orphan
observation in 2012:
library(ggplot2)
library(lubridate)
library(dplyr)
df <- data.frame(Date=seq(as.Date("1910/1/1"), as.Date("1911/12/31"), "days"),
v1=rnorm(730))
df <- df %>% mutate(year = factor(year(Date)),
doy = yday(Date))
# Comment out the legend repositioning if you don't like it
# Labels are positioned at the 15th of each month - see below for details
ggplot(df, aes(x = doy, y = v1, color = year)) +
geom_point() + geom_line() +
scale_x_continuous(breaks = yday(as.Date(paste(2011, 1:12, 15, sep = "-"))),
labels = month.abb) +
theme(legend.position = c(1, 1), legend.justification = c(1, 1)) +
xlab("Day of year")
You didn't specify whether you wanted the tick marks at the beginning,
middle or end of the month, so I opted for the 15th as a compromise:
yday(as.Date(paste(2011, 1:12, 15, sep = "-")))
Change the day to position the tick marks at whichever day of month
you desire. month.abb is a vector of three-character month labels,
which is very convenient for requests like these.
Dennis