library(tidyverse) # 数据处理与可视化
library(lubridate) # 日期处理
library(forecast) # 时间序列分解
library(viridis) # 优化图标颜色
library(patchwork) # 多图组合
set.seed(123)
dates <- seq.Date(as.Date("2023-01-01"), by = "day", length.out = 365)
ts_data <- tibble(
date = dates,
sales = 100 + 5 * sin(seq(0, 4*pi, length=365)) + rnorm(365, sd=3),
temperature = 20 + 10 * sin(seq(0, 2*pi, length=365)) + rnorm(365, sd=2)
)
# 添加周特征
ts_data <- ts_data %>%
mutate(weekday = wday(date, label = TRUE))
glimpse(ts_data)
## Rows: 365
## Columns: 4
## $ date <date> 2023-01-01, 2023-01-02, 2023-01-03, 2023-01-04, 2023-01-0…
## $ sales <dbl> 98.31857, 99.48205, 105.02108, 100.72844, 101.07613, 106.0…
## $ temperature <dbl> 19.58041, 20.92894, 18.45434, 22.23146, 19.76783, 25.69555…
## $ weekday <ord> 周日, 周一, 周二, 周三, 周四, 周五, 周六, 周日, 周一, 周二, 周三, 周四, 周五, 周六, 周日…
ggplot(ts_data, aes(x = date, y = sales)) +
geom_line(color = "steelblue", linewidth = 0.8) +
labs(title = "2023年每日销售额趋势",
x = "日期", y = "销售额(万元)") +
theme_minimal(base_size = 12)
ggplot(ts_data, aes(x = date, y = sales)) +
geom_line(color = "grey70") +
geom_smooth(method = "loess", span = 0.2,
color = "red", se = FALSE) +
labs(title = "销售额趋势线(局部加权回归)") +
theme_bw()
ggplot(ts_data, aes(x = date, y = sales)) +
geom_area(fill = "#56B4E9", alpha = 0.6) +
geom_line(color = "#0072B2") +
labs(title = "时间序列面积图", x = "日期", y = "销售额(万元") +
theme_minimal()
# 时间序列分解
ts_obj <- ts(ts_data$sales, frequency = 7)
decomp <- stl(ts_obj, s.window = "periodic")
# 转换为ggplot格式
autoplot(decomp) +
labs(title = "销售额季节性分解(STL方法)") +
theme_minimal()
special_dates <- ts_data %>%
filter(sales > quantile(sales, 0.95))
ggplot(ts_data, aes(x = date, y = sales)) +
geom_line(color = "grey60") +
geom_point(data = special_dates,
color = "red", size = 2) +
geom_text(data = special_dates,
aes(label = format(date, "%m/%d")),
vjust = -1, size = 3) +
labs(title = "高销售额日期标注")
ts_data <- ts_data %>%
mutate(month = month(date, label = TRUE),
week = isoweek(date))
ggplot(ts_data, aes(x = week, y = month, fill = sales)) +
geom_tile(color = "white") +
scale_fill_viridis_c(option = "magma") +
labs(title = "销售额周-月热力图")
sample<-data.frame(
date=seq.Date(from=as.Date("1993-01-01"),
to=as.Date("1996-12-31"),by=1),
day_num=1:1461,
temp=rnorm(1461,10,2)
)
ggplot(sample,aes(day_num %% 365,0.05*day_num+temp/2,
height=temp,fill=temp))+
geom_tile()+
scale_y_continuous(limits=c(-20,NA))+
scale_x_continuous(breaks=30*0:11,minor_breaks=NULL,
labels=month.abb)+
coord_polar()+
scale_fill_viridis_c()+
theme_minimal()
Date类型