You can use the sec.axis argument in ggplot2::scale_x_date to show the years opposite the dates:
library(ggplot2)
library(dplyr)
df <- data.frame(stringsAsFactors = FALSE,
Date = c("Jan-09", "Feb-09", "Mar-09", "Apr-09", "May-09",
"Jun-09", "Jul-09", "Aug-09", "Sep-09", "Oct-09",
"Nov-09", "Dec-09", "Jan-10", "Feb-10", "Mar-10",
"Apr-10", "May-10", "Jun-10", "Jul-10", "Aug-10", "Sep-10",
"Oct-10", "Nov-10", "Dec-10", "Jan-11", "Feb-11",
"Mar-11", "Apr-11", "May-11", "Jun-11", "Jul-11",
"Aug-11", "Sep-11", "Oct-11", "Nov-11", "Dec-11"),
Rainfall = c(3.1, 0.2, 1.2, 4.1, 3.5, 1.2, 2.7, 6.1, 6.4, 5.7, 3.1, 5.1,
1.8, 3.4, 7.5, 3.2, 7.3, 6.4, 6.8, 3.9, 3.9, 6.6, 1.4, 0.6,
0.7, 0.3, 3.2, 3.2, 4.9, 2.8, 3.8, 10, 5.6, 5.6, 3.9, 1.3))
df <- df %>%
mutate(
Year = 2000 + as.numeric(gsub("\\D", "", Date)),
Month = factor(substr(Date, 1, 3), levels = month.abb, ordered = TRUE)
) %>%
mutate(
DateVar = as.Date(paste0(Date, " 01"), format = "%b-%y %d")
) %>%
arrange(DateVar)
head(df)
# Date Rainfall Year Month DateVar
# 1 Jan-09 3.1 2009 Jan 2009-01-01
# 2 Feb-09 0.2 2009 Feb 2009-02-01
# 3 Mar-09 1.2 2009 Mar 2009-03-01
# 4 Apr-09 4.1 2009 Apr 2009-04-01
# 5 May-09 3.5 2009 May 2009-05-01
# 6 Jun-09 1.2 2009 Jun 2009-06-01
ggplot(df, aes(x = DateVar, y = Rainfall)) +
geom_line() +
geom_vline(xintercept = df[["DateVar"]][df[["Month"]] == "Jan"]) +
scale_x_date(
breaks = df[["DateVar"]],
labels = df[["Month"]],
sec.axis = dup_axis(
breaks = df[["DateVar"]][df[["Month"]] == "Jul"],
labels = df[["Year"]][df[["Month"]] == "Jul"]
)
)
Of course, it can be made prettier with different colors, line types, and theme settings.