[英]R - How to create a seasonal plot - Different lines for years
我昨天已經問了同樣的問題,但直到現在我還沒有得到任何建議,所以我決定刪除舊的,再次詢問,給予額外的信息。
再來一次:
我有這樣的數據幀:
鏈接到原始數據框: https : //megastore.uni-augsburg.de/get/JVu_V51GvQ/
Date DENI011
1 1993-01-01 9.946
2 1993-01-02 13.663
3 1993-01-03 6.502
4 1993-01-04 6.031
5 1993-01-05 15.241
6 1993-01-06 6.561
....
....
6569 2010-12-26 44.113
6570 2010-12-27 34.764
6571 2010-12-28 51.659
6572 2010-12-29 28.259
6573 2010-12-30 19.512
6574 2010-12-31 30.231
我想創建一個圖表,使我能夠比較多年來DENI011中的月度值。 所以我想要這樣的東西:
http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html#Seasonal%20Plot
1月至12月的x尺度,y尺度的值和不同顏色線顯示的年份。
我在這里找到了幾個類似的問題,但對我來說沒什么用。 我試圖按照網站上的說明進行示例,但問題是我無法創建一個ts對象。
然后我這樣試了:
Ref_Data$MonthN <- as.numeric(format(as.Date(Ref_Data$Date),"%m")) # Month's number
Ref_Data$YearN <- as.numeric(format(as.Date(Ref_Data$Date),"%Y"))
Ref_Data$Month <- months(as.Date(Ref_Data$Date), abbreviate=TRUE) # Month's abbr.
g <- ggplot(data = Ref_Data, aes(x = MonthN, y = DENI011, group = YearN, colour=YearN)) +
geom_line() +
scale_x_discrete(breaks = Ref_Data$MonthN, labels = Ref_Data$Month)
這也沒有用,情節看起來很糟糕。 從1993年到2010年,我不需要將所有年份都放在1個地塊中。 實際上只有幾年就可以了,比如1998-2006。
和建議,如何解決這個問題?
正如其他人所指出的那樣,為了創建一個例如您用作示例的圖,您必須首先聚合您的數據。 但是,也可以在類似的情節中保留每日數據。
reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2018-02-11
library(tidyverse)
library(lubridate)
# Import the data
url <- "https://megastore.uni-augsburg.de/get/JVu_V51GvQ/"
raw <- read.table(url, stringsAsFactors = FALSE)
# Parse the dates, and use lower case names
df <- as_tibble(raw) %>%
rename_all(tolower) %>%
mutate(date = ymd(date))
實現此目的的一個技巧是將日期變量中的年份組件設置為常量,有效地將日期折疊為一年,然后控制軸標簽,以便您不在繪圖中包含常量年份。
# Define the plot
p <- df %>%
mutate(
year = factor(year(date)), # use year to define separate curves
date = update(date, year = 1) # use a constant year for the x-axis
) %>%
ggplot(aes(date, deni011, color = year)) +
scale_x_date(date_breaks = "1 month", date_labels = "%b")
# Raw daily data
p + geom_line()
但在這種情況下,您的日常數據變化很大,所以這有點亂。 您可以在一年內磨練,以便更好地了解每日變化。
# Hone in on a single year
p + geom_line(aes(group = year), color = "black", alpha = 0.1) +
geom_line(data = function(x) filter(x, year == 2010), size = 1)
但最終,如果你想要一次看幾年,那么提出平滑的線條而不是原始的每日價值可能是一個好主意。 或者,確實是一些月度匯總。
# Smoothed version
p + geom_smooth(se = F)
#> `geom_smooth()` using method = 'loess'
#> Warning: Removed 117 rows containing non-finite values (stat_smooth).
從一個月開始有多個值,因此在繪制原始數據時,您在一個月內獲得了多個積分。 因此,這條線看起來很奇怪。
如果您想創建與您提供的示例類似的內容,則必須按年份和月份匯總數據。 下面我計算了數據的每年和每月的平均值。 此外,如果要將其繪制為離散變量,則需要將年和月轉換為因子。
library(dplyr)
Ref_Data2 <- Ref_Data %>%
group_by(MonthN, YearN, Month) %>%
summarize(DENI011 = mean(DENI011)) %>%
ungroup() %>%
# Convert the Month column to factor variable with levels from Jan to Dec
# Convert the YearN column to factor
mutate(Month = factor(Month, levels = unique(Month)),
YearN = as.factor(YearN))
g <- ggplot(data = Ref_Data2,
aes(x = Month, y = DENI011, group = YearN, colour = YearN)) +
geom_line()
g
如果您不想添加library(dplyr)
,這是基本R代碼。 與www的答案完全相同的策略和結果。
dat <- read.delim("~/Downloads/df1.dat", sep = " ")
dat$Date <- as.Date(dat$Date)
dat$month <- factor(months(dat$Date, TRUE), levels = month.abb)
dat$year <- gsub("-.*", "", dat$Date)
month_summary <- aggregate(DENI011 ~ month + year, data = dat, mean)
ggplot(month_summary, aes(month, DENI011, color = year, group = year)) +
geom_path()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.