[CHNOSZ-commits] r601 - in pkg/CHNOSZ: . R demo inst man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Aug 19 08:15:12 CEST 2020


Author: jedick
Date: 2020-08-19 08:15:11 +0200 (Wed, 19 Aug 2020)
New Revision: 601

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/diagram.R
   pkg/CHNOSZ/demo/mosaic.R
   pkg/CHNOSZ/inst/NEWS.Rd
   pkg/CHNOSZ/man/diagram.Rd
Log:
Add 'dx' argument to diagram() and revise demo/mosaic.R


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2020-08-16 11:09:19 UTC (rev 600)
+++ pkg/CHNOSZ/DESCRIPTION	2020-08-19 06:15:11 UTC (rev 601)
@@ -1,6 +1,6 @@
-Date: 2020-08-15
+Date: 2020-08-19
 Package: CHNOSZ
-Version: 1.3.6-74
+Version: 1.3.6-75
 Title: Thermodynamic Calculations and Diagrams for Geochemistry
 Authors at R: c(
     person("Jeffrey", "Dick", , "j3ffdick at gmail.com", role = c("aut", "cre"),

Modified: pkg/CHNOSZ/R/diagram.R
===================================================================
--- pkg/CHNOSZ/R/diagram.R	2020-08-16 11:09:19 UTC (rev 600)
+++ pkg/CHNOSZ/R/diagram.R	2020-08-19 06:15:11 UTC (rev 601)
@@ -31,7 +31,7 @@
   fill.NA="gray80", limit.water=NULL,
   # field and line labels
   names=NULL, format.names=TRUE, bold=FALSE, italic=FALSE,
-  font=par("font"), family=par("family"), adj=0.5, dy=0, srt=0,
+  font=par("font"), family=par("family"), adj=0.5, dx=0, dy=0, srt=0,
   min.area=0,
   # title and legend
   main=NULL, legend.x=NA,
@@ -89,6 +89,14 @@
     plotvar <- "loga.balance"
   }
 
+  ## modify default arguments with add = TRUE
+  if(add) {
+    # change missing fill.NA to transparent
+    if(missing(fill.NA)) fill.NA <- "transparent"
+    # change missing limit.water to FALSE 20200819
+    if(missing(limit.water)) limit.water <- FALSE
+  }
+
   ## number of dimensions (T, P or chemical potentials that are varied)
   # length(eout$vars) - the number of variables = the maximum number of dimensions
   # length(dim(eout$values[[1]])) - nd=1 if it was a transect along multiple variables
@@ -299,9 +307,9 @@
       if(type=="loga.balance" | nd==2) lty <- 1
       else lty <- 1:ngroups
     }
-    lty <- rep(lty, length.out=ngroups)
-    lwd <- rep(lwd, length.out=ngroups)
-    col <- rep(col, length.out=ngroups)
+    lty <- rep(lty, length.out = length(plotvals))
+    lwd <- rep(lwd, length.out = length(plotvals))
+    col <- rep(col, length.out = length(plotvals))
 
     if(nd==0) {
 
@@ -353,10 +361,12 @@
         # 20120521: use legend.x=NA to label lines rather than make legend
         if(is.na(legend.x)) {
           maxvals <- do.call(pmax, pv)
-          dy <- rep(dy, length.out=length(plotvals))
-          srt <- rep(srt, length.out=length(plotvals))
+          # label placement and rotation
+          dx <- rep(dx, length.out = length(plotvals))
+          dy <- rep(dy, length.out = length(plotvals))
+          srt <- rep(srt, length.out = length(plotvals))
           # don't assign to adj becuase that messes up the missing test below
-          alladj <- rep(adj, length.out=length(plotvals))
+          alladj <- rep(adj, length.out = length(plotvals))
           for(i in 1:length(plotvals)) {
             # y-values for this line
             myvals <- as.numeric(plotvals[[i]])
@@ -392,7 +402,7 @@
             }
             # also include y-offset (dy) and y-adjustment (labels bottom-aligned with the line)
             # .. and srt (string rotation) 20171127
-            text(xvalues[imax], plotvals[[i]][imax] + dy[i], labels=names[i], adj=c(thisadj, 0), cex=cex.names, srt=srt[i], font=font, family=family)
+            text(xvalues[imax] + dx[i], plotvals[[i]][imax] + dy[i], labels=names[i], adj=c(thisadj, 0), cex=cex.names, srt=srt[i], font=font, family=family)
           }
         } else legend(x=legend.x, lty=lty, legend=names, col=col, cex=cex.names, lwd=lwd, ...)
       }
@@ -576,9 +586,11 @@
           font <- rep(font, length.out = length(names))
           family <- rep(family, length.out = length(names))
           srt <- rep(srt, length.out = length(names))
+          dx <- rep(dx, length.out = length(names))
+          dy <- rep(dy, length.out = length(names))
           for(i in seq_along(names)) {
             if(!(identical(col[i], 0)) & !is.na(col[i]))
-              text(namesx[i], namesy[i], labels=names[i], cex=cex[i], col=col[i], font=font[i], family=family[i], srt = srt[i])
+              text(namesx[i] + dx[i], namesy[i] + dy[i], labels=names[i], cex=cex[i], col=col[i], font=font[i], family=family[i], srt = srt[i])
           }
         }
         return(list(namesx=namesx, namesy=namesy))
@@ -606,8 +618,6 @@
         }
       }
       fill <- rep(fill, length.out=ngroups)
-      # modify the default for fill.NA
-      if(add & missing(fill.NA)) fill.NA <- "transparent"
       # the x and y values 
       xs <- eout$vals[[1]]
       ys <- eout$vals[[2]]

Modified: pkg/CHNOSZ/demo/mosaic.R
===================================================================
--- pkg/CHNOSZ/demo/mosaic.R	2020-08-16 11:09:19 UTC (rev 600)
+++ pkg/CHNOSZ/demo/mosaic.R	2020-08-19 06:15:11 UTC (rev 601)
@@ -1,50 +1,79 @@
 # CHNOSZ/demo/mosaic.R
-# 20141221 first version
+# 20141221 first version jmd
+# 20200819 revision:
+#   - comment out GC65 thermodynamic data
+#   - use flatten() to show S and C species together
+#   - reorder plot layers to make better use of transparency
+#   - add 'dy' argument to adjust positions of labels
+#   - add legend to show activity of aqueous Fe species
+
 library(CHNOSZ)
 
-# Fe-minerals and aqueous species in Fe-S-O-H-C system
-# after Garrels and Christ, 1965 Figure 7.21
-# to reproduce their diagram as closely as posssible, use their thermodynamic data (from Appendix 2)
-mod.OBIGT(c("Fe+2", "Fe+3"), G = c(-20300, -2520))
-mod.OBIGT(c("hematite", "magnetite", "pyrrhotite", "pyrite", "siderite"), G = c(-177100, -242400, -23320, -36000, -161060))
-mod.OBIGT(c("SO4-2", "HS-", "H2S", "HSO4-"), G = c(-177340, 3010, -6540, -179940))
-mod.OBIGT(c("CO2", "HCO3-", "CO3-2"), G = c(-92310, -140310, -126220))
-# conditions and system definition
-pH <- c(0, 14, 500)
-Eh <- c(-1, 1, 500)
+# Fe-bearing minerals and aqueous species in the Fe-S-O-H-C system
+# after Figure 7.21 of Garrels and Christ, 1965
+
+## Uncomment these lines to use thermodynamic data from Appendix 2 of GC65 (at 25 degrees C only)
+#mod.OBIGT(c("Fe+2", "Fe+3"), G = c(-20300, -2520))
+#mod.OBIGT(c("hematite", "magnetite", "pyrrhotite", "pyrite", "siderite"), G = c(-177100, -242400, -23320, -36000, -161060))
+#mod.OBIGT(c("SO4-2", "HS-", "H2S", "HSO4-"), G = c(-177340, 3010, -6540, -179940))
+#mod.OBIGT(c("CO2", "HCO3-", "CO3-2"), G = c(-92310, -140310, -126220))
+
+# Define conditions
+res <- 500
+pH <- c(0, 14, res)
+Eh <- c(-1, 1, res)
 T <- 25
+loga_S <- -6
+loga_C <- 0
+# Define chemical system
 basis(c("FeO", "SO4-2", "H2O", "H+", "e-", "CO3-2"))
-basis("SO4-2", -6)
-basis("CO3-2", 0)
-species(c("Fe+2", "Fe+3"), -6)
+basis("SO4-2", loga_S)
+basis("CO3-2", loga_C)
+species(c("Fe+2", "Fe+3"), -4)
 species(c("pyrrhotite", "pyrite", "hematite", "magnetite", "siderite"), add = TRUE)
-# two sets of changing basis species:
-# speciate SO4-2, HSO4-, HS-, H2S as a function of Eh and pH
-# speciate CO3-2, HCO3-, CO2 as a function of pH
+
+# Use two sets of changing basis species:
+#   speciate SO4-2, HSO4-, HS-, H2S as a function of Eh and pH
+#   speciate CO3-2, HCO3-, CO2 as a function of pH
 bases <- c("SO4-2", "HSO4-", "HS-", "H2S")
 bases2 <- c("CO3-2", "HCO3-", "CO2")
-# calculate affinities using the relative abundances of different basis species
-# (using default blend = TRUE)
-# note curved lines, particularly at the boundaries with siderite
-m1 <- mosaic(bases, bases2, pH = pH, Eh = Eh, T = T)
-# make a diagram and add water stability lines
-diagram(m1$A.species, lwd = 2, limit.water = TRUE)
-water.lines(m1$A.species, col = "seagreen", lwd = 1.5)
-# show lines for Fe(aq) = 10^-4 M
-species(c("Fe+2", "Fe+3"), -4)
-m2 <- mosaic(bases, bases2, pH = pH, Eh = Eh, T = T)
-diagram(m2$A.species, add = TRUE, names = FALSE, limit.water = TRUE)
-# overlay the sulfur and carbonate basis species predominance fields
-d <- diagram(m1$A.bases, add = TRUE, col = "red", col.names = "red", lty = 3)
-d <- diagram(m1$A.bases2, add = TRUE, col = "blue", names = FALSE, lty = 3)
-text(d$namesx, -0.8, as.expression(sapply(m1$A.bases2$species$name, expr.species)), col = "blue")
-# add legend and title
-dP <- describe.property(c("T", "P"), c(25, 1))
-legend("top", dP, bty = "n")
-dS <- expression(sum(S)*"(aq)" == 10^-6~italic(m))
-dC <- expression(sum(C)*"(aq)" == 1~italic(m))
-legend("topright", c(dS, dC), bty = "n")
-title(main=paste("Iron minerals, sulfur, and carbonate in water,",
+
+# Make a diagram with log(activity of aqueous Fe species) = -4
+m4 <- mosaic(bases, bases2, pH = pH, Eh = Eh, T = T)
+diagram(m4$A.species, lty = 2, names = FALSE)
+
+## Show the predominance fields for the sulfur and carbonate basis species
+dS <- diagram(m4$A.bases, italic = TRUE, plot.it = FALSE)
+dC <- diagram(m4$A.bases2, italic = TRUE, plot.it = FALSE)
+dSC <- flatten(dS, dC)
+diagram(dSC, lty = 3, col = 4, col.names = 4, add = TRUE)
+
+# Show lines for log(activity of aqueous Fe species) = -6
+s6 <- species(c("Fe+2", "Fe+3"), -6)
+m6 <- mosaic(bases, bases2, pH = pH, Eh = Eh, T = T)
+srt <- dy <- numeric(nrow(s6))
+dy[s6$name == "Fe+2"] <- 0.15
+dy[s6$name == "hematite"] <- -0.4
+dy[s6$name == "magnetite"] <- 0.07
+dy[s6$name == "siderite"] <- 0.2
+srt[s6$name == "pyrite"] <- -25
+bold <- s6$state == "cr"
+diagram(m6$A.species, add = TRUE, dy = dy, srt = srt, bold = bold)
+
+# Add legend and title
+TP <- describe.property(c("T", "P"), c(25, 1))
+SC <- c(
+  bquote(log * italic(a)["S(aq)"] == .(loga_S)),
+  bquote(log * italic(a)["C(aq)"] == .(loga_C))
+)
+Fe <- c(
+  bquote(log * italic(a)["Fe(aq)"] == .(-4)),
+  bquote(log * italic(a)["Fe(aq)"] == .(-6))
+)
+legend <- lex(TP, SC, Fe)
+legend("topright", legend, lty = c(NA, NA, NA, NA, 1, 2), bty = "n")
+title(main = paste("Iron minerals, sulfur, and carbonate in water,",
   "after Garrels and Christ, 1965, Figure 7.21", sep = "\n"), font.main = 1)
-# reset the database, as it was changed in this example
-reset()
+
+## Reset the database if we changed it using mod.OBIGT()
+#reset()

Modified: pkg/CHNOSZ/inst/NEWS.Rd
===================================================================
--- pkg/CHNOSZ/inst/NEWS.Rd	2020-08-16 11:09:19 UTC (rev 600)
+++ pkg/CHNOSZ/inst/NEWS.Rd	2020-08-19 06:15:11 UTC (rev 601)
@@ -9,7 +9,7 @@
 \newcommand{\s}{\ifelse{latex}{\eqn{_{#1}}}{\ifelse{html}{\out{<sub>#1</sub>}}{#1}}}
 \newcommand{\S}{\ifelse{latex}{\eqn{^{#1}}}{\ifelse{html}{\out{<sup>#1</sup>}}{^#1}}}
 
-\section{Changes in CHNOSZ version 1.3.6-74 (2020-08-16)}{
+\section{Changes in CHNOSZ version 1.3.6-75 (2020-08-19)}{
 
   \subsection{MAJOR CHANGES}{
     \itemize{
@@ -149,6 +149,10 @@
 
       \item New demo \samp{berman.R}, extracted from \samp{berman.Rd}.
 
+      \item Revise demo \samp{mosaic.R} to use \code{flatten()} to show S and C
+      species together, make better use of transparency, and \code{dy} argument
+      to adjust positions of labels.
+
     }
   }
 
@@ -167,8 +171,9 @@
       \item Add a \strong{bottom} argument to \code{ratlab()} to allow changing
       the ion in the denominator to something other than \Hplus.
 
-      \item The \strong{srt} argument in \code{diagram()} can now be used to
-      rotate field labels, not only line labels. This and other arguments
+      \item The \strong{srt}, \strong{dx} (new), and \strong{dy} arguments in
+      \code{diagram()} can now be used to rotate and adjust the position of
+      field labels, not only line labels. This and other arguments
       (\strong{cex}, \strong{col}, \strong{col.names}, \strong{font},
       \strong{family}, \strong{bold}, \strong{italic}) can have length > 1 to
       apply different settings to each species.
@@ -264,6 +269,8 @@
 
       \item TODO: add empty diagram -> exceed.Ttr hint to anintro.Rmd.
 
+      \item TODO: change ugly 'heat' colors in anintro.Rmd.
+
     }
   }
 

Modified: pkg/CHNOSZ/man/diagram.Rd
===================================================================
--- pkg/CHNOSZ/man/diagram.Rd	2020-08-16 11:09:19 UTC (rev 600)
+++ pkg/CHNOSZ/man/diagram.Rd	2020-08-19 06:15:11 UTC (rev 601)
@@ -28,8 +28,8 @@
     fill.NA = "gray80", limit.water = NULL,
     # field and line labels
     names = NULL, format.names = TRUE, bold = FALSE, italic = FALSE, 
-    font = par("font"), family = par("family"), adj = 0.5, dy = 0, srt = 0,
-    min.area = 0,
+    font = par("font"), family = par("family"), adj = 0.5,
+    dx = 0, dy = 0, srt = 0, min.area = 0,
     # title and legend
     main = NULL, legend.x = NA,
     # plotting controls
@@ -75,7 +75,8 @@
   \item{font}{character, font type for names (has no effect if \code{format.names} is TRUE)}
   \item{family}{character, font family for names}
   \item{adj}{numeric, adjustment for line labels}
-  \item{dy}{numeric, y offset for line labels}
+  \item{dx}{numeric, x offset for line or field labels}
+  \item{dy}{numeric, y offset for line or field labels}
   \item{srt}{numeric, rotation for line labels}
   \item{min.area}{numeric, minimum area of fields that should be labeled, expressed as a fraction of the total plot area}
   \item{main}{character, a main \code{\link{title}} for the plot; \code{NULL} means to plot no title}
@@ -143,10 +144,12 @@
 
 \code{fill.NA} gives the color for empty fields, i.e. points at which NA values are present for any species.
 This may occur when there are missing thermodynamic data or the temperature or pressure are not in the range of the equations of state.
-The default for \code{fill.NA} is automatically changed to \samp{transparent} when \code{add} is TRUE.
+To make overlay diagrams easier to construct, the default for \code{fill.NA} is automatically changed to \samp{transparent} when \code{add} is TRUE.
 
-\code{fill.NA} is also used for all areas outside the water stability region on Eh-pH and other diagrams when \code{limit.water} is TRUE.
-The default of NULL for \code{limit.water} causes the main diagram to be overlaid on the water stability regions; change this to FALSE to not show the water limits at all.
+If \code{limit.water} is TRUE, the diagram is clipped to the the water stability region on Eh-pH (and some other) diagrams.
+That is, predominance fields are shown only where water is stable, and \code{fill.NA} is used for areas where \H2O is not stable.
+The default of NULL for \code{limit.water} does not clip the main diagram but instead overlays it on the water stability fields.
+Change \code{limit.water} to FALSE to not show the water stability regions at all; this is automatically done if \code{limit.water} is missing and \code{add} is TRUE.
 
 The default line-drawing algorithm uses \code{\link{contourLines}} to obtain smooth-looking diagonal and curved lines, at the expense of not coinciding exactly with the rectangular grid that is used for drawing colors.
 \code{lty}, \code{col}, and \code{lwd} can be specified, but limiting the lines via \code{xrange} is not currently supported.



More information about the CHNOSZ-commits mailing list