0

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

Doo
  • 29
  • 7

3 Answers3

1

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)
MrFlick
  • 195,160
  • 17
  • 277
  • 295
1
library(lubridate)
dat$Time <- as.POSIXct(dmy(paste("01-", dat$Time , sep ="")))

library(foqat)
geom_ts_batch(dat)

enter image description here

TichPi
  • 146
  • 5
0

With complete dataset you can pass X = dat[2:9]. For 9 plots make par(mfrow=c(9,1)).

par(mar=c(1,1,1,1))
par(mfrow=c(4,1))
apply(X = dat[2:5],MARGIN = 2,FUN = plot,x=dat$Time,type="l",col="blue",xlab="Years",ylab="temperature")
tushaR
  • 3,083
  • 1
  • 20
  • 33
  • It does not take different colours of lines using the above method. Can we plot in different panels (9 panels) since some stations values are quite similar and difficult to identify trends in a single plot. – Doo Mar 10 '16 at 04:23
  • it does not take come up each series in a separate panel. – Doo Mar 10 '16 at 06:14
  • @Doo So you want all 9 panels in the same window, one below another? – tushaR Mar 10 '16 at 06:20
  • Check the edits I made. @MrFlick's solution did not work for you? – tushaR Mar 10 '16 at 06:29
  • @Doo You can play with the margin values par(mar=c(bottom,left,top,right)) so that the labels of "temperature" and "years" are displayed. Since there are 9 plots in 1 plot you can just take the bottom margin as 1 and left as 6. – tushaR Mar 10 '16 at 06:45