rlmggtern

How do I specify a model in a ternary plot (ggtern) in R?


I am trying to reproduce a ternary plot using the packacke ggtern.
Here is a minimal working example (I got this from Lawson and Willden (2016)):

library("mixexp")
library("ggtern")


# ternary plot using ModelPlot (mixexp)
orig <- Xvert(nfac = 3, 
              lc = c(0.35, 0.2, 0.15), 
              uc = c(1, 1, 1),
              ndm = 1, 
              plot = FALSE) 

y <- c(15.3, 20.0, 28.6, 12.5, 32.7, 42.4)
orig <- cbind(orig[1:6, ], y) 
quadm <- lm(y ~ -1 + x1 + x2 + x3 + x1:x2 + x1:x3 + x2:x3, 
            data = orig)

ModelPlot(model = quadm, 
          dimensions = list(x1 = "x1", x2 = "x2", x3 = "x3"),
          lims = c(0.35, 1, 0.20, 1, 0.15, 1),
          constraints = TRUE, 
          contour = TRUE, 
          cuts = 6, 
          fill = F,
          axislabs = c("x1", "x2", "x3"), cornerlabs = c("x1", "x2", "x3"),
          pseudo = T)

Now I want to reproduce this plot using ggtern. My idea would be the following:

# ternary plot using ggtern
my_ternary_plot <- ggtern(data = orig,
                          mapping = aes(x = x1,
                                        y = x2,
                                        z = x3,
                                        value = y)) +
  geom_point() +
  geom_interpolate_tern(method = "lm",
                        formula = value ~ -1 + x + y + z + x:y + x:z + y:z)
my_ternary_plot

But this will return a warning message in geom_interpolate_tern since z is not known. So, what would be the correct syntax for the formula argument to accept the model equation from above?

Thank you in advance!


Solution

  • In a trianglar graph, x, y and z are not 3 independent variables, since x+y+z are always equal to 1. I suspect ggtern is having problems with the third term.

    So if one make the substitution of I(1-x-y) for z, then the interpolation is in terms of just 2 independent variables.

    my_ternary_plot <- ggtern(data = orig,
                              mapping = aes(x = x1,
                                            y = x2,
                                            z = x3,
                                            value = y)) +
       geom_point() +
       geom_interpolate_tern(method = "lm",
                             formula = value ~ -1 + x + y + I(1-x-y) + x:y + x:I(1-x-y) + y:I(1-x-y))
    my_ternary_plot
    

    enter image description here

    The above function can be simplified to this:

    geom_interpolate_tern(method = "lm",
                             formula = value ~ 1 + x + y  + x:y + I(x^2) + I(y^2))