R - 如何创建季节性图 - 多年不同的线 [英] R - How to create a seasonal plot - Different lines for years

查看:51
本文介绍了R - 如何创建季节性图 - 多年不同的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

又来了:

我有一个这样的数据框:

原始数据框链接:

X 轴上的 1 月至 12 月、Y 轴上的值以及用不同颜色线条显示的年份.

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

然后我是这样试的:

Ref_Data$MonthN <- as.numeric(format(as.Date(Ref_Data$Date),"%m")) # 月份的数字Ref_Data$YearN <- as.numeric(format(as.Date(Ref_Data$Date),"%Y"))Ref_Data$Month <-months(as.Date(Ref_Data$Date), abbreviate=TRUE) # 月份的缩写.g <- ggplot(data = Ref_Data, aes(x = MonthN, y = DENI011, group = YearN, colour=YearN)) +geom_line() +scale_x_discrete(breaks = Ref_Data$MonthN, 标签 = Ref_Data$Month)

那也没有用,情节看起来很糟糕.我不需要将 1993 年至 2010 年的所有年份都放在 1 个情节中.实际上只有几年就可以了,比如从 1998 年到 2006 年.

和建议,如何解决这个问题?

解决方案

正如其他人所指出的,为了创建您用作示例的图,您必须首先聚合您的数据.但是,也可以在类似的图中保留每日数据.

reprex::reprex_info()#>由 reprex 包 v0.1.1.9000 创建于 2018-02-11图书馆(tidyverse)图书馆(润滑)# 导入数据网址 <- "https://megastore.uni-augsburg.de/get/JVu_V51GvQ/"原始 <- read.table(url, stringsAsFactors = FALSE)# 解析日期,并使用小写名称df <- as_tibble(raw) %>%rename_all(tolower) %>%变异(日期= ymd(日期))

实现这一点的一个技巧是将日期变量中的年份组件设置为常量,有效地将日期折叠为单个年份,然后控制轴标签,以便您不将常量年份包含在情节.

# 定义绘图p<-df%>%变异(year = factor(year(date)), # 使用年份定义单独的曲线date = update(date, year = 1) # x 轴使用常数年)%>%ggplot(aes(日期,deni011,颜色=年份))+scale_x_date(date_breaks = "1 个月", date_labels = "%b")# 每日原始数据p + geom_line()

但在这种情况下,您的每日数据变化很大,所以这有点混乱.您可以仔细研究一年,以更好地查看每日变化.

# 磨练一年p + geom_line(aes(group = year), color = "black", alpha = 0.1) +geom_line(data = function(x) filter(x, year == 2010), size = 1)

但最终,如果您想一次查看几年,那么呈现平滑的线条而不是原始的每日值可能是个好主意.或者,事实上,一些每月的汇总.

# 平滑版本p + geom_smooth(se = F)#>`geom_smooth()` 使用方法 = 'loess'#>警告:删除了 117 行包含非有限值 (stat_smooth).

I already asked the same question yesterday, but I didnt get any suggestions until now, so I decided to delete the old one and ask again, giving additional infos.

So here again:

I have a dataframe like this:

Link to the original dataframe: 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

I want to create a plot that enables me to compare the monthly values in the DENI011 over the years. So I want to have something like this:

http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html#Seasonal%20Plot

Jan-Dec on the x-scale, values on the y-scale and the years displayed by different colored lines.

I found several similar questions here, but nothing works for me. I tried to follow the instructions on the website with the example, but the problem is that I cant create a ts-object.

Then I tried it this way:

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)

That also didnt work, the plot looks horrible. I dont need to put all the years in 1 plot from 1993-2010. Actually only a few years would be ok, like from 1998-2006 maybe.

And suggestions, how to solve this?

解决方案

As others have noted, in order to create a plot such as the one you used as an example, you'll have to aggregate your data first. However, it's also possible to retain daily data in a similar plot.

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

One trick to achieve this would be to set the year component in your date variable to a constant, effectively collapsing the dates to a single year, and then controlling the axis labelling so that you don't include the constant year in the plot.

# 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()

In this case though, your daily data are quite variable, so this is a bit of a mess. You could hone in on a single year to see the daily variation a bit better.

# 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)

But ultimately, if you want to look a several years at a time, it's probably a good idea to present smoothed lines rather than raw daily values. Or, indeed, some monthly aggregate.

# Smoothed version
p + geom_smooth(se = F)
#> `geom_smooth()` using method = 'loess'
#> Warning: Removed 117 rows containing non-finite values (stat_smooth).

这篇关于R - 如何创建季节性图 - 多年不同的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆