[Vegan-commits] r2536 - in pkg/vegan: R inst

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 26 08:57:12 CEST 2013


Author: jarioksa
Date: 2013-06-26 08:57:12 +0200 (Wed, 26 Jun 2013)
New Revision: 2536

Modified:
   pkg/vegan/R/plot.envfit.R
   pkg/vegan/inst/ChangeLog
Log:
fix string positioning and xlim,ylim scaling in plot.envfit(..., add = TRUE)

Modified: pkg/vegan/R/plot.envfit.R
===================================================================
--- pkg/vegan/R/plot.envfit.R	2013-06-22 12:51:56 UTC (rev 2535)
+++ pkg/vegan/R/plot.envfit.R	2013-06-26 06:57:12 UTC (rev 2536)
@@ -59,52 +59,67 @@
             ax <- -c(-1, 0, 1) * arrow.mul * maxarr
         }
         vect <- arrow.mul * vect
-        vtext <- ordiArrowTextXY(vect, labs$v, ...)
-        vtext <- sweep(vtext, 2, at, "+")
         vect <- sweep(vect, 2, at, "+")
+        if (add) {
+            vtext <- ordiArrowTextXY(vect, labs$v, ...)
+            vtext <- sweep(vtext, 2, at, "+")
+        }
     }
     if (!add) {
-        plot.new() ## needed for string widths and heights
-        if(!is.null(vect)) {
-            ## compute axis limits allowing space for labels
-            sw <- strwidth(labs$v, ...)
+        plot.new()
+        ## needed for string widths and heights We need dimensions to
+        ## fit completely the names of vectors and factors with no
+        ## clipping at axes. If there are (1) factors and vectors, we
+        ## need to adjust arrow lengths, if there are (2) only factors
+        ## or only vectors, we can use their scores directly. After
+        ## finding the scores, we must expand the scores by string
+        ## widths and heights. The expansion can be only estimated
+        ## after setting plot.window with its xlim and ylim, but we
+        ## need to find xlim and ylim to set the plot.window...
+
+        if(is.null(vect) || is.null(x$factors)) {
+            ## Only factors or vectors: set preliminary plot.window
+            xstack <- rbind(vect, x$factors$centroids)
+            plot.window(xlim = range(xstack[,1], at[1]),
+                        ylim = range(xstack[,2], at[2]),
+                        asp = 1, ...)
+        } else {
+            ## Both vectors and factors: set preliminary plot.window
+            ## from factors only and and find arrow.mul (which is
+            ## otherwise ## arrow.mul = 1)
+            plot.window(xlim = range(x$factors$centroids[,1], at[1]),
+                        ylim = range(x$factors$centroids[,2], at[2]),
+                        asp = 1, ...)
+            vfill <- 0.75
+            arrow.mul <- ordiArrowMul(vect, at = at, fill = 1)
+            vect <- arrow.mul * vect
+        }
+        ## Get string dimensions (width/2, height)
+        sw <- strwidth(c(labs$v, labs$f), ...) / 2
+        sh <- strheight(c(labs$v, labs$f), ...)
+        ## Reset limits
+        xstack <- rbind(x$factors$centroids, vect)
+        xlim <- range(xstack[,1] + sw, xstack[,2] - sw)
+        ylim <- range(xstack[,2] + sh, xstack[,2] - sh)
+        plot.window(xlim = xlim, ylim = ylim, asp = 1, ...)
+        ## Re-evaluate arrow.mul, set its text and re-evaluate limits again 
+        if (!is.null(vect)) {
+            arrow.mul <- ordiArrowMul(vect, at = at, fill = 1)
+            vect <- arrow.mul * vect
+            vtext <- ordiArrowTextXY(vect, labs$v, ...)
+            sw <- strwidth(labs$v, ...) / 2
             sh <- strheight(labs$v, ...)
-            xlim <- range(at[1], vtext[,1] + sw, vtext[,1] - sw)
-            ylim <- range(at[2], vtext[,2] + sh, vtext[,2] - sh)
-            if(!is.null(x$factors)) {
-                ## if factors, also need to consider them
-                sw <- strwidth(labs$f, ...)
-                sh <- strheight(labs$f, ...)
-                xlim <- range(xlim, x$factors$centroids[, choices[1]] + sw,
-                              x$factors$centroids[, choices[1]] - sw)
-                ylim <- range(ylim, x$factors$centroids[, choices[2]] + sh,
-                              x$factors$centroids[, choices[2]] - sh)
-            }
-            ## these plotting calls will prob. generate warnings
-            ## because of passing ... everywhere. localFoo needed?
+            xlim <- range(xlim, vtext[,1] + sw, vtext[,1] - sw)
+            ylim <- range(xlim, vtext[,2] + sh, vtext[,2] - sh)
             plot.window(xlim = xlim, ylim = ylim, asp = 1, ...)
-            axis(side = 1, ...)
-            axis(side = 2, ...)
-            box(...)
-            alabs <- colnames(vect)
-            title(..., ylab = alabs[2], xlab = alabs[1])
-        } else if (!is.null(x$factors)) {
-            sw <- strwidth(labs$f, ...)
-            sh <- strheight(labs$f, ...)
-            xlim <- range(at[1], x$factors$centroids[, choices[1]] + sw,
-                          x$factors$centroids[, choices[1]] - sw)
-            ylim <- range(at[2], x$factors$centroids[, choices[2]] + sh,
-                          x$factors$centroids[, choices[2]] - sh)
-            ## these plotting calls will prob. generate warnings
-            ## because of passing ... everywhere. localFoo needed?
-            plot.window(xlim = xlim, ylim = ylim, asp = 1, ...)
-            axis(side = 1, ...)
-            axis(side = 2, ...)
-            box(...)
-            alabs <- colnames(x$factors$centroids[, choices, drop = FALSE])
-            title(..., ylab = alabs[2], xlab = alabs[1])
-        } else stop("Nothing to plot")
+        }
+        axis(side = 1, ...)
+        axis(side = 2, ...)
+        box(...)
+        alabs <- colnames(vect)
+        title(..., ylab = alabs[2], xlab = alabs[1])
     }
+    
     if (!is.null(vect)) {
         arrows(at[1], at[2], vect[, 1], vect[, 2], len = 0.05,
                col = col)

Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog	2013-06-22 12:51:56 UTC (rev 2535)
+++ pkg/vegan/inst/ChangeLog	2013-06-26 06:57:12 UTC (rev 2536)
@@ -32,6 +32,13 @@
 	* plot.envfit, plot.cca, text.cca: use ordiArrowTextXY() for arrow
 	labels instead of expanding arrow heads by 10%.
 
+	* plot.envfit: plot(..., add = FALSE) estimated string (text)
+	dimensions after plot.new() but before plot.window(). Since
+	plot.new() sets xlim, ylim to c(0,1) and then plot.window resets
+	the limits to the data values, string dimensions in user units
+	were poorly estimated. This became evident with new positioning of
+	arrow text based on string dimensions.
+
 Version 2.1-30 (opened May 5, 2013)
 
 	* bioenv: can now use Mahalanobis, Manhattan and Gower distances



More information about the Vegan-commits mailing list