Search code examples
rplotzootimeserieschart

Plot multiple time series in different panels using R


I am trying to plot time series monthly mean temperature data of 9 stations (A to I) putting in 9 different panels. But I am using zoo to keep the month-year format. My simple data set looks like below.

 +--------+------+------+------+------+
|  Time  |  A   |  B   |  C   |  D   |
+--------+------+------+------+------+
| Jan-84 | 28.2 | 28.2 | 27.5 | 18.4 |
| Feb-84 | 29.5 | 26.6 | 27.9 | 17.4 |
| Mar-84 | 30.7 | 30.3 | 30.1 | 19.5 |
| Apr-84 | 30.5 | 33.2 | 29.9 | 20.7 |
| May-84 | 33.2 | 30.1 | 30.2 | 21   |
| Jun-84 | 31.6 | 28.3 | 28.5 | 16.9 |
| Jul-84 | 31.5 | 28.6 | 27.7 | 16.8 |
| Aug-84 | 32.5 | 28.9 | 27.4 | 18.5 |
| Sep-84 | 34   | 28.1 | 29.4 | 18.3 |
| Oct-84 | 32.8 | 28.8 | 28.8 | 17.2 |
| Nov-84 | 28.9 | 31.6 | 29   | 17.8 |
| Dec-84 | 30.6 | 26.9 | 28.1 | 18.9 |
| Jan-85 | 31.8 | 28.6 | 29.3 | 18.2 |
| Feb-85 | 31.3 | 29.6 | 30.4 | 19.5 |
| Mar-85 | 32   | 31.1 | 31.4 | 19.7 |
+--------+------+------+------+------+

This data set can be accessed through following codes.

structure(list(Time = structure(c(6L, 4L, 10L, 1L, 12L, 9L, 8L, 
2L, 15L, 14L, 13L, 3L, 7L, 5L, 11L), .Label = c("Apr-84", "Aug-84", 
"Dec-84", "Feb-84", "Feb-85", "Jan-84", "Jan-85", "Jul-84", "Jun-84", 
"Mar-84", "Mar-85", "May-84", "Nov-84", "Oct-84", "Sep-84"), class =     "factor"), 
A = c(28.2, 29.5, 30.7, 30.5, 33.2, 31.6, 31.5, 32.5, 34, 
32.8, 28.9, 30.6, 31.8, 31.3, 32), B = c(28.2, 26.6, 30.3, 
33.2, 30.1, 28.3, 28.6, 28.9, 28.1, 28.8, 31.6, 26.9, 28.6, 
29.6, 31.1), C = c(27.5, 27.9, 30.1, 29.9, 30.2, 28.5, 27.7, 
27.4, 29.4, 28.8, 29, 28.1, 29.3, 30.4, 31.4), D = c(18.4, 
17.4, 19.5, 20.7, 21, 16.9, 16.8, 18.5, 18.3, 17.2, 17.8, 
18.9, 18.2, 19.5, 19.7)), .Names = c("Time", "A", "B", "C", 
"D"), class = "data.frame", row.names = c(NA, -15L))

I used the following code to get the data to zoo structure and then plot. library(zoo)

dat$Time <- as.yearmon(dat$Time,format="%b-%y")

And then the following code for plotting.

plot(dat, col=Rainbow)

I need to label years in X axis and temperature values in Y axes. I found some ways to plot this but these examples are available when the series are in separate ts objects and plotting after combining. But data are available in the data frame as variables. I am new to R and appreciated if anybody can help me. Thanks


Solution

  • I think you'll get better results if you make a proper zoo object. For example, using your original dat

    zz <- zoo(dat[-1], as.yearmon(dat$Time,format="%b-%y"))
    plot(zz)