[Vegan-commits] r1220 - in branches/1.17: R inst man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jun 6 18:17:44 CEST 2010


Author: jarioksa
Date: 2010-06-06 18:17:43 +0200 (Sun, 06 Jun 2010)
New Revision: 1220

Added:
   branches/1.17/R/scores.ordihull.R
Modified:
   branches/1.17/R/ordiarrows.R
   branches/1.17/R/ordiellipse.R
   branches/1.17/R/ordisegments.R
   branches/1.17/R/ordispider.R
   branches/1.17/R/scores.default.R
   branches/1.17/inst/ChangeLog
   branches/1.17/man/ordihull.Rd
Log:
merge to 1.17-3: 'labels' arg in ordiarrows, ordisegments & ordispider

Modified: branches/1.17/R/ordiarrows.R
===================================================================
--- branches/1.17/R/ordiarrows.R	2010-06-06 15:44:09 UTC (rev 1219)
+++ branches/1.17/R/ordiarrows.R	2010-06-06 16:17:43 UTC (rev 1220)
@@ -1,6 +1,6 @@
 "ordiarrows" <-
     function (ord, groups, levels, replicates, display = "sites",
-              show.groups, startmark, ...)
+              show.groups, startmark, label = FALSE, ...)
 {
     pts <- scores(ord, display = display, ...)
     npoints <- nrow(pts)
@@ -13,6 +13,7 @@
     }
     out <- seq(along = groups)
     inds <- names(table(groups))
+    starts <- names <- NULL
     for (is in inds) {
         gr <- out[groups == is]
         if (length(gr) > 1) {
@@ -22,6 +23,10 @@
             nseg <- nrow(X0)
             if (!missing(startmark))
                 points(X0[1,1], X0[1,2], pch=startmark, ...)
+            if (label) {
+                starts <- rbind(starts, X0[1,])
+                names <- c(names, is)
+            }
             if (nseg > 1)
                 ordiArgAbsorber(X0[-nseg,1], X0[-nseg,2], X1[-nseg,1],
                                 X1[-nseg,2], FUN = segments, ...)
@@ -29,5 +34,7 @@
                             FUN = arrows, ...)
         }
     }
+    if (label)
+        ordiArgAbsorber(starts, labels = names, FUN = ordilabel, ...)
     invisible()
 }

Modified: branches/1.17/R/ordiellipse.R
===================================================================
--- branches/1.17/R/ordiellipse.R	2010-06-06 15:44:09 UTC (rev 1219)
+++ branches/1.17/R/ordiellipse.R	2010-06-06 16:17:43 UTC (rev 1220)
@@ -11,6 +11,12 @@
     kind <- match.arg(kind)
     draw <- match.arg(draw)
     pts <- scores(ord, display = display, ...)
+    ## ordiellipse only works with 2D data (2 columns)
+    pts <- as.matrix(pts)
+    if (ncol(pts) > 2)
+        pts <- pts[ , 1:2, drop = FALSE]
+    if (ncol(pts) < 2)
+        stop("ordiellipse needs two dimensions")
     w <- eval(w)
     if (length(w) == 1)
         w <- rep(1, nrow(pts))

Modified: branches/1.17/R/ordisegments.R
===================================================================
--- branches/1.17/R/ordisegments.R	2010-06-06 15:44:09 UTC (rev 1219)
+++ branches/1.17/R/ordisegments.R	2010-06-06 16:17:43 UTC (rev 1220)
@@ -1,6 +1,6 @@
 "ordisegments" <-
     function (ord, groups, levels, replicates, display = "sites",
-              show.groups, ...)
+              show.groups, label = FALSE, ...)
 {
     pts <- scores(ord, display = display, ...)
     npoints <- nrow(pts)
@@ -13,6 +13,7 @@
     }
     out <- seq(along = groups)
     inds <- names(table(groups))
+    ends <- names <- NULL
     for (is in inds) {
         gr <- out[groups == is]
         if (length(gr) > 1) {
@@ -21,7 +22,13 @@
             X1 <- X[-1, , drop = FALSE]
             ordiArgAbsorber(X0[, 1], X0[, 2], X1[, 1], X1[, 2],
                             FUN = segments, ...)
+            if (label) {
+                ends <- rbind(ends, X[c(1, nrow(X)), ])
+                names <- c(names, is, is)
+            }
         }
     }
+    if (label)
+        ordiArgAbsorber(ends, labels = names, FUN = ordilabel, ...)
     invisible()
 }

Modified: branches/1.17/R/ordispider.R
===================================================================
--- branches/1.17/R/ordispider.R	2010-06-06 15:44:09 UTC (rev 1219)
+++ branches/1.17/R/ordispider.R	2010-06-06 16:17:43 UTC (rev 1220)
@@ -1,6 +1,6 @@
 "ordispider" <-
     function (ord, groups, display = "sites", w = weights(ord, display),
-              show.groups, ...)
+              show.groups, label = FALSE, ...)
 {
     weights.default <- function(object, ...) NULL
     if (inherits(ord, "cca") && missing(groups)) {
@@ -11,6 +11,9 @@
         return(invisible())
     }
     pts <- scores(ord, display = display, ...)
+    ## ordihull: draw lines from centre to the points in the hull
+    if (inherits(ord, "ordihull"))
+        groups <- attr(pts, "hulls")
     w <- eval(w)
     if (length(w) == 1)
         w <- rep(1, nrow(pts))
@@ -24,6 +27,8 @@
     }
     out <- seq(along = groups)
     inds <- names(table(groups))
+    if (label) 
+    cntrs <- names <- NULL
     for (is in inds) {
         gr <- out[groups == is]
         if (length(gr) > 1) {
@@ -32,7 +37,13 @@
             ave <- apply(X, 2, weighted.mean, w = W)
             ordiArgAbsorber(ave[1], ave[2], X[, 1], X[, 2],
                             FUN = segments, ...)
+            if (label) {
+                cntrs <- rbind(cntrs, ave)
+                names <- c(names, is)
+            }
         }
     }
+    if (label) 
+        ordiArgAbsorber(cntrs, label = names, FUN = ordilabel, ...)
     invisible()
 }

Modified: branches/1.17/R/scores.default.R
===================================================================
--- branches/1.17/R/scores.default.R	2010-06-06 15:44:09 UTC (rev 1219)
+++ branches/1.17/R/scores.default.R	2010-06-06 16:17:43 UTC (rev 1220)
@@ -37,8 +37,14 @@
             X <- x$c1
         else stop("Can't find scores")
     }
-    else if (is.numeric(x))
+    else if (is.numeric(x)) {
         X <- as.matrix(x)
+        ## as.matrix() changes 1-row scores into 1-col matrix: this is
+        ## a hack which may fail sometimes (but probably less often
+        ## than without this hack):
+        if (ncol(X) == 1 && nrow(X) == length(choices))
+            X <- t(X)
+    }
     if (is.null(rownames(X))) {
         root <- substr(display, 1, 4)
         rownames(X) <- paste(root, 1:nrow(X), sep = "")

Copied: branches/1.17/R/scores.ordihull.R (from rev 1202, pkg/vegan/R/scores.ordihull.R)
===================================================================
--- branches/1.17/R/scores.ordihull.R	                        (rev 0)
+++ branches/1.17/R/scores.ordihull.R	2010-06-06 16:17:43 UTC (rev 1220)
@@ -0,0 +1,11 @@
+## Extract the points in the hull as a one matrix
+`scores.ordihull` <-
+    function(x, ...)
+{
+    out <- NULL
+    for(i in 1:length(x))
+        out <- rbind(out, x[[i]])
+    hulls <- rep(names(x), sapply(x, function(z) NROW(z)))
+    attr(out, "hulls") <- hulls
+    out
+}

Modified: branches/1.17/inst/ChangeLog
===================================================================
--- branches/1.17/inst/ChangeLog	2010-06-06 15:44:09 UTC (rev 1219)
+++ branches/1.17/inst/ChangeLog	2010-06-06 16:17:43 UTC (rev 1220)
@@ -8,11 +8,16 @@
 
 	* merge r1210: tsallis a bit faster.
 
+	* merge r1197:1203: 'label' argument in ordispider, ordisegments
+	and ordiarrows.
+
 	* merge r1200: cca, rda failed when Condition() was a factor, but
 	constraints had no factors.
 	
 	* merge r1190: RsquareAdj.default handles vector arguments.
 
+	* merge r1189: ordiellipse checks that it gets the 2d it can use. 
+
 	* merge r1188: ordiresids de-weights *CA.
 
 	* merge r1185: metaMDS handles NA distances.

Modified: branches/1.17/man/ordihull.Rd
===================================================================
--- branches/1.17/man/ordihull.Rd	2010-06-06 15:44:09 UTC (rev 1219)
+++ branches/1.17/man/ordihull.Rd	2010-06-06 16:17:43 UTC (rev 1220)
@@ -10,6 +10,7 @@
 \alias{weights.rda}
 \alias{weights.decorana}
 \alias{summary.ordihull}
+\alias{scores.ordihull}
 \alias{summary.ordiellipse}
 
 \title{Add Graphical Items to Ordination Diagrams}
@@ -28,11 +29,11 @@
          draw = c("lines","polygon", "none"), w = weights(ord, display),
          show.groups, label = FALSE, ...)
 ordispider(ord, groups, display="sites", w = weights(ord, display),
-         show.groups, ...)
+         show.groups, label = FALSE, ...)
 ordiarrows(ord, groups, levels, replicates, display = "sites",
-         show.groups, startmark, ...)
+         show.groups, startmark, label = FALSE, ...)
 ordisegments(ord, groups, levels, replicates, display = "sites",
-         show.groups, ...)
+         show.groups, label = FALSE, ...)
 ordigrid(ord, levels, replicates, display = "sites",  ...)
 ordicluster(ord, cluster, prune = 0, display = "sites",
          w = weights(ord, display), ...)
@@ -57,10 +58,13 @@
   \item{show.groups}{Show only given groups. This can be a vector, or
     \code{TRUE} if you want to show items for which condition is
     \code{TRUE}. This argument makes it possible to use different
-    colours and line types for groups. The default is to show all
-    groups. }
-  \item{label}{Label the centre of the points in hull or the ellipse with 
-    the group name.}
+    colours and line types for groups. The default is to show all groups. }
+  \item{label}{Label the \code{groups} by their names. In
+    \code{ordiellipse}, \code{ordihull} and \code{ordispider} the the
+    group name is in the centroid of the object, in \code{ordiarrows}
+    in the start of the arrow, and in \code{ordisegments} at both
+    ends. \code{ordiellipse} and \code{ordihull} use standard
+    \code{\link{text}}, and others use \code{\link{ordilabel}}.}
   \item{startmark}{plotting character used to mark the first
     item. The default is to use no mark, and for instance, 
     \code{startmark = 1} will draw a circle.  For other plotting characters,
@@ -102,15 +106,16 @@
   \code{\link[cluster]{ellipsoidhull}} of package \pkg{cluster}.
 
   Functions \code{ordihull} and \code{ordiellipse} return the
-  \code{\link{invisible}} plotting structure. In \code{ordihull} this
-  is a list of coordinates of the hull and in \code{ordiellipse} a
-  list of covariance matrices and scales used in drawing the
-  ellipses. These result objects have a summary method that returns
-  the coordinates of the centres of the ellipses or hulls and their
-  surface areas in user units. The centres of the hulls may differ
-  from the location of the \code{label} which is the centre of the
-  points instead of the centre of the polygon. With \code{draw = "none"}
-  only the result object is returned and nothing is drawn.
+  \code{\link{invisible}} plotting structure. In \code{ordihull} this is
+  a list of coordinates of the hulls (which can be extracted with
+  \code{scores}), and in \code{ordiellipse} a list of covariance
+  matrices and scales used in drawing the ellipses.  These result
+  objects have a \code{summary} method that returns the coordinates of
+  the centres of the ellipses or hulls and their surface areas in user
+  units. The centres of the hulls may differ from the location of the
+  \code{label} which is the centre of the points instead of the centre
+  of the polygon. With \code{draw = "none"} only the result object is
+  returned and nothing is drawn.
 
   Function \code{ordiarrows} draws
   \code{\link{arrows}} and \code{ordisegments} draws line
@@ -120,13 +125,15 @@
   corresponding items among the groups.
 
   Function \code{ordispider} draws a `spider' diagram where each point
-  is connected to the group centroid with
-  \code{\link{segments}}. Weighted centroids are used in the
-  correspondence analysis  methods \code{\link{cca}} and
+  is connected to the group centroid with \code{\link{segments}}. 
+  Weighted centroids are used in the
+  correspondence analysis methods \code{\link{cca}} and
   \code{\link{decorana}} or if the user gives the weights in the
   call. If \code{ordispider} is called with \code{\link{cca}} or
   \code{\link{rda}} result without \code{groups} argument, the function
-  connects each `WA' scores to the corresponding `LC' score.
+  connects each `WA' scores to the corresponding `LC' score. If the
+  argument is a (\code{invisible}) \code{ordihull} object, the functin
+  will connect the points of the hull to their centroid.
 
   Function \code{ordicluster} overlays a cluster dendrogram onto
   ordination. It needs the result from a hierarchic clustering such as
@@ -150,7 +157,6 @@
   \code{ordihull} is \code{\link{chull}}.
 }
 
-
 \examples{
 data(dune)
 data(dune.env)
@@ -162,7 +168,14 @@
 pl <- ordihull(mod, Management, scaling = 3, label = TRUE)
 ## ... and find centres and areas of the hulls
 summary(pl)
-ordispider(mod, col="red", scaling = 3)
+## use ordispider to label and mark the hull
+plot(mod, type = "n")
+pl <- ordihull(mod, Management, scaling = 3)
+ordispider(pl, col="red", lty=3, label = TRUE )
+## ordispider to connect WA and LC scores
+plot(mod, dis=c("wa","lc"), type="p")
+ordispider(mod)
+## Other types of plots
 plot(mod, type = "p", display="sites")
 ordicluster(mod, hclust(vegdist(dune)), prune=3, col = "blue")
 plot(mod, type="n", display = "sites")
@@ -171,4 +184,3 @@
 summary(pl)
 }
 \keyword{aplot }
-



More information about the Vegan-commits mailing list