我正在嘗試比較不同年份的變數,但無法將它們繪制在一起。時間序列是一個溫度序列,可以在
如果有人能給我一些建議或指出我正確的方向,我將不勝感激
uj5u.com熱心網友回復:
你可以試試這個方法。
第一個圖表顯示所有可用溫度,第二個圖表按月匯總。
在第一個圖表中,我們強制使用同一年,以便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")

uj5u.com熱心網友回復:
要以不同的方式訂購月份并總結幾年的值,您必須在繪制資料之前對資料進行一些處理:
library(dplyr) # work data
library(ggplot2) # plots
library(lubridate) # date
library(readr) # fetch data
# your data
df <- read_csv2("https://raw.githubusercontent.com/gonzalodqa/timeseries/main/temp.csv")
df %>%
mutate(date = make_date(year, month,day)) %>%
# reorder month
group_by(month_2 = factor(as.character(month(date, label = T, locale = Sys.setlocale("LC_TIME", "English"))),
levels = c('Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun')),
# group years as you like
year_2 = ifelse( year(date) %in% (2018:2019), '2018/2019', '2020/2021')) %>%
# you can put whatever aggregation function you need
summarise(val = mean(T42, na.rm = T)) %>%
# plot it!
ggplot(aes(x = month_2, y = val, color = year_2, group = year_2))
geom_line()
ylab('T42')
xlab('month')
theme_light()

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/329912.html
上一篇:ggplot,分兩類的條形圖
