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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jul 13 01:57:41 CEST 2012


Author: weylandt
Date: 2012-07-13 01:57:40 +0200 (Fri, 13 Jul 2012)
New Revision: 680

Modified:
   pkg/xtsExtra/R/plot.R
   pkg/xtsExtra/man/plot.xts.Rd
Log:
Add color support to plot.xts scatterplots

Modified: pkg/xtsExtra/R/plot.R
===================================================================
--- pkg/xtsExtra/R/plot.R	2012-07-12 23:22:42 UTC (rev 679)
+++ pkg/xtsExtra/R/plot.R	2012-07-12 23:57:40 UTC (rev 680)
@@ -178,11 +178,12 @@
   return(invisible(reclass(x)))
 }
 
-do_scatterplot <- function(x, y, xy.labels, xy.lines, xlab, ylab, main, log, cex, xlim, ylim, type, pch, ...){
+do_scatterplot <- function(x, y, xy.labels, xy.lines, xlab, ylab, main, log, cex, xlim, ylim, type, pch, col, ...){
   if(missing(main)) main <- paste(xlab, "vs.", ylab)
   if(missing(log))  log  <- ''
   if(missing(cex))  cex  <- 0.8
   if(missing(pch))  pch  <- 1L
+  if(missing(col))  col  <- 1
   
   x <- try.xts(x); y <- try.xts(y)
   
@@ -203,10 +204,12 @@
   type  <- if(missing(type)){if(do.lab) "c" else "l"} else type
 
   plot(xy[1:2], type = ptype, main = main, xlab = xlab, 
-        ylab = ylab, xlim = xlim, ylim = ylim, log = log, pch = pch)
+        ylab = ylab, xlim = xlim, ylim = ylim, log = log, pch = pch, col = col)
 
-  if(do.lab) text(xy[1:2], cex = cex, labels = if(!is.logical(xy.labels)) xy.labels else index2char(index(xy.xts)))
-  if(xy.lines) lines(xy[1:2], type = type)
+  if(do.lab) text(xy[1:2], cex = cex, labels = if(!is.logical(xy.labels)) xy.labels else index2char(index(xy.xts)), col = col)
+  
+  if(xy.lines) segments(xy[[1]][-NROW(xy[[1]])],xy[[2]][-NROW(xy[[2]])], 
+                     xy[[1]][-1],xy[[2]][-1], col = col)
 
   return(invisible(xy.xts))
 }

Modified: pkg/xtsExtra/man/plot.xts.Rd
===================================================================
--- pkg/xtsExtra/man/plot.xts.Rd	2012-07-12 23:22:42 UTC (rev 679)
+++ pkg/xtsExtra/man/plot.xts.Rd	2012-07-12 23:57:40 UTC (rev 680)
@@ -123,8 +123,9 @@
 # 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)
+# Use of color gradient
+cr <- colorRampPalette(c("#00FF00","#FF0000"))
+plot(sample_xts[,1],sample_xts[,2], xy.labels = FALSE, xy.lines = TRUE, col = cr(NROW(sample_xts)))
 
 plot(sample_xts[,1], sample_xts[,2], xy.labels = FALSE, xy.lines = TRUE, type = "l")
 }



More information about the Xts-commits mailing list