Friday, August 19, 2011

display time series data in R

Thanks to the Revolutions blog, several things learned here:
1. R code for heat calendar
2. generate SVG from R
3. pretty-R tool

OK. Let's explain it by plotting the fund WASCX (IVY ASSET STRATEGY FUND CLASS C) from 2009-03-14:
# source code of calendarHeat (by Paul Bleicher)
source("http://blog.revolution-computing.com/downloads/calendarHeat.R")
 
# fetch stock time series data from Yahoo finance
stock <- "WASCX"
start.date <- "2009-03-14"
end.date <- Sys.Date()
quote <- paste("http://ichart.finance.yahoo.com/table.csv?s=",
                stock,
                "&a=", substr(start.date,6,7),
                "&b=", substr(start.date, 9, 10),
                "&c=", substr(start.date, 1,4), 
                "&d=", substr(end.date,6,7),
                "&e=", substr(end.date, 9, 10),
                "&f=", substr(end.date, 1,4),
                "&g=d&ignore=.csv", sep="")             
stock.data <- read.csv(quote, as.is=TRUE)
 
# display in SVG format
svg("WASCX.svg",width=14,height=7)
calendarHeat(stock.data$Date, stock.data$Adj.Close, 
  varname="WASCX Adjusted Close")
dev.off()
Created by Pretty R at inside-R.org

No comments:

Post a Comment