У вас почти все есть, вам просто нужен способ определить все даты первого января, а затем передать вектор этих дат, как x
в
abline(v=x)
lubridate и dplyr, предоставляя хорошие способы расчета это из.
# fake data: a sample of dates (in order) and a cumulative series
set.seed(123)
n <- 200
dat <- data.frame(
Date = sort(sample(seq(from=as.Date('2015-01-01'),
to=as.Date('2018-01-01'),
by='day'),
n)
),
y = cumsum(runif(n, min = -10, max=10)))
# load libraries and add flag for first jan dates
library(dplyr)
library(lubridate)
dat <- dat %>%
# ensure it's sorted by date
arrange(Date) %>%
# group by year and month
group_by(yr = year(Date), mth = month(Date)) %>%
# flag each first January row
mutate(first_jan_row = mth ==1 & row_number()==1)
Теперь вы можете построить их:
# your plot
plot(dat$Date, dat$y, type='l')
# ablines on all first jan dates
abline(v=dat$Date[dat$first_jan_row])
Результат:
![enter image description here](https://i.stack.imgur.com/21T9s.png)