[R] xts/time-series and plot questions...
Joshua Ulrich
josh.m.ulrich at gmail.com
Mon Oct 3 22:55:02 CEST 2011
Hi Doug,
Thanks for taking the time to write a great question.
On Mon, Oct 3, 2011 at 12:23 PM, Douglas Philips <dgou at mac.com> wrote:
> 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)...
>
xts requires a time-based index, so there's no way to make an index
"year-free". What you can do, is split the xts object into years,
convert all the index values to have the same year, and merge them
together.
The "toyear" function I've provided below converts the index values to
a specific year. A "month-free" solution would be similar. I'd also
recommend using plot.zoo for more complex graphs.
toyear <- function(x, year) {
# get year of last obs
xyear <- .indexyear(last(x))+1900
# get index and convert to POSIXlt
ind <- as.POSIXlt(index(x))
# set index year to desired value
ind$year <- year-1900
index(x) <- ind
# label column with year of last obs
colnames(x) <- paste(colnames(x),xyear,sep=".")
x
}
# split data into a list of xts objects by year
tmp_dat_yr_list <- split(temp_data, "years")
# convert each list element to be "2011"
tmp_dat_yr_list <- lapply(tmp_dat_yr_list, toyear, 2011)
# merge all list elements into one object
temp_data_by_year <- do.call(merge, tmp_dat_yr_list)
# plot.zoo has more features than plot.xts at the moment
plot.zoo(temp_data_by_year, screens=1,
col=rainbow(ncol(temp_data_by_year)))
> Pointers to online answers, google search terms, etc. greatly appreciated!
>
> Thanks,
> -=Doug
>
Best,
--
Joshua Ulrich | FOSS Trading: www.fosstrading.com
More information about the R-help
mailing list