[R] Plotting by factor with xts

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jan 23 04:09:19 CET 2011


On Sat, Jan 22, 2011 at 12:46 PM, Nick Torenvliet
<nick.torenvliet at gmail.com> wrote:
> Hi all,
>
> I've got an xts time series of stock symbols and closing prices.
>
>> head(x)
>           symbol close
> 2010-01-04 "AFB"  "13.46"
> 2010-01-04 "AKP"  "12.80"
> 2010-01-04 "APX"  " 8.78"
> 2010-01-04 "AYN"  "13.15"
> 2010-01-04 "BAF"  "13.50"
> 2010-01-04 "BBF"  "12.86"
>> tail(x)
>           symbol close
> 2011-01-21 "VMO"  "12.35"
> 2011-01-21 "VOQ"  "13.77"
> 2011-01-21 "VPV"  "11.97"
> 2011-01-21 "VTJ"  "14.74"
> 2011-01-21 "VTN"  "13.16"
> 2011-01-21 "XAA"  "12.82"
>
>> is.xts(x)
> [1] TRUE
>
> I've made the symbols a factor
>
>> head(xFactor)
> symbol   <NA>   <NA>   <NA>   <NA>   <NA>
>   AFB    AKP    APX    AYN    BAF    BBF
> 154 Levels: AFB AKP APX AYN BAF BBF BBK BFK BFO BFZ BIE BJZ BKK BKN BLH ...
> XAA
>

read.zoo has a split argument.  Assume we have a data frame with Date,
Symbol and Price columns. Then:

DF <- data.frame(Date = Sys.Date() + c(1:4, 1:4), Symbol =
rep(LETTERS[1:2], each = 4), Price = 1:8)

library(zoo)
z <- read.zoo(DF, split = 2)
plot(z, screen = 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