Overview

Example data and code to produce Piper plots using ggplot are from: https://github.com/markolipka/ggplot_Piper. The ggplot_Piper code relies on the hydrogeo package.

Changes to example code

# load example data -- already in milliequivilants per liter
ex_data_meq <- tribble(
  ~IDs, ~Ca, ~Mg, ~Na, ~K, ~Cl, ~SO4, ~HCO3, ~CO3,
   "A",  43,  30,  54,  31,  54,   34,   70,  0,
   "B",  55,  10,  76,  22,  38,   15,  110,  0,
   "C",  73,  23,  23,  32,  14,   82,   55,  0,
   "D",  40,  14,  14,  22,  30,   30,   30,  0,
   "E",  62,  36,  22,  11,  43,   43,   45,  0
  )

# check for electroneutrality
ex_data_sum <- ex_data_meq %>%
  group_by(IDs) %>%
  summarise(sum_cation = Ca + Mg + Na + K,
          sum_anion  = Cl + SO4 + HCO3 + CO3
            )

# change meq to percents  hydrogeo and check results
ex_data_pct <- ex_data_meq %>%     
  as.list() %>%          # turns the tibble into a list for hydrogeo
  toPercent() %>%        # hydrogeo turns the meqs into percents
  as_tibble()            # returns the list back into a tibble

ex_data_ck <- ex_data_pct %>%
  group_by(IDs) %>%
  summarise(sum_cation = Ca + Mg + Na + K,
          sum_anion  = Cl + SO4 + HCO3 + CO3
            )

# transforms the data so it works in ggplot
ex_data_piper <- transform_piper_data(Ca   = ex_data_pct$Ca,
                                      Mg   = ex_data_pct$Mg,
                                      Cl   = ex_data_pct$Cl,
                                     SO4  = ex_data_pct$SO4,
                                    name = ex_data_pct$IDs)
# plots the piper diagram background

ggplot_piper() +                       geom_point(aes(x, y),
             data = ex_data_piper) +   # plot points as a check 
  geom_point(aes(x, y,
                 color = factor(observation)),
             data = ex_data_piper)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

  geom_point(aes(x, y,
                 color = factor(observation)),
             size = 3,
             data = ex_data_piper)
## mapping: x = ~x, y = ~y, colour = ~factor(observation) 
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
# plot points in black
ggplot_piper() +
  geom_point(aes(x, y),         # aes means aesthetic sets axes and which data 
             data = ex_data_piper) #+   

  geom_point(aes(x, y,
                 colour = factor(observation)),
             data = ex_data_piper)
## mapping: x = ~x, y = ~y, colour = ~factor(observation) 
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
  geom_point(aes(x, y,
                 colour = factor(observation)),
             size = 3,
             data = ex_data_piper)
## mapping: x = ~x, y = ~y, colour = ~factor(observation) 
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
# plot points in color
ggplot_piper() +
  geom_point(aes(x, y,      # this gives us the color as a factor of observation
                 colour = factor(observation)),
             data = ex_data_piper)

# change size of example points
ggplot_piper() +
  geom_point(aes(x, y,
                 colour = factor(observation)),
             size = 3,
             data = ex_data_piper)