rggplot2tidyverseggtern

Is there any alternative to ggtern in R?


Its looks like that ggtern has not been synchronised with new version of ggplot2. Therefore we can not use ggtern.

library(ggtern)
set.seed(1)
plot <- ggtern(data = data.frame(x = runif(100),
                                 y = runif(100),
                                 z = runif(100)),
               aes(x, y, z))
plot + stat_density_tern(geom = 'polygon',
                         n         = 200,
                         aes(fill  = ..level..,
                             alpha = ..level..)) +
  geom_point() +
  theme_rgbw() +
  labs(title = "Example Density/Contour Plot")    +
  scale_fill_gradient(low = "blue",high = "red")  +
  guides(color = "none", fill = "none", alpha = "none")
Error: geom_point requires the following missing aesthetics: x and y

Does anyone have in find other options for ternary diagrams apart from ggtern in R?


Solution

  • Manually, you could plot the points with a function: (I used the formulas at https://en.wikipedia.org/wiki/Ternary_plot)

    I'm not familiar with the output of stat_density_tern so I'm not sure what is expected from that part.

    library(tidyverse)
    tern <- function(df) {
      df %>% mutate(x_pos = 0.5 * (2*y + z) / (x+y+z),
                    y_pos = sqrt(3) / 2 * z / (x+y+z)) 
    }
    
    tern(plot) %>%
      ggplot(aes(x_pos, y_pos)) +
      geom_point() +
      annotate("path", x = c(0, 0.5, 1, 0), y = c(0,sqrt(3)/2,0,0)) +
      coord_equal()
    

    enter image description here