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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 9 02:13:10 CEST 2012


Author: weylandt
Date: 2012-08-09 02:13:10 +0200 (Thu, 09 Aug 2012)
New Revision: 708

Modified:
   pkg/xtsExtra/R/plot.R
   pkg/xtsExtra/man/plot.xts.Rd
Log:
Add preliminary legend support

Modified: pkg/xtsExtra/R/plot.R
===================================================================
--- pkg/xtsExtra/R/plot.R	2012-08-08 23:39:42 UTC (rev 707)
+++ pkg/xtsExtra/R/plot.R	2012-08-09 00:13:10 UTC (rev 708)
@@ -23,6 +23,7 @@
       auto.grid = TRUE, major.ticks = 'auto', minor.ticks = TRUE, major.format = TRUE, 
       bar.col.up = 'white', bar.col.dn ='red', candle.col='black',
       xy.labels = FALSE, xy.lines = NULL, ylim = 'auto', panel = lines,
+      auto.legend = FALSE, legend.names = colnames(x), legend.loc = "topleft", legend.pars = NULL,
       events, blocks, nc, nr, ...) {
   
   # Restore old par() options from what I change in here
@@ -117,6 +118,8 @@
     
     x.split <- split.xts.by.cols(x, screens)
     
+    if(auto.legend) legend.names <- split(legend.names, screens)
+    
     # For now, loop over screens one by one constructing relevant elements
     for(i in seq_along(levels((screens)))){
       x.plot <- x.split[[i]]
@@ -142,8 +145,13 @@
             events = events, blocks = blocks,
             yax.loc = yax.loc, ylim = get.elm.recycle(ylim, i))
       
-      do_add.lines(x.plot, panel = panel, col = col.panel, lwd = lwd.panel, 
+      legend.pars.add <- do_add.lines(x.plot, panel = panel, col = col.panel, lwd = lwd.panel, 
                    pch = pch.panel, type = type.panel, cex = cex.panel)
+
+      if(auto.legend) do.call(do_add.legend, 
+                  c(legend.names = list(legend.names[[i]]), 
+                    legend.loc = get.elm.recycle(legend.loc, i), 
+                    legend.pars.add, legend.pars))
     }
     
   }  
@@ -370,9 +378,10 @@
     
     # Panel function (by default lines.xts) always uses POSIXct for plotting internally
     # No need to special case lines: this formulation emulates lines.xts
-    panel(.index(x), x[,j], col = col.t, pch = pch.t, tpe = type.t, 
+    panel(.index(x), x[,j], col = col.t, pch = pch.t, type = type.t, 
           lwd = lwd.t, cex = cex.t)
   }
+  list(col = col, pch = pch, cex = cex, lwd = lwd, type = type)
 }
 
 do_add.shading <- function(blocks, y){
@@ -398,7 +407,15 @@
   }
 }
 
-do_add.legend <- function(){}
+do_add.legend <- function(legend.names, legend.loc, col, lwd, pch, cex, type, ...){
+  legend(
+    x = legend.loc, 
+    legend = legend.names, 
+    col = col, 
+    lwd = lwd, 
+    pch = if(type != "l") pch else NULL, 
+    cex = cex, ...)
+}
 
 do_plot.ohlc <- function(x, bar.col.up, bar.col.dn, candle.col, major.ticks, 
                         minor.ticks, major.format, auto.grid, 

Modified: pkg/xtsExtra/man/plot.xts.Rd
===================================================================
--- pkg/xtsExtra/man/plot.xts.Rd	2012-08-08 23:39:42 UTC (rev 707)
+++ pkg/xtsExtra/man/plot.xts.Rd	2012-08-09 00:13:10 UTC (rev 708)
@@ -12,7 +12,10 @@
             major.format=TRUE, bar.col.up = 'white',
             bar.col.dn ='red', candle.col='black',
             xy.labels = FALSE, xy.lines = NULL, 
-            ylim = 'auto', panel = lines, events, blocks, nc, nr, ...)
+            ylim = 'auto', panel = lines, 
+            auto.legend = FALSE, legend.names = colnames(x), 
+            legend.loc = "topleft", legend.pars = NULL, 
+            events, blocks, nc, nr, ...)
 }
 \arguments{
   \item{x}{an \code{xts} object}
@@ -32,6 +35,10 @@
   \item{xy.lines}{connect points in scatterplot?}
   \item{ylim}{How to handle \code{ylim} for plots. If \code{'fixed'} all panels share \code{ylim = range(x)}; if \code{'auto'} panels sharing a y axis have the same limits. If a numeric matrix, rows are recycled panel-wise as \code{ylim}.}
   \item{panel}{A panel function for plotting; by default, \code{lines.xts}. Currently, is passed \code{col}, \code{pch}, \code{type}, \code{lwd}, \code{cex} by default so be prepared to handle these arguments, perhaps by receiving them via \code{...} and ignoring. If \code{panel != lines}, that is, if the user supplies a panel function, the first two arguments passed will be \code{as.POSIXct(index(z))} and \code{coredata(z)} where \code{z} is the series being plotted in that panel; as a result, note that any plotting inside \code{panel} requires \code{POSIXct}, regardless of the index class of \code{x}. Note further that \code{panel} is called for each series individually, even if they share a screen.}
+  \item{auto.legend}{Should a legend be added automatically?}
+  \item{legend.names}{Names of series to be passed to the legend writing code. Should be one name per series plotted.}
+  \item{legend.loc}{Location of legend: will be recycled panelwise.}
+  \item{legend.pars}{Additional arguments, passed as a list, to be passed to \code{legend}. Will not recycle}
   \item{events}{A list with mandatory elements \code{time} and \code{label} and optional 
   elements \code{col} and \code{lty} giving the events to be highlighted. See examples.}
   \item{blocks}{A list with mandatory elements \code{start.time} and \code{end.time} and 



More information about the Xts-commits mailing list