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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 9 01:39:42 CEST 2012


Author: weylandt
Date: 2012-08-09 01:39:42 +0200 (Thu, 09 Aug 2012)
New Revision: 707

Modified:
   pkg/xtsExtra/R/barplot.R
   pkg/xtsExtra/R/plot.R
   pkg/xtsExtra/man/plot.xts.Rd
Log:
Small cleanup and doc improvements

Modified: pkg/xtsExtra/R/barplot.R
===================================================================
--- pkg/xtsExtra/R/barplot.R	2012-08-08 15:33:06 UTC (rev 706)
+++ pkg/xtsExtra/R/barplot.R	2012-08-08 23:39:42 UTC (rev 707)
@@ -7,7 +7,7 @@
 #
 #   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
+#   the Free Software Foundation, either version 2 of the License, or
 #   (at your option) any later version.
 #
 #   This program is distributed in the hope that it will be useful,
@@ -21,8 +21,8 @@
 barplot.xts <- function(height, stacked = TRUE, scale = FALSE, auto.legend = TRUE, 
                         major.format = TRUE, ylim = NULL, space = 0.2, cex.axis = 0.8,
                         cex.legend = 0.8, cex.lab = 1, cex.labels = 0.8, cex.main = 1,
-                        xaxis=TRUE, box.color = "black", xlab="Date",
-                        ylab="Value", major.ticks='auto', minor.ticks=TRUE,
+                        xaxis = TRUE, box.color = "black", xlab = "Date",
+                        ylab = "Value", major.ticks = 'auto', minor.ticks = TRUE,
                         xaxis.labels = NULL, col, ...) {
   # Don't like this name for input variable, 
   # but we must match S3 generic so we'll just change it
@@ -87,7 +87,7 @@
   # Set up two panels if needed
   if(auto.legend){
     layout(rbind(1,2), heights = c(6,1), widths = 1)
-    par(mar = c(3,4,4,2) +.1) # set the margins of the barplot panel
+    par(mar = c(3, 4, 4, 2) + 0.1) # set the margins of the barplot panel
       # Note to self: mar= order is c(bottom, left, top, right)
   }
     
@@ -113,7 +113,7 @@
   if(stacked){
     barplot(t(positives), col = col, space = space, axisnames = FALSE, 
             axes = FALSE, ylim = ylim, ...)
-    barplot(t(negatives), add =TRUE, col = col, space = space, 
+    barplot(t(negatives), add = TRUE, col = col, space = space, 
         xlab = xlab, cex.names = cex.lab, axes = FALSE, axisnames = FALSE, 
             ylim = ylim, ...)
   } else {
@@ -133,7 +133,8 @@
       xaxis.labels = names(ep1)
     else
       ep1 = 1:length(xaxis.labels)
-    axis(1, at=ep1, labels=xaxis.labels, lwd=1, mgp=c(3,1.5,0), cex.axis = cex.axis) 
+    
+    axis(1, at = ep1, labels = xaxis.labels, lwd = 1, mgp = c(3,1.5,0), cex.axis = cex.axis) 
     #axis(1, at = lab.ind, lab=rownames[lab.ind], cex.axis = cex.axis, col = box.color)
     #             title(xlab = xlab, cex = cex.lab)
     # use axis(..., las=3) for vertical labels.
@@ -142,7 +143,7 @@
   box(col = box.color)
     
   if(auto.legend){ # For now, only supporting under-legend
-    par(mar = c(0,2,0,1)+.1) # set the margins of the second panel
+    par(mar = c(0, 2, 0, 1) + 0.1) # set the margins of the second panel
     plot.new()
     
     ncol = min(nc, 4)
@@ -150,7 +151,7 @@
     do_barplot.legend("center", legend = colnames(x), cex = cex.legend, 
         fill = col, ncol = ncol, box.col = box.color, border.col = box.color)
   }
-  assign(".barplot.xts",recordPlot(),.GlobalEnv)
+  assign(".barplot.xts", recordPlot(), .GlobalEnv)
   invisible(height)
 }
 

Modified: pkg/xtsExtra/R/plot.R
===================================================================
--- pkg/xtsExtra/R/plot.R	2012-08-08 15:33:06 UTC (rev 706)
+++ pkg/xtsExtra/R/plot.R	2012-08-08 23:39:42 UTC (rev 707)
@@ -7,7 +7,7 @@
 #
 #   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
+#   the Free Software Foundation, either version 2 of the License, or
 #   (at your option) any later version.
 #
 #   This program is distributed in the hope that it will be useful,
@@ -113,6 +113,8 @@
     ylim <- screens[["ylim"]]
     screens <- screens[["screens"]]
     
+    panel <- match.fun(panel)
+    
     x.split <- split.xts.by.cols(x, screens)
     
     # For now, loop over screens one by one constructing relevant elements
@@ -132,8 +134,6 @@
       log.panel <- get.elm.from.dots("log", dots, screens, i)
       if(is.null(log.panel)) log.panel <- ""
       
-      panel <- match.fun(panel)
-      
       # Note that do_add.grid also sets up axes and what not
       do_add.grid(x.plot, major.ticks, major.format, minor.ticks, 
             auto.grid = auto.grid, ylab = ylab.panel, log = log.panel, 
@@ -368,13 +368,10 @@
     lwd.t  <- get.elm.recycle(lwd, j)
     type.t <- get.elm.recycle(type, j)
     
-    if(identical(panel, lines)){
-      panel(x[,j], col = col.t, pch = pch.t, type = type.t, lwd = lwd.t, cex = cex.t)
-    } else {
-      panel(as.POSIXct(index(x)), x[,j], col = col.t, pch = pch.t, type = type.t, lwd = lwd.t, cex = cex.t)
-    }
-    
-    
+    # 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, 
+          lwd = lwd.t, cex = cex.t)
   }
 }
 
@@ -428,8 +425,6 @@
   width = .2*deltat(x)
   
   # Better to do this with xts:::Op etc when moved to xts package?
-  
-  # Candles -- not happy about lwd fixed: make dynamic / smart?
   if(candles) rect(.index(x) - width/4, x[,2L], .index(x) + width/4, x[,3L], 
                    col = candle.col, border = candle.col)
   
@@ -451,21 +446,18 @@
 }
 
 get.elm.from.dots <- function(par, dots, screens, n){
-  if(!(par %in% names(dots))) {
-    # Return NULL if par is not supplied
-    return(NULL)
+  # Return NULL if par is not supplied
+  if(!(par %in% names(dots))) return(NULL)
+  
+  # Repeat par to length of screens and take n-th screen
+  if(length(screens) == 1L){
+    par <- rep(list(dots[[par]]), length.out = length(screens))
   } else {
-    # Repeat par to length of screens and take n-th screen
-    if(length(screens) == 1L){
-      par <- rep(list(dots[[par]]), length.out = length(screens))
-    } else {
-      par <- rep(dots[[par]], length.out = length(screens))
-    }
+    par <- rep(dots[[par]], length.out = length(screens))
+  }
     
+  par <- split(par, screens)
     
-    par <- split(par, screens)
-    
-    j <- n %% length(par)
-    par[[if(j) j else length(par)]]
-  }  
-}
\ No newline at end of file
+  j <- n %% length(par)
+  par[[if(j) j else length(par)]]  
+}

Modified: pkg/xtsExtra/man/plot.xts.Rd
===================================================================
--- pkg/xtsExtra/man/plot.xts.Rd	2012-08-08 15:33:06 UTC (rev 706)
+++ pkg/xtsExtra/man/plot.xts.Rd	2012-08-08 23:39:42 UTC (rev 707)
@@ -155,6 +155,16 @@
 cr <- colorRampPalette(c("#00FF00","#FF0000"))
 plot(sample_xts[,1],sample_xts[,2], xy.labels = FALSE, xy.lines = TRUE, col = cr(NROW(sample_xts)), type = "l")
 
+# Can also use non-xts time series classes if called explicitly
+tser <- ts(cumsum(rnorm(50, 0.05, 0.15)), start = 2007, frequency = 12)
+plot(tser)
+plot.xts(tser)
+
+tser <- cbind(tser, ts(cumsum(rnorm(50, 0.05, 0.15)), start = 2007, frequency = 12))
+plot(tser)
+plot.xts(tser)
+
+
 \dontrun{
 # Some of these will fail because they try to access the 
 # parent frame in ways not congruent with plot.xts



More information about the Xts-commits mailing list