(1) 首先需要将month和year转换为时间格式,然后使用ggplot2绘制散点图:

library(tidyverse)
library(lubridate)

Utilities %>% 
  mutate(date = make_date(year, month, 1)) %>% 
  ggplot(aes(x = date, y = Gas.ccf)) +
  geom_point()

(2) 使用颜色区分不同月份,可以使用scale_color_gradient()函数来设置颜色,也可以使用geom_line()来连接点:

Utilities %>% 
  mutate(date = make_date(year, month, 1)) %>% 
  ggplot(aes(x = date, y = Gas.ccf, color = month)) +
  geom_point() +
  scale_color_gradient(low = 'blue', high = 'red') # 使用蓝色到红色的渐变色

# 或者使用geom_line()连接点
Utilities %>% 
  mutate(date = make_date(year, month, 1)) %>% 
  ggplot(aes(x = date, y = Gas.ccf, color = month)) +
  geom_line() +
  scale_color_gradient(low = 'blue', high = 'red')

(3) 绘制箱线图可以使用geom_boxplot()函数,同时需要使用scale_x_discrete()函数将月份变为因子:

Utilities %>% 
  ggplot(aes(x = factor(month), y = Gas.ccf)) +
  geom_boxplot() +
  scale_x_discrete(name = 'Month', labels = month.name)

从箱线图可以看出,1月和2月的气体使用量变化最大。

(4) 可以使用时间序列图来观察气体使用量随时间的变化情况。可以使用geom_smooth()函数来拟合出气体使用量的趋势线:

Utilities %>% 
  mutate(date = make_date(year, month, 1)) %>% 
  ggplot(aes(x = date, y = Gas.ccf)) +
  geom_point() +
  geom_smooth(method = 'lm')

从趋势线可以看出,气体使用量随时间呈现出下降的趋势。

R语言探索住宅天然气使用量趋势:时间序列分析和箱线图可视化

原文地址: https://www.cveoy.top/t/topic/n86M 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录