本教程将演示以下ggplot2可视化方法:

基础时间序列图(折现图、面积图)

季节性分解图

日历热力图

螺旋图


1. 环境准备

加载必要包

library(tidyverse)    # 数据处理与可视化
library(lubridate)    # 日期处理
library(forecast)     # 时间序列分解
library(viridis)      # 优化图标颜色
library(patchwork)    # 多图组合

2. 数据准备

生成模拟数据

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> 周日, 周一, 周二, 周三, 周四, 周五, 周六, 周日, 周一, 周二, 周三, 周四, 周五, 周六, 周日…

3. 基础时间序列图

基础折线图

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()


4. 季节性分析

季节性分解图

# 时间序列分解
ts_obj <- ts(ts_data$sales, frequency = 7)
decomp <- stl(ts_obj, s.window = "periodic")

# 转换为ggplot格式
autoplot(decomp) +
  labs(title = "销售额季节性分解(STL方法)") +
  theme_minimal()


5. 高级技巧

重点日期标注

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()


6. 注意事项

  1. 日期格式处理:确保日期列转换为Date类型
  2. 时间频率标识:明确时间单位(日/周/月)
  3. 颜色选择:时间序列建议使用渐变色,分类变量使用离散色板
  4. 图表信息量:避免过度装饰,突出核心趋势