首页 > 解决方案 > 将不同年份的时间序列绘制在一起

问题描述

我正在尝试比较不同年份的变量,但我无法将它们绘制在一起。时间序列是一个温度序列,可以在https://github.com/gonzalodqa/timeseries as temp.csv 中找到合并同一个月下同一情节中的线条 在此处输入图像描述

如果有人能给我一些建议或指出我正确的方向,我将不胜感激

标签: rggplot2plottime-series

解决方案


你可以试试这个方法。

第一个图表显示所有可用温度,第二个图表按月汇总。

在第一个图表中,我们强制使用同一年份,以便ggplot将它们对齐,但我们按颜色分隔线条。

对于第二个,我们只使用monthas xvariable 和yearas colourvariable。

注意:

  • 有了scale_x_datetime我们可以隐藏年份,这样没有人可以看到我们强迫 2020 年进行每一次观察
  • scale_x_continous我们可以显示月份的名称而不是数字

[试着在有和没有scale_x_...理解我在说什么的情况下运行图表]

month.abb是月份名称的有用默认变量。

# read data
df <- readr::read_csv2("https://raw.githubusercontent.com/gonzalodqa/timeseries/main/temp.csv")


# libraries
library(ggplot2)
library(dplyr)


# line chart by datetime
df %>% 
  # make datetime: force unique year
  mutate(datetime = lubridate::make_datetime(2020, month, day, hour, minute, second)) %>% 
  
  ggplot() +
  geom_line(aes(x = datetime, y = T42, colour = factor(year))) +
  scale_x_datetime(breaks = lubridate::make_datetime(2020,1:12), labels = month.abb) +
  labs(title = "Temperature by Datetime", colour = "Year")

# line chart by month
df %>% 
  
  # average by year-month
  group_by(year, month) %>% 
  summarise(T42 = mean(T42, na.rm = TRUE), .groups = "drop") %>% 
  
  ggplot() +
  geom_line(aes(x = month, y = T42, colour = factor(year))) +
  scale_x_continuous(breaks = 1:12, labels = month.abb, minor_breaks = NULL) +
  labs(title = "Average Temperature by Month", colour = "Year")


如果您希望图表从 7 月开始,您可以使用以下代码:

months_order <- c(7:12,1:6)

# line chart by month
df %>% 
  
  # average by year-month
  group_by(year, month) %>% 
  summarise(T42 = mean(T42, na.rm = TRUE), .groups = "drop") %>% 
    
  # create new groups starting from each July
  group_by(neworder = cumsum(month == 7)) %>% 
    
  # keep only complete years
  filter(n() == 12) %>% 
    
  # give new names to groups
  mutate(years = paste(unique(year), collapse = " / ")) %>% 
  ungroup() %>% 
  
  # reorder months
  mutate(month = factor(month, levels = months_order, labels = month.abb[months_order], ordered = TRUE)) %>% 
      
  # plot
  ggplot() +
  geom_line(aes(x = month, y = T42, colour = years, group = years)) +
  labs(title = "Average Temperature by Month", colour = "Year")


编辑

要获得类似于第一个情节但从 7 月开始的内容,您可以使用以下代码:

# libraries
library(ggplot2)
library(dplyr)
library(lubridate)


# custom months order
months_order <- c(7:12,1:6)

# fake dates for plot
# note: choose 4 to include 29 Feb which exist only in leap years
dates <- make_datetime(c(rep(3,6), rep(4,6)), months_order)

# line chart by datetime
df %>%
  
  # create date time
  mutate(datetime = make_datetime(year, month, day, hour, minute, second)) %>%
  
  # filter years of interest
  filter(datetime >= make_datetime(2018,7), datetime < make_datetime(2020,7)) %>%
  
  # create increasing group after each july
  group_by(year, month) %>%
  mutate(dummy = month(datetime) == 7 & datetime == min(datetime)) %>%
  ungroup() %>%
  mutate(dummy = cumsum(dummy)) %>%
  
  # force unique years and create custom name
  group_by(dummy) %>%
  mutate(datetime = datetime - years(year - 4) - years(month>=7),
         years = paste(unique(year), collapse = " / ")) %>%
  ungroup() %>%
  
  # plot
  ggplot() +
  geom_line(aes(x = datetime, y = T42, colour = years)) +
  scale_x_datetime(breaks = dates, labels = month.abb[months_order]) +
  labs(title = "Temperature by Datetime", colour = "Year")


推荐阅读