我試圖按月在同一個時間序列圖上繪制多個時間段。這是我的資料:
這實際上在視覺上效果很好,因為 x 軸根據需要從 10 月到 6 月;然而,我是通過設定限制來做到的,
startTime <- as.Date("2016-10-01")
endTime <- as.Date("2017-06-30")
start_end <- c(startTime,endTime)
然后設定 1 個月的休息時間。
scale_x_date(limits=start_end,breaks=date_breaks("1 month"),labels=date_format("%d %b"))
It is needless to say that this technique will not work if I would like to include other winter seasons and a legend.
I also tried to assign a season to certain time periods and then use them as a factor:
master_dataset <- master_dataset %>%
mutate(season = case_when(date>=as.Date('2015-11-02')&date<=as.Date('2016-06-30')~"season 2015-16",
date>=as.Date('2016-11-02')&date<=as.Date('2017-06-30')~"season 2016-17",
date>=as.Date('2017-10-13')&date<=as.Date('2018-06-30')~"season 2017-18",
date>=as.Date('2018-10-18')&date<=as.Date('2019-06-30')~"season 2018-19"))
ggplot(master_dataset, aes(month(date, label=TRUE, abbr=TRUE), fl_all_cumsum, group=factor(season),colour=factor(season)))
geom_line()
labs(x="Month", colour="Season")
theme_classic()

As you can see, I managed to include the other seasons in the graph but there are several issues now:
- grouped by month it aggregates the daily values and I lose the daily dynamic in the graph (look how it is based on monthly steps)
- the x-axis goes in chronological order which messes up my visualization (remember I care for the winter season development so I need the x-axis to go from October-End of June; see the first graph I produced)
- Not big of an issue but because the data has NA gaps, the legend also shows a factor "NA"
我不是程式員,所以我無法思考如何為這樣的問題編碼。在一個完美的世界中,我想要像我制作的第一個圖表一樣的東西,但包括所有的冬季和一個傳說。有人對此有解決方案嗎?提前致謝。
佐林
uj5u.com熱心網友回復:
這確實是一種痛苦,而且相當繁瑣。我創建了與您的date專欄相同的“假日期” ,但年份設定為 2015/2016(使用 2016 表示將落在 2 月的日期,因此閏日不會丟失)。然后我們繪制所有資料,告訴 ggplot 它都是 2015-2016 年,所以它被繪制在同一軸上,但我們不標記年份。(使用季節標簽,不是“假的”。)
## Configure some constants:
start_month = 10 # first month on x-axis
end_month = 6 # last month on x-axis
fake_year_start = 2015 # year we'll use for start_month-December
fake_year_end = fake_year_start 1 # year we'll use for January-end_month
fake_limits = c( # x-axis limits for plot
ymd(paste(fake_year_start, start_month, "01", sep = "-")),
ceiling_date(ymd(paste(fake_year_end, end_month, "01", sep = "-")), unit = "month")
)
df = df %>%
mutate(
## add (real) year and month columns
year = year(date),
month = month(date),
## add the year for the season start and end
season_start = ifelse(month >= start_month, year, year - 1),
season_end = season_start 1,
## create season label
season = paste(season_start, substr(season_end, 3, 4), sep = "-"),
## add the appropriate fake year
fake_year = ifelse(month >= start_month, fake_year_start, fake_year_end),
## make a fake_date that is the same as the real date
## except set all the years to the fake_year
fake_date = date,
fake_date = "year<-"(fake_date, fake_year)
) %>%
filter(
## drop irrelevant data
month >= start_month | month <= end_month,
!is.na(fl_all_cumsum)
)
ggplot(df, aes(x = fake_date, y = fl_all_cumsum, group = season,colour= season))
geom_line()
labs(x="Month", colour = "Season")
scale_x_date(
limits = fake_limits,
breaks = scales::date_breaks("1 month"),
labels = scales::date_format("%d %b")
)
theme_classic()

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/376593.html
