gpt4 book ai didi

r - 将不同年份的时间序列绘制在一起

转载 作者:行者123 更新时间:2023-12-03 08:15:11 26 4
gpt4 key购买 nike

我正在尝试比较不同年份的变量,但我无法将它们绘制在一起。时间序列是一个温度序列,可以在 https://github.com/gonzalodqa/timeseries 中找到。作为临时.csv我想绘制类似图像的内容,但我发现很难对年份之间的月份进行子集化,然后将同一月份下同一图中的线条组合起来 enter image description here

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

最佳答案

你可以试试这个方法。

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

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

对于第二个,我们只使用 monthx变量和yearcolour变量。

请注意:

  • 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")

关于r - 将不同年份的时间序列绘制在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69658051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com