r - 将不同年份的时间序列绘制在一起
问题描述
我正在尝试比较不同年份的变量,但我无法将它们绘制在一起。时间序列是一个温度序列,可以在https://github.com/gonzalodqa/timeseries as temp.csv 中找到合并同一个月下同一情节中的线条
如果有人能给我一些建议或指出我正确的方向,我将不胜感激
解决方案
你可以试试这个方法。
第一个图表显示所有可用温度,第二个图表按月汇总。
在第一个图表中,我们强制使用同一年份,以便ggplot
将它们对齐,但我们按颜色分隔线条。
对于第二个,我们只使用month
as x
variable 和year
as colour
variable。
注意:
- 有了
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")
推荐阅读
- mysql - 会话变量不同于 MariaDB 中的全局变量
- c# - 如何通过机器人真正向 Teams 中的用户发送消息
- ms-access - 我可以创建“链接”访问对象(查询、宏等)(类似于“链接表”)吗?
- asp.net-core - .Net Core WebApp 需要清除站点数据才能运行
- advanced-custom-fields - 高级自定义字段 (ACF) 中继器 + 联系表 7
- mysql - mysql:与子查询连接时重复的列名
- angular - 将 'as' 与 *ngIf 一起使用
- python - 计算字符串中出现的字符数
- reactjs - 如何将以下内容映射为预期结果的反应状态
- java - 未找到 [SaajSoapMessage {www.example.org/documentRequest] 的端点映射