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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Sep 29 18:45:12 CEST 2010


Author: gsimpson
Date: 2010-09-29 18:45:12 +0200 (Wed, 29 Sep 2010)
New Revision: 194

Modified:
   pkg/R/Stratiplot.R
   pkg/inst/ChangeLog
   pkg/man/Stratiplot.Rd
Log:
Adds ability to draw a zone legend on the right of the plot

Modified: pkg/R/Stratiplot.R
===================================================================
--- pkg/R/Stratiplot.R	2010-09-29 15:56:24 UTC (rev 193)
+++ pkg/R/Stratiplot.R	2010-09-29 16:45:12 UTC (rev 194)
@@ -14,6 +14,8 @@
                                  rev.sort = FALSE,
                                  strip = FALSE,
                                  topPad = 6,
+                                 zoneNames = NULL,
+                                 drawLegend = TRUE,
                                  ...) {
     ## inline function for custom axis
     axis.VarLabs <- function(side, ...) {
@@ -75,9 +77,11 @@
     ## add padYlim * range as per base graphics
     padY <- 0.01
     if(missing(ylim)) {
-        diffy <- padY * (maxy - miny)
-        ylim <- c(miny - diffy, maxy + diffy)
+        ##diffy <- padY * (maxy - miny)
+        diffy <- maxy - miny
+        ylim <- c(miny - (padY * diffy), maxy + (padY * diffy))
     } else {
+        ## should these be ylim[1] and ylim[2] ???
         minLim <- min(ylim)
         maxLim <- max(ylim)
         ## add padY * range as per base graphics
@@ -105,6 +109,41 @@
         str.max <- max(sapply(levels(sx$ind), convWidth, gp))
         str.max <- ceiling(str.max) + topPad
     }
+    ## Legend specification for Zones
+    dotArgs <- list(...)
+    if("zones" %in% names(dotArgs) && drawLegend) {
+        Zones <- sort(c(ylim, dotArgs$zones), decreasing = rev)
+        Heights <- abs(diff(Zones))
+        Ydiff <- abs(diff(ylim))
+        Heights <- Heights / Ydiff
+        Ylocs <- cumsum(c(0, Heights[-length(Heights)]))
+        ZoneUnits <- function(h, units = "npc") {
+            unit(h, units = units)
+        }
+        HeightUnits <- do.call(unit.c, lapply(Heights, ZoneUnits))
+        MidUnits <- do.call(unit.c, lapply(Heights/2, ZoneUnits))
+        YlocsUnits <- do.call(unit.c, lapply(Ylocs, ZoneUnits))
+        ## rectangles to draw
+        zoneRects <- rectGrob(y = YlocsUnits, width = unit(1.5, "cm"),
+                              height = HeightUnits, vjust = 0)
+        if(is.null(zoneNames))
+            zoneNames <- paste("Zone", seq_along(c(1, dotArgs$zones)))
+        labelText <- rep(zoneNames, length = length(dotArgs$zones) + 1)
+        labelYlocs <- YlocsUnits + MidUnits
+        zoneLabels <- textGrob(label = labelText, y = labelYlocs,
+                               just = rep("center",2),
+                               default.units = "npc")
+        key.layout <- grid.layout(nrow = 1, ncol = 1,
+                                  heights = unit(1, "null"),
+                                  widths = unit(1.5, "cm"),
+                                  respect = FALSE)
+        key.gf <- frameGrob(layout = key.layout)
+        key.gf <- placeGrob(key.gf, zoneRects, row = 1, col = 1)
+        key.gf <- placeGrob(key.gf, zoneLabels, row = 1, col = 1)
+        Legend <- list(right = list(fun = key.gf))
+    } else {
+        Legend <- NULL
+    }
     ## plotting
     xyplot(y ~ values | ind,
            data = sx,
@@ -120,5 +159,6 @@
            par.settings = list(layout.widths = list(panel = max.abun),
            layout.heights = list(top.padding = str.max)),
            axis = if(isTRUE(strip)) {axis.default} else {axis.VarLabs},
+           legend = Legend,
            ...)
 }

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-09-29 15:56:24 UTC (rev 193)
+++ pkg/inst/ChangeLog	2010-09-29 16:45:12 UTC (rev 194)
@@ -18,6 +18,12 @@
 
 	* tran: improvements to the underlying code.
 
+	* Stratiplot: if 'zones' are supplied, a legend on the right-hand
+	side of the diagram can be drawn by setting argument 'drawLegend'
+	to TRUE (the default). Currently, only simple blocks that
+	demarcate the zone boundaries are drawn and labelled using
+	argument 'zoneNames'.
+
 Version 0.6-26
 
 	* abernethy: New data set containing the classic Abernethy Forest

Modified: pkg/man/Stratiplot.Rd
===================================================================
--- pkg/man/Stratiplot.Rd	2010-09-29 15:56:24 UTC (rev 193)
+++ pkg/man/Stratiplot.Rd	2010-09-29 16:45:12 UTC (rev 194)
@@ -15,7 +15,8 @@
 
 \method{Stratiplot}{default}(x, y, type = "l", ylab = NULL, xlab = "",
            pages = 1, rev = TRUE, ylim, sort = c("none", "wa", "var"),
-           svar = NULL, rev.sort = FALSE, strip = FALSE, topPad =6, \dots)
+           svar = NULL, rev.sort = FALSE, strip = FALSE, topPad =6,
+           zoneNames = NULL, drawLegend = TRUE, \dots)
 
 \method{Stratiplot}{formula}(formula, data, subset, na.action, type = "l",
            ylab = NULL, xlab = "", pages = 1, \dots)
@@ -74,6 +75,10 @@
     actual space required can be automagically calculated from the
     variable names themselves. The currently gets most of the way there,
     but \code{topPad} is used to add some extra space if required.}
+  \item{zoneNames}{character vector of labels, one per zone, with which
+    to label the zone legend, if drawn (see argument
+    \code{drawLegend}). See Details.} 
+  \item{drawLegend}{logical; should a legend for the zones}
   \item{\dots}{additional arguments passed to
     \code{\link{panel.Stratiplot}} and the underlying
     \code{\link[lattice]{xyplot}} function.}
@@ -96,6 +101,23 @@
   within \code{Stratiplot} and an appropriate data structure formed to
   facilitate plotting via \code{\link[lattice]{xyplot}}. As such, the special
   features of \pkg{Lattice} formulae cannot be used.
+
+  If zones are drawn on the stratigraphic plot, the \code{zoneNames}
+  argument can be used to supply a set of names with which to label the
+  zones using a legend. This legend is drawn on the right-hand side of
+  the the straigraphic diagram if \code{drawLegend = TRUE} is
+  supplied. The \code{zoneNames} must be supplied in stratigraphic
+  order, as that is the order in which they are drawn in the
+  legend. Whether this ordering is reversed or not will depend on the
+  value of argument \code{rev}. It is up to the user to provide the
+  labels in the correct order. Zones are specified by the zone
+  boundaries (excluding the core sequence top and bottom), and as a
+  result 1 more label is required than the number of zone boundaries
+  supplied. If no \code{zoneNames} is not supplied, but a legend is
+  requested, suitable names will be produced. If you do not wish to have
+  any labelling at all, then set \code{zoneNames = ""} as this will get
+  recycled to the correct length. See the Example section for an
+  illustration of how this drawing zones works. 
 }
 \value{
   Returns a Lattice plot object of class \code{"trellis"}.
@@ -126,6 +148,34 @@
 spp.want <- c("O.univ","G.ruber","G.tenel","G.pacR")
 (plt4 <- Stratiplot(V12.122[, spp.want], y = Depths,
                     type = c("poly", "g")))
+
+## Addin zones to a Stratigraphic plot
+
+## Default labelling and draw zone legend
+## Here we choose 4 arbitrary Depths as the zone boundaries
+set.seed(123)
+Zones <-sample(Depths, 4)
+(plt5 <- Stratiplot(Depths ~ O.univ + G.ruber + G.tenel + G.pacR,
+                    data = V12.122, type = c("poly","g"),
+                    zones = Zones))
+
+## As before, but supplying your own zone labels
+zone.labs <- c("A","B","C","D","E")
+(plt6 <- Stratiplot(Depths ~ O.univ + G.ruber + G.tenel + G.pacR,
+                    data = V12.122, type = c("poly","g"),
+                    zones = Zones, zoneNames = zone.labs))
+
+## Suppress the drawing of the zone legend
+(plt7 <- Stratiplot(Depths ~ O.univ + G.ruber + G.tenel + G.pacR,
+                    data = V12.122, type = c("poly","g"),
+                    zones = Zones, drawLegend = FALSE))
+
+## Add zones and draw a legend, but do not label the zones
+(plt8 <- Stratiplot(Depths ~ O.univ + G.ruber + G.tenel + G.pacR,
+                    data = V12.122, type = c("poly","g"),
+                    zones = Zones, zoneNames = ""))
+
+
 }
 % Add one or more standard keywords, see file 'KEYWORDS' in the
 % R documentation directory.



More information about the Analogue-commits mailing list