[R] xts/time-series and plot questions...
Douglas Philips
dgou at mac.com
Mon Oct 3 19:23:00 CEST 2011
Hello,
I'm a complete newbie to R. Spent this past weekend reading The Art of R Programming, The R Cookbook, the language spec, Wikis and FAQs. I sort-of have my head around R; the dizzying selection of libraries, packages, etc? Not really. I've probably missed or failed to understand something...
I have very a simple data set. Two years (ish) of temperature data, collected and time-stamped every 10 minutes. Sample of the data from read.csv(...):
> head(temp.data)
DateTime Temperature
1 2009-11-23 23:20:00 62.9
2 2009-11-23 23:30:00 63.4
3 2009-11-23 23:40:00 63.6
4 2009-11-23 23:50:00 64.2
5 2009-11-24 00:00:00 64.5
6 2009-11-24 00:10:00 64.7
Converted to an xts object:
> str(temp_data)
xts [1:83089, 1] 62.9 63.4 63.6 64.2 64.5 64.7 65.2 65.3 65.8 65.6 ...
- attr(*, "index")= atomic [1:83089] 1.26e+09 1.26e+09 1.26e+09 1.26e+09 1.26e+09 ...
..- attr(*, "tzone")= chr ""
..- attr(*, "tclass")= chr [1:2] "POSIXct" "POSIXt"
- attr(*, "class")= chr [1:2] "xts" "zoo"
- attr(*, ".indexCLASS")= chr [1:2] "POSIXct" "POSIXt"
- attr(*, ".indexTZ")= chr ""
So far so good! I can do all kinds of cool things like plot individual months: plot(temp_data["2009-12"]) and plot monthly, weekly, daily mean, sd, etc. (I have to say, xts has been a dream to work with!)
What I would like to do is plot several sections of this data on the same graph.
Specifically, I would like to plot all the data from one calendar month, regardless of year, on one plot.
i.e. one line for Jan 2009, another line for Jan 2010, anothe line for Jan 2011, etc.
I can use xts functions to slice the data into months (or weeks, or days), but I am not sure how to arrange to get the X-axis to work right. If I do:
plot(temp_data["2010-01"]); lines(temp_data("2011-01"))
lines aren't overlayed; the output from lines() is lost because it is far off of the right of the plot as the plot autoranged() the x and y axes. But I don't think xlim is my problem so much as I need a way to 'slide' temp_data["2011-01"] so that it will appear in the same part of the graph/plot as the 2010-01 data does.
What I think I want to do is write a "normalizing" function that takes data for any given month and makes it "year-free"??? This way I could plot corresponding months on the same graph. One month, or a quarter, or even a full year. I don't know, however, how to convince xts to ignore the year, or if there is an xts compatible object that is year-free (or month-free for looking at week/day segements)...
Pointers to online answers, google search terms, etc. greatly appreciated!
Thanks,
-=Doug
More information about the R-help
mailing list