简体   繁体   English

R中绘制多元时间序列的问题

[英]issues plotting multivariate time series in R

I have a time-series of 10 objects measured along 12 hours to monitor a specific variable. 我有一个时间序列,在12个小时内测量了10个对象,以监视特定变量。 The time-series is stored in a data frame like this: 时间序列存储在这样的数据帧中:

> myTS
    hr1  hr2  hr3  hr4  hr5  hr6  hr7  hr8  hr9 hr10 hr11 hr12
1    43  108   71   64  112   46  115  375  187  163   55  190
2   153  123  110  141  107  139  105  137  126  277  316   48
3   159   65   65   69   70   73   65   66   65   66  139   90
4   310  300  256  251   25  208  219  180   63  134  454  351
5   183  225   20  313  245   30  267  345  279  330  36    88

I'm trying to plot these on a single plot window with the time on the x axis and values on the y axis for each of the 5 objects, and differentiate the 5 lines using different colors. 我正在尝试在单个绘图窗口上绘制这些图形,并为5个对象分别在x轴上的时间和y轴上的值,并使用不同的颜色区分5条线。 I have tried the plot.ts() function but it gives an error saying: 我已经试过plot.ts()函数,但是给出了一个错误信息:

Error in plotts(x = x, y = y, plot.type = plot.type, xy.labels = xy.labels,  : 
cannot plot more than 10 series as "multiple"

Then I tried the lines() function but still doesn't work. 然后,我尝试了lines()函数,但仍然无法正常工作。 Any help please??!! 有任何帮助吗?

Following may be useful: 以下可能有用:

ddf$id = rownames(ddf)
ddf
  hr1 hr2 hr3 hr4 hr5 hr6 hr7 hr8 hr9 hr10 hr11 hr12 id
1  43 108  71  64 112  46 115 375 187  163   55  190  1
2 153 123 110 141 107 139 105 137 126  277  316   48  2
3 159  65  65  69  70  73  65  66  65   66  139   90  3
4 310 300 256 251  25 208 219 180  63  134  454  351  4
5 183 225  20 313 245  30 267 345 279  330   36   88  5

library(reshape2)    
mm = melt(ddf, id='id')

library(ggplot2)
ggplot(mm)+geom_line(aes(x=variable, y=value, group=id, color=id))

在此处输入图片说明

If DF is the data frame here are a few ways: 如果DF是数据帧,则有以下几种方法:

1) autoplot.zoo 1)autoplot.zoo

library(zoo)
library(ggplot2)
autoplot(zoo(t(DF)), facets = NULL)

autoplot.zoo屏幕截图

2) ts.plot 2)ts.plot

ts.plot(t(DF), col = 1:5)
legend("topleft", legend = 1:5, col = 1:5, lty = 1)

ts.plot屏幕截图

3) matplot 3)Matplot

matplot(t(DF), type = "o")

matplot屏幕截图

Note: We used this as DF (Next time please output your data using dput ). 注意:我们将其用作DF (下次请使用dput输出数据)。

DF <-  structure(list(hr1 = c(43L, 153L, 159L, 310L, 183L), hr2 = c(108L, 
123L, 65L, 300L, 225L), hr3 = c(71L, 110L, 65L, 256L, 20L), hr4 = c(64L, 
141L, 69L, 251L, 313L), hr5 = c(112L, 107L, 70L, 25L, 245L), 
    hr6 = c(46L, 139L, 73L, 208L, 30L), hr7 = c(115L, 105L, 65L, 
    219L, 267L), hr8 = c(375L, 137L, 66L, 180L, 345L), hr9 = c(187L, 
    126L, 65L, 63L, 279L), hr10 = c(163L, 277L, 66L, 134L, 330L
    ), hr11 = c(55L, 316L, 139L, 454L, 36L), hr12 = c(190L, 48L, 
    90L, 351L, 88L)), .Names = c("hr1", "hr2", "hr3", "hr4", 
"hr5", "hr6", "hr7", "hr8", "hr9", "hr10", "hr11", "hr12"), 
class = "data.frame", row.names = c("1", "2", "3", "4", "5"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM