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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 17 00:46:16 CEST 2014


Author: rossbennett34
Date: 2014-07-17 00:46:15 +0200 (Thu, 17 Jul 2014)
New Revision: 818

Modified:
   pkg/xtsExtra/R/plot2.R
   pkg/xtsExtra/sandbox/test_plot2.R
Log:
Adding panels and more examples to test_plot2

Modified: pkg/xtsExtra/R/plot2.R
===================================================================
--- pkg/xtsExtra/R/plot2.R	2014-07-16 22:05:27 UTC (rev 817)
+++ pkg/xtsExtra/R/plot2.R	2014-07-16 22:46:15 UTC (rev 818)
@@ -201,7 +201,7 @@
     args <- mainPanel$args
     .formals <- formals(FUN)
     .formals <- modify.args(formals=.formals, arglist=args, dots=TRUE)
-    if("R" %in% names(.formals)) .formals <- modify.args(formals=.formals, arglist=NULL, R=R, dots=TRUE)
+    if("R" %in% names(.formals)) .formals <- modify.args(formals=.formals, arglist=NULL, R=x, dots=TRUE)
     .formals$... <- NULL
     R <- try(do.call(FUN, .formals), silent=TRUE)
     if(inherits(R, "try-error")) { 
@@ -368,24 +368,22 @@
     }
   } else {
     cs$add(expression(chart.lines(R[xsubset], type=type)),expr=TRUE)
+    assign(".xts_chob", cs, .plotxtsEnv)
   }
-  assign(".xts_chob", cs, .plotxtsEnv)
   
   # Plot the panels or default to a simple line chart
-  #if(!is.null(panel) && nchar(panel) > 0) {
-  #  panel <- parse(text=panel, srcfile=NULL)
-  #  for( p in 1:length(panel)) {
-  #    if(length(panel[p][[1]][-1]) > 0) {
-  #      cs <- eval(panel[p])
-  #    } else {
-  #      cs <- eval(panel[p])
-  #    }
-  #  }
-  #} else {
-  #  cs$add(expression(chart.lines(xdata[xsubset])),expr=TRUE)
-  #}
-  # assign(".xts_chob", cs, .plotxtsEnv)
-  
+  if(!is.null(panels) && nchar(panels) > 0) {
+    panels <- parse(text=panels, srcfile=NULL)
+    for( p in 1:length(panels)) {
+      if(length(panels[p][[1]][-1]) > 0) {
+        cs <- eval(panels[p])
+      } else {
+        cs <- eval(panels[p])
+      }
+    }
+  }
+
+  assign(".xts_chob", cs, .plotxtsEnv)
   cs
 } #}}}
 

Modified: pkg/xtsExtra/sandbox/test_plot2.R
===================================================================
--- pkg/xtsExtra/sandbox/test_plot2.R	2014-07-16 22:05:27 UTC (rev 817)
+++ pkg/xtsExtra/sandbox/test_plot2.R	2014-07-16 22:46:15 UTC (rev 818)
@@ -8,6 +8,9 @@
 # basic plot with defaults
 plot2_xts(R)
 
+plot2_xts(R, mainPanel=list(name="CumReturns"),
+          panels=c("addReturns(type='h')", "addDrawdowns()"))
+
 # assign to a variable and then print it results in a plot
 x <- plot2_xts(R)
 class(x)
@@ -16,6 +19,12 @@
 # small multiples, line plot of each column
 plot2_xts(R, byColumn=TRUE)
 
+layout(matrix(1:2))
+plot2_xts(R, byColumn=2)
+layout(matrix(1))
+
+plot2_xts(R[,1])
+
 # bar chart of returns
 plot2_xts(R[,1], type="h")
 
@@ -31,10 +40,24 @@
 addReturns(type="h")
 addDrawdowns()
 
+# Replicate charts.Performance Summary in a 2x2 layout
+# y-axis range here can be deceiving
+layout(matrix(1:4, 2, 2))
+for(i in 1:ncol(R)){
+  p <- plot2_xts(R[,i], mainPanel=list(name="CumReturns"),
+                 panels=c("addReturns(type='h')", "addDrawdowns()"),
+                 name=colnames(R)[i])
+  print(p)
+}
+layout(matrix(1))
+
+# make chart specifications simple functions that return expressions to
+# evaluate just like panels
+
 # layout safe
-# layout(matrix(1:4, 2, 2))
-# for(i in 1:4) {plot(plot2_xts(R[,i], type="h"))}
-# layout(matrix(1))
+layout(matrix(1:4, 2, 2))
+for(i in 1:4) {plot(plot2_xts(R[,i], type="h"))}
+layout(matrix(1))
 
 # Rolling performance
 plot2_xts(R, mainPanel=list(name="CumReturns"))



More information about the Xts-commits mailing list