rggplot2ggtern

Ternary Diagrams in R - ggter 3.3.0 vs older versions


I saw a template very interesting for ternary diagrams, made from Nicholas Hamilton, exactly at the link:

Ternary plot and filled contour

the code is the following:

#Orignal Data as per Question

library(ggplot2)
library(ggtern)

a <- c(0.1, 0.5,0.5, 0.6, 0.2, 0          , 0         , 0.004166667, 0.45) 
b <- c(0.75,0.5,0  , 0.1, 0.2, 0.951612903,0.918103448, 0.7875     , 0.45)
c <- c(0.15,0  ,0.5, 0.3, 0.6, 0.048387097,0.081896552, 0.208333333, 0.10) 
d <- c(500,2324.90,2551.44,1244.50, 551.22,-644.20,-377.17,-100, 2493.04) 
df <- data.frame(a, b, c, d)

#For labelling each point.
df$id <- 1:nrow(df)

#Build Plot
ggtern(data=df,aes(x=c,y=a,z=b),aes(x,y,z)) + 
  stat_density2d(geom="polygon",
                 n=400,
                 aes(fill=..level..,
                 weight=d,
                 alpha=abs(..level..)),
                 binwidth=100) + 
  geom_density2d(aes(weight=d,color=..level..),
                 n=400,
                 binwidth=100) +
  geom_point(aes(fill=d),color="black",size=5,shape=21) + 
  geom_text(aes(label=id),size=3) + 
  scale_fill_gradient(low="yellow",high="red") + 
  scale_color_gradient(low="yellow",high="red") + 
  theme_tern_rgbw() + 
  theme(legend.justification=c(0,1), legend.position=c(0,1)) + 
  guides(fill = guide_colorbar(order=1),
         alpha= guide_legend(order=2),
         color="none") + 
  labs(  title= "Ternary Plot and Filled Contour",
         fill = "Value, V",alpha="|V - 0|")

The issue is that with the update of this package, some errors occours:

Warning messages:
1: Ignoring unknown aesthetics: weight 
2: Ignoring unknown aesthetics: weight 
3: Removing Layer 1 ('StatDensity2d'), as it is not an approved stat (for ternary plots) under the present ggtern package. 
4: Removing Layer 2 ('GeomDensity2d'), as it is not an approved geometry (for ternary plots) under the present ggtern package.

The problem is that geom_density2d and stat_density2d is no longer supported and replaced by geom_density_tern and stat_density_tern. The modification of the package is very substantial, at the point that even making the replacement of it, any new errors are occurring. The syntax of the package was strongly changed. Does somebody know how to translate this code and make it suitable for the new version?

My interested output was this:

My interested output was this:

enter image description here

Can somebody help me?

Thank you in advance for every eventual support!


Solution

  • I think I fixed the problem. From the old and the new version, some commands were changed.

    I used geom_density_tern and stat_density_tern;

    I removed binwidth = 100, I still do not know what it stands for;

    I set a lower bdl: bdl=0.02;

    Below the code:

    library(ggtern)
    library(ggplot2)
    
    #Orignal Data as per Question
    a <- c(0.1, 0.5,0.5, 0.6, 0.2, 0          , 0         , 0.004166667, 0.45) 
    b <- c(0.75,0.5,0  , 0.1, 0.2, 0.951612903,0.918103448, 0.7875     , 0.45)
    c <- c(0.15,0  ,0.5, 0.3, 0.6, 0.048387097,0.081896552, 0.208333333, 0.10) 
    d <- c(500,2324.90,2551.44,1244.50, 551.22,-644.20,-377.17,-100, 2493.04) 
    df <- data.frame(a, b, c, d)
    
    #For labelling each point.
    df$id <- 1:nrow(df)
    
    #Build Plot
    ggtern(data=df,aes(x=c,y=a,z=b),aes(x,y,z)) + 
      stat_density_tern(geom="polygon",
                     n=400,
                     aes(fill=..level..,
                     weight=d,
                     alpha=abs(..level..)),
    na.rm = TRUE,
    bdl = 0.02) + 
      geom_density_tern(aes(weight=d,color=..level..),
                     n=400,
    #                 binwidth=100,
    bdl = 0.02) +
      geom_point(aes(fill=d),color="black",size=5,shape=21) + 
      geom_text(aes(label=id),size=3) + 
      scale_fill_gradient(low="yellow",high="red") + 
      scale_color_gradient(low="yellow",high="red") + 
      theme_rgbw() + 
      theme(legend.justification=c(0,1), legend.position=c(0,1)) + 
      guides(fill = guide_colorbar(order=1),
             alpha= guide_legend(order=2),
             color="none") + 
      labs(  title= "Ternary Plot and Filled Contour",
             fill = "Value, V",alpha="|V - 0|")