使用GGPLOT2每个具有不等的若干意见多的时间序列图(Multiple time series p

2019-10-31 00:35发布

我在寻找一些帮助,多时间序列图按照下面的说明。

我有具有以下结构的数据帧。 柱ISIN被重复,它有5个唯一值。 对于每个ISIN,有由t_week,MS和t_MS的数据的多行。 每个ISIN有不等的行数。 换句话说,数据帧具有用于每个具有不同数目的数据点的伊辛2时间序列(t_week,MS)(t_week,t_MS)。

我想绘制所有的5 ISIN时间序列(t_week,MS)使用GGPLOT2一个阴谋。 我可以很容易地绘制多个时间序列的长度相等,而是寻求帮助,在这里做的是正确的“R”的方式。 请帮忙。

问候

ķ

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

Answer 1:

该canocial ggplot2方法如下:

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

导向这个构造的曲线图t_week VS MS ,具有用于在每个独特元件的不同颜色的线isin 。 这是没有问题的时间序列不包含相同的行数的,他们甚至没有覆盖相同时间范围。 一个例子:

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



文章来源: Multiple time series plots using ggplot2 with each having unequal number of observations
标签: r ggplot2