[Xts-commits] r620 - in pkg/xtsExtra: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon May 28 20:12:00 CEST 2012


Author: weylandt
Date: 2012-05-28 20:12:00 +0200 (Mon, 28 May 2012)
New Revision: 620

Added:
   pkg/xtsExtra/R/barplot.R
   pkg/xtsExtra/man/barplot.xts.Rd
Modified:
   pkg/xtsExtra/NAMESPACE
   pkg/xtsExtra/R/plot.R
   pkg/xtsExtra/man/plot.xts.Rd
Log:
Got panel margins working for plot.xts and added barplot outline (empty now); still need to fix x/y axis labelling for plot.xts

Modified: pkg/xtsExtra/NAMESPACE
===================================================================
--- pkg/xtsExtra/NAMESPACE	2012-05-23 05:41:30 UTC (rev 619)
+++ pkg/xtsExtra/NAMESPACE	2012-05-28 18:12:00 UTC (rev 620)
@@ -1,4 +1,4 @@
-# exportPattern("^[[:alpha:]]+")
+exportPattern("^[[:alpha:]]+")
 # Won't want to export everything eventually (obviously)
 
 S3method(plot, xts)

Added: pkg/xtsExtra/R/barplot.R
===================================================================
--- pkg/xtsExtra/R/barplot.R	                        (rev 0)
+++ pkg/xtsExtra/R/barplot.R	2012-05-28 18:12:00 UTC (rev 620)
@@ -0,0 +1,25 @@
+#   xtsExtra: Extensions to xts during GSOC-2012
+#
+#   Copyright (C) 2012  Michael Weylandt: michael.weylandt at gmail.com
+#
+#   Barplot code inspired by chart.StackedBar in the PerformanceAnalytics Package 
+#   Thanks to B. Peterson & P. Carl
+#
+#   This program is free software: you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation, either version 3 of the License, or
+#   (at your option) any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+barplot.xts <- function(height, stacked = TRUE, scale = FALSE, ...) {
+  .NotYetImplemented()
+  return(invisible(height))
+}
+  
\ No newline at end of file

Modified: pkg/xtsExtra/R/plot.R
===================================================================
--- pkg/xtsExtra/R/plot.R	2012-05-23 05:41:30 UTC (rev 619)
+++ pkg/xtsExtra/R/plot.R	2012-05-28 18:12:00 UTC (rev 620)
@@ -1,10 +1,9 @@
-#
 #   xtsExtra: Extensions to xts during GSOC-2012
 #
 #   Copyright (C) 2012  Michael Weylandt: michael.weylandt at gmail.com
 #
-#   Scatterplot code taken from plot.zoo in the CRAN zoo package
-#   
+#   Scatterplot code taken from plot.zoo in the CRAN zoo package 
+#   Thanks to  A. Zeilis & G.Grothendieck
 #
 #   This program is free software: you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
@@ -20,8 +19,10 @@
 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # To do: 
-#    REMOVE par() ARGS FROM FORMALS AND INSTEAD TREAT ... BETTER
-#      par(mfrow = c(2,2), mar = c(0,5,0,5), oma = c(6, 0, 5, 0)) -- seems to work for one column?
+#    REMOVE par() ARGS FROM FORMALS AND INSTEAD TREAT ... BETTER [Still need to do "type"]
+#    I think layout is working, but need to turn off x/y labels smartly when things are adjacent
+#    Handle not adjacent cases
+#    
 #    DO LAYOUT WITHOUT USING LAYOUT -- NEED TO BE ABLE TO MOVE BETWEEN PLOTS WHEN ADDING LINES?
 #    GET LAYOUT TO SUPPORT ADJACENT COLUMNS
 #    HANDLE xlim AS ISO8601 AS WELL
@@ -30,8 +31,9 @@
 #    Combine OHLC and multi-panel (i.e., if passed cbind(SPY, AGG)) 
 #    candle.col is not supported? 
 #    ylab.loc = c("left", "right", "out","in","flip","above") -- above kills panel alignment automatically
+#    Refactor plotting functionality into some non-exported bits
+#    We've stopped handling ylab? 
 
-
 ## How I really want to handle screens
 ## Give user ultimate flexibility in setting up screens combining them as desired with layout-like interface
 ## Go by rows on matrix and whenever number of panels changes, add new time axis
@@ -42,20 +44,24 @@
 
 `plot.xts` <- function(x, y = NULL, 
                        screens, screens.layout,
-                       type, auto.grid=TRUE,
+                       auto.grid=TRUE,
                        major.ticks='auto', minor.ticks=TRUE, 
                        major.format=TRUE,
                        bar.col='grey', candle.col='white',
                        xy.labels = FALSE, xy.lines = NULL,
                        ...) {
   
+  # Restore old par() options from what I change in here
+  old.par <- par(no.readonly = TRUE)
+  on.exit(par(old.par))
+  
   dots <- list(...)
   
   setParGlb <- function(arg, default){
     # See if par was passed through ... 
     # if so, use it, else use default
     #
-    # Also strip from dots once it's been handled directly
+    # Also strip from dots once it has been handled directly
     # This is for "global" parameters
     # See setParCol for ones which are columnwise (series-wise)
     
@@ -109,7 +115,7 @@
        r
     } else {
       default
-    }     
+    }
     rep(r, length.out = length(screens))
   }
   
@@ -122,6 +128,7 @@
     ylab <- setParGlb(ylab, deparse(substitute(y)))
     main <- setParGlb(main, paste(xlab, "vs.", ylab))
     log <- setParGlb(log, '')
+    cex <- setParGlb(cex, 0.7)
     
     x <- try.xts(x); y <- try.xts(y)
     
@@ -139,13 +146,14 @@
     if(is.null(xy.lines)) xy.lines <- do.lab
     
     ptype <- setParGlb(type, if(do.lab) "n" else "p")
+    type <- setParGlb(type, if(do.lab) "c" else "l")
 
     do.call("plot.default", c(xy[1:2], list(type = ptype, main = main, xlab = xlab, 
       ylab = ylab, xlim = xlim, ylim = ylim, log = log), dots))
       
     if(do.lab) do.call("text", 
-      c(xy[1:2], list(labels = if(!is.logical(xy.labels)) xy.labels else index2char(index(xy.xts)), log = log), dots))
-    if(xy.lines) do.call("lines", c(xy[1:2], list( type = if(do.lab) "c" else "l", log = log), dots))
+      c(xy[1:2], dots, list(cex = cex, labels = if(!is.logical(xy.labels)) xy.labels else index2char(index(xy.xts)))))
+    if(xy.lines) do.call("lines", c(xy[1:2], list(type = type), dots))
 
     assign(".plot.xts", recordPlot(), .GlobalEnv)
     return(invisible(xy.xts))
@@ -158,10 +166,12 @@
   
   x <- try.xts(x)
   
-  # Catch OHLC case independently -- will violate DRY but that's ok for now
-  if(!missing(type) && type %in% c('candles','bars')){
-  
-    if(!is.OHLC(x)) stop(type, '-chart not supported for non-OHLC series')
+  # Catch OHLC case independently -- will violate DRY but that seems ok for now
+  if("type" %in% names(dots) && dots[["type"]] %in% c('candles','bars')){
+    
+    type <- setParGlb(type, 'candles') # This default doesn't really matter since we can't get here without it existing already
+    
+    if(!xts:::is.OHLC(x)) stop(type, '-chart not supported for non-OHLC series')
     if(type == 'bars') stop('OHLC bars not yet supported.')
   
     # Handle OHLC candles 
@@ -210,14 +220,16 @@
   
     ep <- axTicksByTime(x,major.ticks, format.labels=major.format)
   
-    type <- split(if(missing(type)) rep('l', NCOL(x)) else rep(type, length.out = NCOL(x)), screens)
-  
     col <- setParCol(col, lapply(split(seq_len(NCOL(x)), screens), rank), screens)
     lwd <- setParCol(lwd, split(rep(1, NCOL(x)), screens), screens)
+    type <- setParCol(type, split(rep('l', length(screens)), screens))
+    
     ylab <- setParScr(ylab, if(NCOL(x) == 1 || length(levels(screens)) == 1) "" else
       if(!is.null(colnames(x))) colnames(x) else paste("Column", seq_len(NCOL(x))), screens)
     log <- setParScr(log, '', screens)
+    
 
+    par(mar = c(0,0,0,0), oma = c(4, 6, 4, 4))
     layout(screens.layout) # BETTER TO DO THIS MANUALLY WITH PAR()
   
     # For now, loop over screens and do plots automatically
@@ -251,6 +263,6 @@
   return(invisible(reclass(x)))
 }
 
-setup.grid <- function(x){
-  # Sets up the axis background for the plot
-}
\ No newline at end of file
+#setup.grid <- function(x){
+#  # Sets up the axis background for the plot
+#}
\ No newline at end of file

Added: pkg/xtsExtra/man/barplot.xts.Rd
===================================================================
--- pkg/xtsExtra/man/barplot.xts.Rd	                        (rev 0)
+++ pkg/xtsExtra/man/barplot.xts.Rd	2012-05-28 18:12:00 UTC (rev 620)
@@ -0,0 +1,18 @@
+\name{barplot.xts}
+\alias{barplot.xts}
+\title{ Time-Oriented Barplots from xts Objects }
+\description{A barplot method for xts objects.}
+\usage{
+\method{barplot}{xts}(height, stacked = TRUE, scale = FALSE, ...)
+}
+\arguments{
+  \item{height}{ an \code{xts} object of desired frequency. Use \code{to.period} functions to aggregate data outside of \code{barplot}.}
+  \item{stacked}{logical. Should barplot be stacked?}
+  \item{scale}{logical. Should data be rescaled to sum to 100\% at each bar? Currently does not support negative values.}
+  \item{\dots}{ additional graphical arguments }
+}
+\details{
+Used to provide time-oriented barplots. 
+}
+\value{Invisibly returns the plotted object. Also assigns \code{.barplot.xts} to \code{.GlobalEnv} which can be used to "replay" the plot.}
+\author{ Michael Weylandt using code by P. Carl and B. Peterson}
\ No newline at end of file

Modified: pkg/xtsExtra/man/plot.xts.Rd
===================================================================
--- pkg/xtsExtra/man/plot.xts.Rd	2012-05-23 05:41:30 UTC (rev 619)
+++ pkg/xtsExtra/man/plot.xts.Rd	2012-05-28 18:12:00 UTC (rev 620)
@@ -7,7 +7,7 @@
 \usage{
 \method{plot}{xts}(x, y = NULL, 
                 	screens, screens.layout,
-                	type, auto.grid=TRUE,
+                	auto.grid=TRUE,
                     major.ticks='auto', minor.ticks=TRUE, 
                     major.format=TRUE,
                     bar.col='grey', candle.col='white',
@@ -21,7 +21,6 @@
   	be plotted in. If not specified, then defaults to a single series per screen for
   	\code{type} not \code{"candles"} or \code{"bars"} See examples.}
   \item{screens.layout}{ Matrix (in a form that could be passed to layout) which arranges screens.}
-  \item{type}{ type of plot to produce; can be \code{"candles"} or \code{"bars"} in addition to the regular options. }
   \item{auto.grid}{ should grid lines be drawn }
   \item{major.ticks}{ should major tickmarks be drawn and labeled }
   \item{minor.ticks}{ should minor tickmarks be drawn }
@@ -34,20 +33,71 @@
 }
 \details{
 Mainly used to draw time-series plots with sensible x-axis labels, it
-can also plot basic OHLC series using \code{type='candles'} or \code{type='bars'}.
+can also plot basic OHLC series using \code{type='candles'} or \code{type='bars'} [\code{'bars'} does not seem to be currently supported].
 
+Care has been taken to make plotting arguments passed by \code{...} behave smartly by classifying them as 'global', 'screen-wise', or 'column-wise' arguments and treating them as such. A partial listing includes:
+\itemize{
+  \item Global parameters: \code{main}
+  \item Screen-wise parameters: \code{log}
+  \item Column-wise parameters: \code{col},\code{type},\code{lwd}
+}
+
 Better financial plots can be found in the \pkg{quantmod} package, though
 these are generally incompatible with standard R graphics tools.
 }
-\value{
-Plots an xts object to the current device.
-}
-\author{ Jeffrey A. Ryan }
+\value{Invisibly returns the plotted object (or the merged object in the scatterplot case). Also assigns \code{.plot.xts} to \code{.GlobalEnv} which can be used to "replay" the plot.}
+\author{ Jeffrey A. Ryan with extensions by Michael Weylandt }
 \examples{
-data(sample_matrix)
-plot(sample_matrix)
-plot(as.xts(sample_matrix))
-plot(as.xts(sample_matrix), type='candles')
+data(sample_matrix, package = "xts")
+sample_xts <- as.xts(sample_matrix)
+
+# A very basic time series plot
+plot(sample_xts[,1]) 
+
+# A candle chart. quantmod does this much better
+plot(sample_xts, type='candles') 
+
+# By default prefers panels
+plot(sample_xts[,1:2]) 
+
+# But this can be too much for many-columned data
+plot(sample_xts[,rep(1:4, each = 3)]) 
+
+# Can customize screen layout
+plot(sample_xts, screens.layout = matrix(1:4, ncol = 2)) 
+
+# Or even be fancy with it
+plot(sample_xts[,1:3], screens.layout = matrix(c(1,1,2,3),ncol = 2, byrow = TRUE))
+
+plot(sample_xts[,1:4], screens.layout = matrix(c(1,1,1,1,2,3,4,4),ncol = 2, byrow = TRUE))
+
+# Or assign multiple series per screen (screens gets recycled as necessary)
+# Note smart assignment of colors
+plot(sample_xts, screens = 1:2) 
+
+# Example of how columnwise args are treated 
+# Note here that col gets recycled alongside screens so it's not helpful here
+plot(sample_xts, screens = 1:2, col = 1:2)
+
+# Best practice is to be specific in setting arguments and not use recycling
+plot(sample_xts, screens = c(1,2,1,2), col = c(1,1,2,2))
+
+# Example of how panelwise args are treated
+plot(10^sample_xts, screens = 1:2, log= c("","y"))
+
+# Global arguments play nice as well
+plot(sample_xts[,1:3], main = "A Truly Remarkble Time Series Plot")
+
+# Makes a scatterplot if we pass two series
+plot(sample_xts[,1],sample_xts[,2])
+
+# With some options
+plot(sample_xts[1:100,1],sample_xts[1:100,2], xy.labels = TRUE)
+
+# This will make more sense when color-gradient is implemented
+plot(sample_xts[,1],sample_xts[,2], xy.labels = FALSE, xy.lines = TRUE)
+
+plot(sample_xts[,1], sample_xts[,2], xy.labels = FALSE, xy.lines = TRUE, type = "l")
 }
 % Add one or more standard keywords, see file 'KEYWORDS' in the
 % R documentation directory.



More information about the Xts-commits mailing list