[R] How to import time-series data

Gabor Grothendieck ggrothendieck at gmail.com
Mon Feb 13 17:54:01 CET 2012


On Mon, Feb 13, 2012 at 8:23 AM, RichardSmith <richardsmith404 at gmail.com> wrote:
>
> Gabor Grothendieck wrote
>>
>> Try this xyplot.zoo lattice graph.   Time series are represented in
>> columns so we transpose the data and convert it to zoo.  The screen=
>> argument available in xyplot.zoo groups series into panels:
>>
>> Lines <- "plant,aphid,1,2,3,4
>> pumpkin,1-2,0.065566,0.057844,0.08,0.086879
>> pumpkin,1-3,0.107612,0.097272,0.11663,0.160499
>> squash,1-4,0.126939,0.115003,0.140275,0.188829"
>>
>> library(zoo)
>> library(lattice)
>> DF <- read.csv(text = Lines)
>> z <- zoo(t(DF[3:6]))
>> colnames(z) <- DF$aphid
>> xyplot(z, screen = DF$plant)
>>
>
> Thank you! That gets the data in exactly the shape I was expecting. I was
> hoping to get the graph showing all lines on one plot, but coloured
> according to plant. I tried to change screen=DF$plant for groups=DF$plant,
> but it doesn't work, and I can't figure out from the documentation why it
> doesn't work (I think I need to more thoroughly understand data types
> first). Could you point me in the right direction?
>

1. Try this which uses lattice zoo graphics:

xyplot(z, screen = 1, col = DF$plant)

or with a legend:

key <- list(space = "top",  text = levels(DF$plant), points = FALSE,
	lines = TRUE, col = 1:nlevels(DF$plant))
xyplot(z, screen = 1, col = DF$plant, auto.key = key)

2. or using classic zoo graphics

plot(z, screen = 1, col = DF$plant)

To add a legend:

legend("topleft", legend = levels(DF$plant), col = 1:nlevels(DF$plant), lty = 1)

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list