[R] plot time series data in wide format

Pete Brecknock Peter.Brecknock at bp.com
Sat Nov 2 02:17:22 CET 2013


wudadan wrote
> Dear R users,
> 
> I wonder if there is a way that I can plot a time series data which is in
> a
> wide format like this:
> 
> CITY_NAME       2000Q1    2000Q2      2000Q3        2000Q4     2001Q1
> 2001Q2      2001Q3     2001Q4     2002Q1      2002Q2
> CITY1                100.5210   101.9667  103.24933   104.0506   104.4317
> 105.3921   106.7643   107.5202   107.2561   107.8184
> CITY2                100.0412   100.6146  103.20293   104.0867   104.6612
> 106.6126   109.3514   110.1943   110.9480   113.0071
> CITY3                 99.5895    99.2298   99.26947    99.4101   100.5776
> 101.3719   101.5957   102.2411   103.4390   105.1745
> CITY4                 99.6491   101.5386  104.90953   106.1065   108.1785
> 110.6845   113.3746   114.1254   116.2121   119.1033
> CITY5                100.9828   103.6847  105.04793   106.5925   108.7437
> 110.5549   111.9343   112.6704   113.6201   115.3020
> 
> Ideally, each city of the five city is represented by a line in the plot.
> 
> Any suggestion is appreciated!
> 
> Thanks!
> Gary
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________

> R-help@

>  mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

How about using the zoo package ....

library(zoo)

# Read Data
text <- "CITY_NAME  2000Q1 2000Q2  2000Q3  2000Q4  2001Q1 2001Q2  2001Q3 
2001Q4  2002Q1 2002Q2 
CITY1 100.5210 101.9667 103.24933 104.0506 104.4317 105.3921 106.7643
107.5202 107.2561 107.8184 
CITY2 100.0412 100.6146 103.20293 104.0867 104.6612 106.6126 109.3514
110.1943 110.9480 113.0071 
CITY3  99.5895  99.2298  99.26947  99.4101 100.5776 101.3719 101.5957
102.2411 103.4390 105.1745 
CITY4  99.6491 101.5386 104.90953 106.1065 108.1785 110.6845 113.3746
114.1254 116.2121 119.1033 
CITY5 100.9828 103.6847 105.04793 106.5925 108.7437 110.5549 111.9343
112.6704 113.6201 115.3020"

df <- read.table(textConnection(text), header=TRUE, check.names=FALSE)

#Create zoo object
d <- t(df[,-1])
ind <- as.yearqtr(names(df)[-1]) 
z <- zoo(d,ind)

# Plot
plot(z, plot.type="single", col=1:5, lwd=2)
legend("topleft",legend=c("City1","City2","City3","City4","City5"),lty=1,
lwd=2, col=1:5)

HTH

Pete




--
View this message in context: http://r.789695.n4.nabble.com/plot-time-series-data-in-wide-format-tp4679589p4679591.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list