R - 如何创建季节性图 - 多年不同的线

标签 r ggplot2 time-series

我昨天已经问过同样的问题,但直到现在我都没有得到任何建议,所以我决定删除旧的并再次询问,提供更多信息。

所以又来了:

我有一个这样的数据框:

链接到原始数据框: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
enter image description here

x 尺度上的 Jan-Dec、y 尺度上的值和用不同颜色线条显示的年份。

我在这里发现了几个类似的问题,但对我没有任何作用。我尝试按照网站上的说明进行操作,但问题是我无法创建 ts-object。

然后我这样试了一下:
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).

关于R - 如何创建季节性图 - 多年不同的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48722758/

相关文章:

r - R 中的错​​误 : could not find function . ..

python-2.7 - 用一系列非数字对象中的最近值替换 NaN?

python - python中时间序列排序和不足部分替换为NaN

r - ..x.. 在 ggplot 表示法中代表什么

r - 2 列数据的 2 种颜色的简单点图

r - 在0 :(b - 1) : numerical expression has 6 elements: only the first used中

javascript - Shiny 中的单独列搜索(选择输入) renderDatatable()

R sp : unit of area of Polygon

r - 在 switch 命令中使用可能值的列表

r - 更改 ggplot2 中闪避条形图的颜色