rggplot2plotternary

Select specific colors for filling a Ternary Plot


As per the documentation and tutorials provided for the Ternary package in R, the following script produces a ternary plot where each vertex of the triangle corresponds to an RGB color. I am trying to generate the same plot, but manually specifying the three colours that would be assigned to each vertex (axis).

library (Ternary)
TernaryPlot()
cols <- TernaryPointValues(rgb)
ColourTernary(cols, spectrum = NULL)

Instead of base R's RGB colours, I want to generate a ternary plot where the vertices are assigned the colours "#016699", "#769022", and "#F99C1C".

I understand that the function TernaryPointValues generates a matrix with a colour assigned to each coordinate within the Ternary Plot. Is there a way to specify the three colours other than rgb in that function?

Alternatively to the Ternary library, solutions with other R packages such as ggtern are welcome.


Solution

  • We just need to write a function that interpolates across three colors. We can do the interpolation in RGB space, just like your example.

    library (Ternary)
    
    color_interp <- function(a, b, c, A = "#016699", B = "#769022", C = "#F99C1C") {
      m <- cbind(col2rgb(A), col2rgb(B), col2rgb(C)) / 256
      rgb(matrix(c(a, b, c), 1) %*% t(m))
    }
    color_interp_vec <- Vectorize(color_interp, c('a', 'b', 'c'))
    cols <- TernaryPointValues(color_interp_vec)    
    
    TernaryPlot()
    ColourTernary(cols, spectrum = NULL)
    

    enter image description here