简体   繁体   中英

Plot Vector of Points in Time Series Data Matrix

I have a 100*8 data matrix where each row is a vector of values at 8 different time points. I am interested to know how to plot the following matrix in R to get the graph closely similar to the one below:

在此处输入图片说明

Here is an example of my data matrix.

           1    2        3     4    5    6    7      8
line1     0.22  0.075  0.35  0.89   0   0.35  0.42  2.34   
line2      0    0.47   0.89  2.51   0   0.36  1.14  2.09
line3     1.22  0.075  0.35  0.89   0   0.35  0.42  1.34
line4     2.22  0.75   0.45  0.99   0   0.54  0.24  2.34
line5     3.22  0.275  0.55  0.819  0   0.25  0.34  2.34

Any help or advice would be highly appreciated. Thanks.

Try matplot() . By default it treats columns as the series so we need to transpose ( t() ) the data frame before use. Here is an example using the subset of data you supplied

timeser <- read.table(text = "           1    2        3     4    5    6    7      8
line1     0.22  0.075  0.35  0.89   0   0.35  0.42  2.34   
line2      0    0.47   0.89  2.51   0   0.36  1.14  2.09
line3     1.22  0.075  0.35  0.89   0   0.35  0.42  1.34
line4     2.22  0.75   0.45  0.99   0   0.54  0.24  2.34
line5     3.22  0.275  0.55  0.819  0   0.25  0.34  2.34", header = TRUE)

matplot(t(timeser), type = "l")

Producing

在此处输入图片说明

You can convert you object to zoo object and then use plot.zoo to get desired plot. zoo is a timeseries class.

> mat
       X1    X2   X3    X4 X5   X6   X7   X8
[1,] 0.22 0.075 0.35 0.890  0 0.35 0.42 2.34
[2,] 0.00 0.470 0.89 2.510  0 0.36 1.14 2.09
[3,] 1.22 0.075 0.35 0.890  0 0.35 0.42 1.34
[4,] 2.22 0.750 0.45 0.990  0 0.54 0.24 2.34
[5,] 3.22 0.275 0.55 0.819  0 0.25 0.34 2.34

> plot.zoo(zoo(t(mat), order.by=1:ncol(mat)), screens = 1, col = rainbow(ncol(t(mat))), ylab="Data")

This will give

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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