[Analogue-commits] r145 - in pkg: R inst man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Aug 15 23:25:24 CEST 2009


Author: gsimpson
Date: 2009-08-15 23:25:23 +0200 (Sat, 15 Aug 2009)
New Revision: 145

Modified:
   pkg/R/Stratiplot.R
   pkg/R/Stratiplot.formula.R
   pkg/inst/ChangeLog
   pkg/man/Stratiplot.Rd
Log:
Additions to Stratiplot to implement reversing the y-axis and sorting of the plotted variables, by WA of depth/time or a supplied variable.

Modified: pkg/R/Stratiplot.R
===================================================================
--- pkg/R/Stratiplot.R	2009-08-15 21:17:25 UTC (rev 144)
+++ pkg/R/Stratiplot.R	2009-08-15 21:25:23 UTC (rev 145)
@@ -4,15 +4,41 @@
 
 `Stratiplot.default` <- function(x, y,
                                  type = "l",
-                                 ylab = "",
+                                 ylab = NULL,
                                  xlab = "",
                                  pages = 1,
+                                 rev = TRUE,
+                                 sort = c("none", "wa", "var"),
+                                 svar = NULL,
+                                 rev.sort = FALSE,
                                  ...) {
     ## process 'type'
     TYPE <- c("l","h","g","smooth","b","o", "poly","p")
     if(any(!(type %in% TYPE)))
         stop("Invalid 'type' specified")
-    n.vars <- ncol(x)
+    n.vars <- NCOL(x)
+    ## ylabel
+    if(is.null(ylab))
+        ylab <- deparse(substitute(y))
+    ## do we need to sort variables
+    if(missing(sort))
+        sort <- "none"
+    sort <- match.arg(sort)
+    if((check.var <- (missing(svar) || is.null(svar))))
+        svar <- y
+    ord <- seq_len(n.vars)
+    if(sort == "wa") {
+        ## sort by
+        opt <- optima(x, svar)
+        ord <- order(opt)
+    } else if(check.var) {
+        warning("With 'sort = \"var\"', 'svar' not supplied.\nNo sorting applied.")
+    } else {
+        ord <- order(svar)
+    }
+    if(rev.sort)
+        ord <- rev(ord)
+    x <- x[, ord]
     sx <- stack(x)
     sx$ind <- factor(sx$ind, levels = colnames(x))
     ## check length of y
@@ -25,12 +51,14 @@
     }
     ## plot parameters
     maxy <- max(y)
-    ylimits <- rev(c(0 - (0.03*maxy), maxy + (0.03 * maxy)))
+    ylimits <- c(0 - (0.03*maxy), maxy + (0.03 * maxy))
+    if(rev)
+        ylimits <- rev(ylimits)
     max.abun <- sapply(x, function(x) round(max(x), 1))
     xlimits <- lapply(max.abun * 1.05, function(x) c(0, x))
     scales <- list(cex = 0.75, tck = 0.75,
-                  y = list(axs = "r", limits = ylimits),
-                  x = list(axs = "r", rot = 45, relation = "free"))
+                   y = list(axs = "r", limits = ylimits),
+                   x = list(axs = "r", rot = 45, relation = "free"))
     par.strip.text <- list(cex = 0.75)
     ## plotting
     xyplot(y ~ values | ind,
@@ -48,5 +76,6 @@
            panel = panel.Stratiplot,
            layout = c(n.vars, 1, pages),
            par.settings = list(layout.widths = list(panel = max.abun)),
+           ##index.cond = list(ord),
            ...)
 }

Modified: pkg/R/Stratiplot.formula.R
===================================================================
--- pkg/R/Stratiplot.formula.R	2009-08-15 21:17:25 UTC (rev 144)
+++ pkg/R/Stratiplot.formula.R	2009-08-15 21:25:23 UTC (rev 145)
@@ -1,12 +1,6 @@
-`Stratiplot.formula` <- function(formula,
-                                 data,
-                                 subset,
-                                 na.action,
-                                 type = "l",
-                                 ylab = "",
-                                 xlab = "",
-                                 pages = 1,
-                                 ...) {
+`Stratiplot.formula` <- function(formula,  data, subset, na.action,
+                                 type = "l", ylab = NULL, xlab = "",
+                                 pages = 1, ...) {
     cl <- match.call()
     mf <- match.call(expand.dots = FALSE)
     m <- match(c("formula", "data", "subset", "na.action"),
@@ -20,6 +14,8 @@
     data <- data.frame(model.matrix(mt, mf))[,-1]
     n.vars <- ncol(data)
     y <- rep(y, n.vars)
+    if(is.null(ylab))
+        ylab <- as.character(attr(mt, "variables")[[2]])
     Stratiplot.default(x = data, y = y, type = type,
                        ylab = ylab, xlab = xlab, pages = pages,
                        ...)

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2009-08-15 21:17:25 UTC (rev 144)
+++ pkg/inst/ChangeLog	2009-08-15 21:25:23 UTC (rev 145)
@@ -5,6 +5,16 @@
 	* predict.wa: deshrinking method was not being honoured when
 	expanding predictions.
 
+	* Stratiplot: gains option ('rev') to reverse the y-axis limits.
+
+	Can now also sort/order the columns of 'x' (the species or
+	variables) as weighted averages of 'y' (to emphasise the change
+	in composition along 'y'), or using a supplied variable. The
+	latter is useful if you want to sort the variables by their optima
+	with an environmental variable.
+
+	Now also provides a guess as to the y-axis label if none is supplied.
+
 Version 0.6-16
 
 	* roc: For large problems the calculation of AUC and its standard

Modified: pkg/man/Stratiplot.Rd
===================================================================
--- pkg/man/Stratiplot.Rd	2009-08-15 21:17:25 UTC (rev 144)
+++ pkg/man/Stratiplot.Rd	2009-08-15 21:25:23 UTC (rev 145)
@@ -13,11 +13,12 @@
 \usage{
 Stratiplot(x, \dots)
 
-\method{Stratiplot}{default}(x, y, type = "l", ylab = "", xlab = "",
-           pages = 1, \dots)
+\method{Stratiplot}{default}(x, y, type = "l", ylab = NULL, xlab = "",
+           pages = 1, rev = TRUE, sort = c("none", "wa", "var"),
+           svar = NULL, rev.sort = FALSE, \dots)
 
 \method{Stratiplot}{formula}(formula, data, subset, na.action, type = "l",
-           ylab = "", xlab = "", pages = 1, \dots)
+           ylab = NULL, xlab = "", pages = 1, \dots)
 }
 %- maybe also 'usage' for other objects documented here.
 \arguments{
@@ -49,6 +50,16 @@
   \item{ylab, xlab}{the x- and y-axis labels.}
   \item{pages}{numeric; the number of pages to draw the plot over. May
     be useful for data sets with many species.}
+  \item{rev}{logical; should the y-axis limits be reversed}
+  \item{sort}{character; how should the variables (columns) of \code{x}
+    be sorted on the plot. \code{"wa"} sorts by weighted averages
+    of variable \code{svar} if not \code{NULL} or of \code{y}
+    otherwise. The default when \code{"wa"} is specified is to
+    order by wiehgted average of the depth/time axis -- \code{y}. If
+    \code{"var"}, then ordering is done as per the \strong{order} of
+    \code{svar}.}
+  \item{svar}{vector; optional variable to sort columns of \code{x} by.}
+  \item{rev.sort}{logical; should the sorting order be reversed.}
   \item{\dots}{additional arguments passed to
     \code{\link{panel.Stratiplot}} and the underlying
     \code{\link[lattice]{xyplot}} function.}
@@ -85,22 +96,21 @@
 \code{\link{panel.Stratiplot}}, \code{\link{panel.Loess}}.}
 \examples{
 data(V12.122)
-depths <- as.numeric(rownames(V12.122))
+Depths <- as.numeric(rownames(V12.122))
 
-(plt <- Stratiplot(depths ~ O.univ + G.ruber + G.tenel + G.pacR,
-                   data = V12.122,
-                   type = c("h","l","g","smooth")))
+(plt <- Stratiplot(Depths ~ O.univ + G.ruber + G.tenel + G.pacR,
+                   data = V12.122,  type = c("h","l","g","smooth")))
 
-(plt2 <- Stratiplot(depths ~ O.univ + G.ruber + G.tenel + G.pacR,
-                    data = V12.122,
-                    type = c("poly","g")))
+(plt2 <- Stratiplot(Depths ~ O.univ + G.ruber + G.tenel + G.pacR,
+                    data = V12.122, type = c("poly","g")))
 
-(plt3 <- Stratiplot(depths ~ O.univ + G.ruber + G.tenel + G.pacR,
-                    data = V12.122,
-                    type = c("h")))
+## Order taxa by WA in depth --- ephasises change over time
+(plt3 <- Stratiplot(Depths ~ O.univ + G.ruber + G.tenel + G.pacR,
+                    data = V12.122, type = c("h"), sort = "wa"))
 
+## Using the default interface
 spp.want <- c("O.univ","G.ruber","G.tenel","G.pacR")
-(plt4 <- Stratiplot(V12.122[, spp.want], y = depths,
+(plt4 <- Stratiplot(V12.122[, spp.want], y = Depths,
                     type = c("poly", "g")))
 }
 % Add one or more standard keywords, see file 'KEYWORDS' in the



More information about the Analogue-commits mailing list