Skip to content
Prev 273242 / 398506 Next

xts/time-series and plot questions...

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:
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)))
Best,
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com