rggplot2plotternaryggtern

How to modify the ternary diagrams with ggtern


How to modify the ternary diagrams with ggtern

I used the following code to plot the ternary diagrams, and want to make some changes:

  1. modify the tick labels of x, y, z to probability number (0.2 0.4 0.6 0.8 1.0)
  2. remove the side arrows and corresponding text of x, y, z.
  3. remove the second legend (gray/white)
  4. replace the text in the first legend with "high" and "low"
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         = 400,
                         aes(fill  = ..level..,
                             alpha = ..level..)) +
  geom_point() +
  theme_rgbg() +
  theme(legend.justification=c(0,1), legend.position=c(0,1)) +
  #theme_gridsontop() + 
  labs(title = "Example Density/Contour Plot")    +
  scale_fill_gradient(low = "blue",high = "red")  +
  # scale_color_gradient(low="yellow",high="red") + 
  guides(fill = guide_colorbar(order=1),color="none")

enter image description here


Solution

  • Try the following modifications:

    1. The tick labels are controlled by the parameter labels = in each of the three continuous scales used in ggtern. These are scale_L_continuous for the left-sided edge, scale_R_continuous for the right edge, and scale_T_continuous for the bottom edge. Set the breaks to c(0, 0.2, 0.4, 0.6, 0.8, 1) (or more succinctly with 0:5 / 5), and set labels to the same values.
    2. You can remove the arrows by adding theme_noarrows() to your plot.
    3. Remove the alpha legend (the grey and white one) using guides(alpha = guide_none())
    4. You can change the labels of the fill legend (the blue - red one) by passing labels as an argument to scale_fill_gradient. Since there are 5 breaks (1:5), we pass five labels: c("low", "", "", "", "high")

    Here's a full reprex. If you cut and paste exactly this code in a fresh R session you should get exactly the same plot:

    library(ggtern)
    set.seed(1)
    
    ggtern(data = data.frame(x = runif(100), y = runif(100), z = runif(100)),
           mapping = aes(x, y, z = z)) +
      stat_density_tern(geom = 'polygon', n = 400,
                        aes(fill  = ..level.., alpha = ..level..)) +
      geom_point() +
      scale_fill_gradient(low = "blue", high = "red", name = "", breaks = 1:5, 
                          labels = c("low", "", "", "", "high"))  +
      scale_L_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
      scale_R_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
      scale_T_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
      labs(title = "Example Density/Contour Plot") +
      guides(fill = guide_colorbar(order = 1), alpha = guide_none()) +
      theme_rgbg() +
      theme_noarrows() +
      theme(legend.justification = c(0, 1), 
            legend.position      = c(0, 1))
    

    Created on 2020-11-24 by the reprex package (v0.3.0)