[Xts-commits] r858 - in pkg/xtsExtra: R sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Sep 21 16:29:37 CEST 2014


Author: rossbennett34
Date: 2014-09-21 16:29:36 +0200 (Sun, 21 Sep 2014)
New Revision: 858

Modified:
   pkg/xtsExtra/R/plot2.R
   pkg/xtsExtra/sandbox/test_plot2.R
Log:
adding args for addLegend

Modified: pkg/xtsExtra/R/plot2.R
===================================================================
--- pkg/xtsExtra/R/plot2.R	2014-09-21 14:13:40 UTC (rev 857)
+++ pkg/xtsExtra/R/plot2.R	2014-09-21 14:29:36 UTC (rev 858)
@@ -1403,10 +1403,14 @@
 #' @param legend.loc legend.loc places a legend into one of nine locations on 
 #' the chart: bottomright, bottom, bottomleft, left, topleft, top, topright, 
 #' right, or center.
+#' @param legend.names character vector of names for the legend. If \code{NULL},
+#' the column names of the current plot object are used.
+#' @param colorset fill colorset for the legend. If \code{NULL},
+#' the colorset of the current plot object data is used.
 #' @param ncol number of columns for the legend
 #' @param \dots any other passthrough parameters. Not currently used.
 #' @author Ross Bennett
-addLegend <- function(legend.loc="center", ncol=1, ...){
+addLegend <- function(legend.loc="center", legend.names=NULL, colorset=NULL, ncol=1, ...){
   lenv <- new.env()
   lenv$main <- ""
   
@@ -1491,8 +1495,16 @@
   lenv$ly <- ly
   lenv$xjust <- xjust
   lenv$yjust <- yjust
-  lenv$colorset <- plot_object$Env$theme$colorset[1:nc]
-  lenv$names <- plot_object$Env$column_names
+  if(!is.null(colorset)){
+    lenv$colorset <- colorset[1:nc]
+  } else {
+    lenv$colorset <- plot_object$Env$theme$colorset[1:nc]
+  }
+  if(!is.null(legend.names)){
+    lenv$names <- legend.names
+  } else {
+    lenv$names <- plot_object$Env$column_names
+  }
   lenv$nc <- ncol
   # add expression for legend
   exp <- expression(legend(x=lx, y=ly, legend=names, xjust=xjust, yjust=yjust, 

Modified: pkg/xtsExtra/sandbox/test_plot2.R
===================================================================
--- pkg/xtsExtra/sandbox/test_plot2.R	2014-09-21 14:13:40 UTC (rev 857)
+++ pkg/xtsExtra/sandbox/test_plot2.R	2014-09-21 14:29:36 UTC (rev 858)
@@ -102,6 +102,7 @@
 }
 plot(R, FUN=foo)
 addLegend(ncol = 4)
+addLegend(legend.names = c("foo", "bar"), colorset = c(1,2), ncol=2)
 
 plot(R, FUN=foo, legend.loc="topleft")
 plot(R, FUN=foo, legend.loc="left")



More information about the Xts-commits mailing list