[CHNOSZ-commits] r731 - in pkg/CHNOSZ: . R inst man vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jun 20 11:04:39 CEST 2022


Author: jedick
Date: 2022-06-20 11:04:39 +0200 (Mon, 20 Jun 2022)
New Revision: 731

Added:
   pkg/CHNOSZ/R/stack_mosaic.R
   pkg/CHNOSZ/man/stack_mosaic.Rd
Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/NAMESPACE
   pkg/CHNOSZ/inst/NEWS.Rd
   pkg/CHNOSZ/man/mosaic.Rd
   pkg/CHNOSZ/vignettes/multi-metal.Rmd
Log:
Add stack_mosaic()


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2022-06-20 05:41:11 UTC (rev 730)
+++ pkg/CHNOSZ/DESCRIPTION	2022-06-20 09:04:39 UTC (rev 731)
@@ -1,6 +1,6 @@
 Date: 2022-06-20
 Package: CHNOSZ
-Version: 1.9.9-23
+Version: 1.9.9-24
 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/NAMESPACE
===================================================================
--- pkg/CHNOSZ/NAMESPACE	2022-06-20 05:41:11 UTC (rev 730)
+++ pkg/CHNOSZ/NAMESPACE	2022-06-20 09:04:39 UTC (rev 731)
@@ -58,7 +58,9 @@
 # added 20220324
   "logB_to_OBIGT",
 # added 20220416
-  "rank_affinity"
+  "rank_affinity",
+# added 20220620
+  "stack_mosaic", "add_alpha"
 )
 
 # Load shared objects
@@ -68,7 +70,7 @@
 # Imports from default packages
 importFrom("grDevices", "dev.cur", "dev.off", "extendrange",
   "heat.colors", "png", "rainbow", "topo.colors", "dev.list",
-  "contourLines")
+  "contourLines", "col2rgb", "rgb")
 importFrom("graphics", "abline", "axTicks", "axis", "barplot", "box",
   "contour", "image", "legend", "lines", "mtext", "par", "plot",
   "plot.new", "plot.window", "points", "rect", "text", "title")

Added: pkg/CHNOSZ/R/stack_mosaic.R
===================================================================
--- pkg/CHNOSZ/R/stack_mosaic.R	                        (rev 0)
+++ pkg/CHNOSZ/R/stack_mosaic.R	2022-06-20 09:04:39 UTC (rev 731)
@@ -0,0 +1,56 @@
+# CHNOSZ/stack_mosaic.R
+
+# Function to create mosaic stack 20220617
+# Adapted from vignettes/multi-metal.Rmd
+# col: Colors for species1, species2, and species12
+#   (default of NA for col[3] means to plot species12 boundaries with color for species2)
+# ...: Arguments for mosaic() (incl. affinity() arguments)
+stack_mosaic <- function(bases, species1, species2, species12, names = NULL, col = list(4, 3, 6), col.names = list(4, 3, 6),
+  fill = NULL, dx = list(0, 0, 0), dy = list(0, 0, 0), srt = list(0, 0, 0), lwd = list(1, 1, 1), lty = list(1, 1, 1), ...) {
+
+  # Default is to use semi-transparent fill for bimetallic species
+  if(is.null(fill)) fill <- list(NA, NA, add_alpha(col.names[3], "50"))
+
+  # Load species1 (first metal-bearing species)
+  species(species1)
+  # Calculate affinity of species1 while speciating bases (e.g. aqueous S species)
+  mosaic1 <- mosaic(bases, ...)
+  # Show predominance fields
+  diagram1 <- diagram(mosaic1$A.species, names = names[[1]], col = col[[1]], col.names = col.names[[1]], fill = fill[[1]],
+    dx = dx[[1]], dy = dy[[1]], srt = srt[[1]], lwd = lwd[[1]], lty = lty[[1]])
+
+  # Load species12 (bimetallic species) and species2 (second metal-bearing species)
+  species(c(species12, species2))
+  # Speciate bases again (NULL)
+  # Take the predominant members of species1 (diagram1$predominant)
+  mosaic2 <- mosaic(list(bases, species1), stable = list(NULL, diagram1$predominant), ...)
+  # Set colors
+  col <- c(rep_len(col[[3]], length(species12)), rep_len(col[[2]], length(species2)))
+  col.names <- c(rep_len(col.names[[3]], length(species12)), rep_len(col.names[[2]], length(species2)))
+  # For NULL names, use the species names
+  if(is.null(names[[3]])) names[[3]] <- species12
+  if(is.null(names[[2]])) names[[2]] <- species2
+  names <- c(names[[3]], names[[2]])
+  # Set other parameters
+  dx <- c(rep_len(dx[[3]], length(species12)), rep_len(dx[[2]], length(species2)))
+  dy <- c(rep_len(dy[[3]], length(species12)), rep_len(dy[[2]], length(species2)))
+  srt <- c(rep_len(srt[[3]], length(species12)), rep_len(srt[[2]], length(species2)))
+  lwd <- c(rep_len(lwd[[3]], length(species12)), rep_len(lwd[[2]], length(species2)))
+  lty <- c(rep_len(lty[[3]], length(species12)), rep_len(lty[[2]], length(species2)))
+  fill <- c(rep_len(fill[[3]], length(species12)), rep_len(fill[[2]], length(species2)))
+  diagram2 <- diagram(mosaic2$A.species, add = TRUE, names = names, col = col, col.names = col.names, fill = fill,
+    dx = dx, dy = dy, srt = srt, lwd = lwd, lty = lty)
+
+  out <- list(diagram1, diagram2)
+  invisible(out)
+
+}
+
+# Function to add transparency to given color 20220223
+add_alpha <- function(col, alpha) {
+  x <- col2rgb(col)
+  newcol <- rgb(x[1], x[2], x[3], maxColorValue = 255)
+  newcol <- paste0(newcol, alpha)
+  newcol
+}
+

Modified: pkg/CHNOSZ/inst/NEWS.Rd
===================================================================
--- pkg/CHNOSZ/inst/NEWS.Rd	2022-06-20 05:41:11 UTC (rev 730)
+++ pkg/CHNOSZ/inst/NEWS.Rd	2022-06-20 09:04:39 UTC (rev 731)
@@ -10,7 +10,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.9.9-22 (2022-06-20)}{
+\section{Changes in CHNOSZ version 1.9.9-24 (2022-06-20)}{
 
   \subsection{MAJOR CHANGE}{
     \itemize{
@@ -59,33 +59,31 @@
     }
   }
 
-  \subsection{NEW FEATURES: PROTEINS}{
+  \subsection{NEW FEATURES: DIAGRAMS}{
     \itemize{
 
-      \item Add an \strong{as.residue} argument to \code{add.protein()} to
-      normalize amino acid compositions by protein length.
+      \item Add \code{stack_mosaic()} to create stacked mosaic diagrams, where
+      the species formed in the first layer become the basis species for the
+      species formed in the second layer. This function implements the main
+      steps for making mosaic diagrams for bimetallic systems described in
+      \strong{multi-metal.Rmd}.
 
-      \item Add a check to \code{add.protein()} that all new protein IDs
-      (protein name + organism name) are unique.
-
-      \item Add function and demo for \code{rank_affinity()} to calculate (sums
-      of ranks of affinities for species in designated groups) normalized by
-      number of species in each group. The output of the new function can be
-      used by \code{diagram()}.
-
     }
   }
 
-  \subsection{OTHER CHANGES}{
+  \subsection{NEW FEATURES: PROTEINS}{
     \itemize{
 
-      \item Add a \strong{zap} argument to \code{mod.OBIGT()} to clear
-      parameters of preexisting species (used by \code{logB_to_OBIGT()}).
+      \item Add function and demo for \code{rank_affinity()} to calculate means
+      of affinity rankings for specified groups of proteins, intended for
+      evolutionary comparisons. The output of the new function can be used by
+      \code{diagram()}.
 
-      \item Change default for \code{affinity()} to \code{loga.protein = 0}
-      (was -3).
+      \item Add an \strong{as.residue} argument to \code{add.protein()} to
+      normalize amino acid compositions by protein length.
 
-      \item Change license from GPL (>= 2) to GPL-3.
+      \item Add a check to \code{add.protein()} that all new protein IDs
+      (protein name + organism name) are unique.
 
     }
   }
@@ -117,14 +115,14 @@
 
       \item \code{diagram()}: Improve handling of length > 2 \code{col},
       \code{lty} and \code{lwd} arguments for predominance diagrams. Because
-      field boundaries are drawn for the first species, then removing that
-      species before drawing boundaries for the second species, etc., species
-      for which different-colored or -styled lines are desired should be placed
-      at the top of the species list. This change makes possible one less call
-      to \code{diagram()} in the Mosaic Stacking sections in
+      field boundaries are drawn for the first species, then that species is
+      removed before drawing boundaries for the second species, and so on,
+      species for which different-colored or -styled lines are desired should
+      be placed at the top of the species list. This change makes possible one
+      less call to \code{diagram()} in the Mosaic Stacking sections in
       \strong{multi-metal.Rmd}, and now the entire chalcopyrite field in Mosaic
-      Stacking 2 is bounded by a thick orange line, instead of just the
-      border shared with bornite.
+      Stacking 2 is bounded by a thick orange line, instead of just the border
+      shared with bornite.
 
       \item Improve handling of non-integer coefficients in
       \code{expr.species()}. The result for FeS1.33 was previously equivalent
@@ -133,6 +131,20 @@
     }
   }
 
+  \subsection{OTHER CHANGES}{
+    \itemize{
+
+      \item Add a \strong{zap} argument to \code{mod.OBIGT()} to clear
+      parameters of preexisting species (used by \code{logB_to_OBIGT()}).
+
+      \item Change default for \code{affinity()} to \code{loga.protein = 0}
+      (was -3).
+
+      \item Change license from GPL (>= 2) to GPL-3.
+
+    }
+  }
+
 }
 
 \section{Changes in CHNOSZ version 1.4.3 (2022-02-20)}{

Modified: pkg/CHNOSZ/man/mosaic.Rd
===================================================================
--- pkg/CHNOSZ/man/mosaic.Rd	2022-06-20 05:41:11 UTC (rev 730)
+++ pkg/CHNOSZ/man/mosaic.Rd	2022-06-20 09:04:39 UTC (rev 731)
@@ -55,6 +55,7 @@
 \seealso{
 \code{demo("mosaic")}, which extends the example below with carbonate species in order to plot a siderite field.
 To calculate mineral solubilities with mosaic calculations that account for ligand speciation, use \code{bases} as the first argument to \code{\link{solubility}}.
+\code{\link{stack_mosaic}} implements calculations for bimetallic systems.
 }
 
 \examples{

Added: pkg/CHNOSZ/man/stack_mosaic.Rd
===================================================================
--- pkg/CHNOSZ/man/stack_mosaic.Rd	                        (rev 0)
+++ pkg/CHNOSZ/man/stack_mosaic.Rd	2022-06-20 09:04:39 UTC (rev 731)
@@ -0,0 +1,106 @@
+\encoding{UTF-8}
+\name{stack_mosaic}
+\alias{stack_mosaic}
+\alias{add_alpha}
+\title{Stacked mosaic diagram}
+\description{
+Create a stacked mosaic diagram, where the species formed in the first layer become the basis species for the species formed in the second layer.
+The species in each layer are usually minerals with different metals; any bimetallic species are added to the second layer.
+}
+
+\usage{
+  stack_mosaic(bases, species1, species2, species12, names = NULL,
+    col = list(4, 3, 6), col.names = list(4, 3, 6), fill = NULL,
+    dx = list(0, 0, 0), dy = list(0, 0, 0), srt = list(0, 0, 0),
+    lwd = list(1, 1, 1), lty = list(1, 1, 1), ...)
+  add_alpha(col, alpha)
+}
+
+\arguments{
+  \item{bases}{basis species to be changed for each layer (commonly S-bearing aqueous species)}
+  \item{species1}{species (minerals and/or aqueous species) with metal 1}
+  \item{species2}{species with metal 2}
+  \item{species12}{bimetallic species}
+  \item{names}{character, species names (or chemical formulas) for labeling fields}
+  \item{col}{line color}
+  \item{col.names}{text color}
+  \item{fill}{field color}
+  \item{dx}{label x-offset}
+  \item{dy}{label y-offset}
+  \item{srt}{label rotation}
+  \item{lwd}{line width}
+  \item{lty}{line type}
+  \item{...}{arguments for \code{\link{mosaic}} and \code{\link{affinity}}}
+  \item{alpha}{character, hexadecimal value of color transparency (alpha)}
+}
+
+\details{
+\code{stack_mosaic} creates a stacked mosaic diagram following steps that are described in detail in the vignette \viglink{multi-metal}.
+Briefly, the first layer of the diagram is made by speciating the species in \code{bases} across the diagram to form the first set of species in \code{species1}.
+Then, both \code{bases} \emph{and} \code{species1} (the stable species at each point on the diagram) are used to form the second set of species, consisting of both \code{species2} \emph{and} \code{species12}.
+
+The plot parameters \code{col}, \code{col.names}, \code{fill}, \code{dx}, \code{dy}, \code{srt}, \code{lwd}, and \code{lty} should be length-3 lists (not vectors).
+The values of elements 1--3 of the list are recycled to the number of species in \code{species1}, \code{species2}, and \code{species12}, respectively.
+
+For \code{fill}, the default is to use no fill except for \code{species12}, where the fill color is taken from \code{col.names} with added transparency.
+The default definition of \code{fill} is \code{list(NA, NA, add_alpha(col.names[3], "50"))}.
+\code{add_alpha} adds transparency to a color by appending the value of \code{alpha} to the hexadecimal representation of the color given in \code{col}.
+}
+
+\section{Warning}{
+The bimetallic species in \code{species12} are shown as part of the second layer, although their formation is sensitive to the presence of stable species in the first layer.
+It follows that changing the order of layers (i.e., swapping \code{species1} and \code{species2}) can affect the depiction of mineral assemblages that have \code{species12}.
+Only one of the alternatives is thermodynamically correct, but currently there is no check to determine which one it is.
+}
+
+\value{
+A list of length two containing the output of each of the \code{\link{diagram}} calls use to make the diagram.
+}
+
+\examples{
+# Define temperature (degrees C), pressure (bar), pH and logfO2 ranges
+T <- 200
+P <- "Psat"
+res <- 200
+pH <- c(0, 14, res)
+O2 <- c(-48, -33, res)
+
+# Define system: Fe-Cu-O-S-Cl
+# NOTE: the basis species must include the first species listed
+# in each of bases, species1, and species2 below
+basis(c("pyrite", "Cu", "Cl-", "H2S", "H2O", "oxygen", "H+"))
+basis("H2S", -2)
+# Calculate solution composition for 1 mol/kg NaCl
+NaCl <- NaCl(T = T, P = P, m_tot=1)
+basis("Cl-", log10(NaCl$m_Cl))
+
+# Define arguments for stack_mosaic: Speciate aqueous sulfur
+bases <- c("H2S", "HS-", "HSO4-", "SO4-2")
+# Calculate stabilities of Fe-bearing minerals first
+species1 <- c("pyrite", "pyrrhotite", "magnetite", "hematite")
+# Calculate stabilities of Cu-bearing and FeCu-bearing minerals second
+species2 <- c("copper", "cuprite", "tenorite", "chalcocite", "covellite")
+species12 <- c("chalcopyrite", "bornite")
+# Use abbreviations for Fe-bearing minerals and formulas for Cu-bearing minerals
+names1 <- c("Py", "Po", "Mag", "Hem")
+names2 <- info(info(species2))$formula
+names12 <- info(info(species12))$formula
+names <- list(names1, names2, names12)
+# Adjust x-position for one species (chalcocite, Cu2S)
+dx <- list(c(0, 0, 0, 0), c(0, 0, 0, 1, 0), c(0, 0))
+# Use thick dashed lines for the bimetallic species
+lwd <- list(1, 1, 2)
+lty <- list(1, 1, 2)
+
+# Make the diagram
+stack_mosaic(bases, species1, species2, species12, names = names,
+  dx = dx, lwd = lwd, lty = lty,
+  pH = pH, O2 = O2, T = T, P = P, IS = NaCl$IS)
+# Add legend and title
+lTP <- lex(lTP(T, P))
+db <- describe.basis(ibasis = c(3:4))
+legend("topright", c(lTP, db), bg = "white")
+title("Fe-Cu-S-O-H-Cl", font.main = 1)
+}
+
+\concept{Extended workflow}

Modified: pkg/CHNOSZ/vignettes/multi-metal.Rmd
===================================================================
--- pkg/CHNOSZ/vignettes/multi-metal.Rmd	2022-06-20 05:41:11 UTC (rev 730)
+++ pkg/CHNOSZ/vignettes/multi-metal.Rmd	2022-06-20 09:04:39 UTC (rev 731)
@@ -446,6 +446,8 @@
 
 ## Mosaic Stacking 1
 
+*For a function that implements the workflow described below, see `stack_mosaic()` (added in CHNOSZ 2.0.0).*
+
 A mosaic diagram shows the effects of changing basis species on the stabilities of minerals.
 The Fe-S-O-H system is a common example: the speciation of aqueous sulfur species affects the stabilities of iron oxides and sulfides.
 Examples of mosaic diagrams with Fe or other single metals are given elsewhere.



More information about the CHNOSZ-commits mailing list