Thursday, August 16, 2012

plot.xts is wonderful

As mentioned in FOSS Trading post A New plot.xts yesterday

“The Google Summer of Code (2012) project to extend xts has produced a very promising new plot.xts function. Michael Weylandt, the project's student, wrote R-SIG-Finance to request impressions, feedback, and bug reports. The function is housed in the xtsExtra package of the xts project on R-Forge.

Please try xtsExtra::plot.xts and let us know what you think. A sample of the eye-candy produced by the code in Michael's email is below. Granted, this isn't a one-liner, but it's certainly impressive! Great work Michael!”

and as announced by author Michael Weylandt in

“As the community which makes the most heavy use of xts, I would like to draw your attention to a new set of plotting functions for xts objects available as part of Google Summer of Code 2012. This work represents a major overhaul of previously existing plot.xts and should provide you with the most comprehensive and flexible time series plotting available in R. Features include:

  • "automagic" layout construction and axis alignment
  • smart argument recycling
  • panel function abilities
  • more attractive candle and bar plots for OHLC objects
  • scatterplots to view the co-evolution of multiple series
  • event markers
  • regime highlighting
  • time-oriented barplots via barplot.xts [based on code by Peter Carl]
  • interoperability with all known R time series classes using the xts try/reclass paradigm

while retaining the same smart axis formatting and gridlines that plot.xts provided. We have made every effort to maintain complete compatibility with documented usages of the old plot.xts and to be 95% compatible with plot.zoo. My goal has been to craft a design which uses smart defaults to put attractive and informative graphics ever at your fingertips, while remaining flexible enough for "power-users" to craft every detail as they desire…”

Michael has done a fantastic job with this Google Summer of Code (GSOC) project, and I look forward to incorporating all the new features of plot.xts.  As a quick example very different from that shown in the post and mailing list announcement, I thought it would be fun to show how we can use plot.xts to replace the veteran chart.TimeSeries and charts.PerformanceSummary plots from the PerformanceAnalytics package.

For the chart.TimeSeries, I will almost exactly replicate the chart given in the documentation.

From TimelyPortfolio

Not really as a useful application but for the sake of demonstration, we can produce something like this to separate styles and also avoid event label collision (just implemented by Michael very late last night).

From TimelyPortfolio

plot.xts even allows plot.zoo and lattice panel type functionality which allows us to do very nice things like in this charts.PerformanceSummary style chart.

From TimelyPortfolio

I very much appreciate this very powerful contribution to R.  Thanks to everyone involved.

R code in GIST (note: install xtsExtra from R-forge) :


  1. I knew you were going to make some beautiful charts with xtsExtra::plot.xts!

  2. Unbelievable. These new functions are easy like a dream.

  3. This comment has been removed by a blog administrator.

  4. Learning R, just playing around. This is great. A big thank you.

    Remark: the code should be:


    i.e. begin.time,etc instead of start.time,etc.

    Sadly, I was unable to test the last plot because of:

    Warning in install.packages :
    package ‘r-cran-rcolorbrewer’ is not available (for R version 2.15.2)

    I'm getting this error message even when I try to install from the package, tested on both Windows 7 and kUbuntu 12.10 (I get the same error with other packages).

    This is even less successful:
    Error in install.packages : object 'RColorBrewer' not found

  5. Not sure what these error messages for RColorBrewer were about, they have disappeared now and the package is properly installed and working.

    The following is a fix to the last part of the code, where the addalpha function was missing (Google found it for me):

    ### playing around with type="h"
    #add alpha to colors
    addalpha <- function(cols,alpha=180) {
    rgbcomp <- col2rgb(cols)
    rgbcomp[4] <- alpha

  6. you also want this somewhere near the top


  7. Hi Timeportfolio, xtsExtra::plot.xts is now deprecated and your code is not (for now) reproducible. Could you provide an update of your code? I'm going to try that tomorrow, if I can, I'm going to provide a little Repo (or gist).