使用ggplot2的多个时间序列图,每个时间序列图具有不同数量的观察值 [英] Multiple time series plots using ggplot2 with each having unequal number of observations

查看:343
本文介绍了使用ggplot2的多个时间序列图,每个时间序列图具有不同数量的观察值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些有关多个时间序列图的帮助,按照以下描述。



我有一个具有以下结构的数据框。列isin正在重复,它有5个唯一值。对于每个isin,都有多行数据由t_week,MS和t_MS组成。每个isin具有不等数量的行。换句话说,数据帧有两个时间序列(t_week,MS)(t_week,t_MS),每个数据点的数据点数量不等。

我想使用ggplot2在一个图上绘制所有5个时间序列(t_week,MS)。我可以很容易地绘制多个相同长度的时间序列,但在这里寻找帮助正确的R方式。请帮助。



问候



K

  str(df)
'data.frame':95 obs。 4个变量:
$ isin:chrIN0019960056IN0019960056IN0019960056IN0019960056...
$ t_week:日期,格式:2006-01-092006-01- 16...
$ MS:num 0 0 0.01 0.86 0.54 0.23 1.55 0.07 0.29 0.79 ...
$ t_MS:num 0.14 0.14 0.14 0.75 0.35 0.31 0.63 0.28 0.54 0.52 ...


解决方案

canocial ggplot2 方式如下:

  ggplot(df,aes(x = t_week,y = MS,color = isin)) + geom_line()

这将构造一个 t_week vs MS ,对于 isin 中的每个唯一元素使用不同的颜色线。时间序列不包含相同数量的行是没有问题的,它们甚至不必覆盖相同的时间范围。一个例子:

$ $ p $ df_part1 = data.frame(t_week = seq(1,5,length = 100),MS = runif( 100),isin =A)
df_part2 = data.frame(t_week = seq(2,6,length = 500),MS = runif(500)+ 1,isin =B)
df = rbind(df_part1,df_part2)

library(ggplot2)
ggplot(df,aes(x = t_week,y = MS,color = isin))+ geom_line()


I am looking for some help with multiple time series plot as per the following description.

I have a data frame with the following structure. Column isin is repeating and it has 5 unique values. For each isin, there are multiple rows of data consisting of t_week, MS and t_MS. Each isin has unequal number of rows. In another words, the data frame has 2 time series (t_week, MS) (t_week, t_MS) for each isin with unequal number of data points.

I would like to plot all the 5 isin time series (t_week, MS) on a single plot using ggplot2. I can plot multiple time series of equal length easily, but looking for help here doing it right "The R" way. Please help.

regards

K

str(df)
'data.frame':   95 obs. of  4 variables:
 $ isin  : chr  "IN0019960056" "IN0019960056" "IN0019960056" "IN0019960056" ...
 $ t_week: Date, format: "2006-01-09" "2006-01-16" ...
 $ MS    : num  0 0 0.01 0.86 0.54 0.23 1.55 0.07 0.29 0.79 ...
 $ t_MS  : num  0.14 0.14 0.14 0.75 0.35 0.31 0.63 0.28 0.54 0.52 ...

解决方案

The canocial ggplot2 way is the following:

ggplot(df, aes(x = t_week, y = MS, color = isin)) + geom_line()

this wil construct a plot of t_week vs MS, with a differently colored line for each unique element in isin. It is no problem that the timeseries do not consist of the same number of rows, they don't even have to cover the same time range. An example:

df_part1 = data.frame(t_week = seq(1,5,length=100), MS = runif(100), isin = "A")
df_part2 = data.frame(t_week = seq(2,6,length=500), MS = runif(500) + 1, isin = "B")
df = rbind(df_part1, df_part2)

library(ggplot2)
ggplot(df, aes(x = t_week, y = MS, color = isin)) + geom_line()

这篇关于使用ggplot2的多个时间序列图,每个时间序列图具有不同数量的观察值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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