如何将特定日期设置为一年的开始日期

How to set specific date as the beginning date of the year

我想绘制流量数据的年均值使用 WATER YEAR 从 10 月开始到 9 月结束(比如 10/01/1983 到 09/30/1984,这被定义为 1984 水年) 我试图在别处寻找解决方案,但我失败了。

现在我使用以下脚本绘制年平均流量

library(tidyverse)
library(lubridate)
library(ggplot2)

#df <- read_csv('dataframe.csv')

df <- df %>% 
  mutate(date = mdy(df$date))

df <- df %>%
  mutate(year = floor_date(date, "year")) %>%
  group_by(year) %>%
  summarize(avg = mean(flow)) 


y <- df$avg
x <- as.Date(df$year, format = "Y")
d <- data.frame(x = x, y = y)

# interpolate values from zero to y and create corresponding number of x values
vals <- lapply(d$y, function(y) seq(0, y, by = 0.1))
y <- unlist(vals)
mid <- rep(d$x, lengths(vals))
d2 <- data.frame(x = mid - 100,
                 xend = mid + 100,
                 y = y,
                 yend = y)

ggplot(data = d2, aes(x = x, xend = xend, y = y, yend = yend, color = y)) +
  geom_segment(size = 2) +
  scale_color_gradient2(low = "midnightblue", mid = "deepskyblue", high = "aquamarine", 
                        midpoint = max(d2$y)/2)+
  scale_x_date(date_breaks = "1 year",date_labels = "%Y", expand = c(0,0)) +
  theme(axis.text.x = element_text(angle=90, vjust=.5))+
  labs(x = "Years", y = "Mean Annual Flow (cms)")+
  ggtitle("Mean Annual Flow, Rancho River at ELdorado (1983-2020)")+
  theme(plot.title = element_text(hjust = 0.5))

为此,我使用日历年得到了以下结果

如果我使用水年,1983 年将没有结果

数据框如下link

https://drive.google.com/file/d/11PVub9avzMFhUz02cHfceGh9DrlVQDbD/view?usp=sharing

请提供帮助。

如果date优于10/01/year(date)则说明今年是下一年(水年):

df %>%
 mutate(date=mdy(date), year=year(date), year = year + (date >= mdy(paste0("10/01/", year))))
# A tibble: 5,058 x 3
   date        flow  year
   <date>     <dbl> <dbl>
 1 1983-10-01  3.31  1984
 2 1983-10-02  3.19  1984
 3 1983-10-03  3.7   1984
 4 1983-10-04  3.83  1984
 5 1983-10-05  3.44  1984
 6 1983-10-06  4.37  1984
 7 1983-10-07  6.78  1984
 8 1983-10-08  6.3   1984
 9 1983-10-09  6.46  1984
10 1983-10-10  6.62  1984
# … with 5,048 more rows