2013-05-11 35 views
0

我正在尋找一些幫助,按照以下說明的多個時間序列圖。使用ggplot2的多個時間序列圖,每個圖具有不等數量的觀測值

我有一個數據框,具有以下結構。列isin正在重複,它有5個唯一值。對於每個isin,都有多行數據由t_week,MS和t_MS組成。每個isin具有不等數量的行。換句話說,數據幀有兩個時間序列(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 ... 
+0

請給出[再現的示例](http://stackoverflow.com/a/5963610/1412059)。 – Roland 2013-05-11 10:47:39

+0

並請向我們展示您現在使用的代碼來構建情節。 – 2013-05-11 12:10:55

回答

3

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

enter image description here

+0

這真棒@保羅。謝謝一堆。我正在嘗試各種使用ddply並融化的組合,導致不合理的情節。我沒有想到ISIN可以被看作是分類變量,ggplot最終會做到這一點。再次感謝卓越的解決方案。 – kishore 2013-05-11 13:04:06

+0

如果這樣可以解決您的問題,您可以通過點擊我答案旁邊的綠色勾號來標記。 – 2013-05-11 13:44:18

相關問題