User Request – Middle Segments

Recently, a user of ggtern was desirable to produce a plot similar to what can be found HERE. This is relatively simple, given the use of layers during plot construction.

In ggplot2 (and hence ggtern), one can define a 'global' dataframe in the constructor, which gets picked up by each layer, UNLESS one states an alternate data-source on a per-layer basis, therefore, we can use one data set for the points, and, another specifically for the red segments meeting at the middle.

#Load the ggtern library

#Create Some Random (Trivial) Data and ensure the rows sum to unity
random <- data.frame(T = runif(100), 
                     L = runif(100), 
                     R = runif(100))
random <- random/apply(random, 1, sum)

# For Rendering the Lines, use Segment geometry
lines <- data.frame(x = c(0.5, 0, 0.5), 
                    y = c(0.5, 0.5, 0), 
                    z = c(0, 0.5, 0.5), 
                    xend = c(1, 1, 1)/3, 
                    yend = c(1, 1, 1)/3, 
                    zend = c(1, 1, 1)/3)

# Render.
ggtern(data = random, aes(T, L, R)) + 
       geom_point() + 
       geom_segment(data = lines, 
                    aes(x, y, z, 
                        xend = xend, yend = yend, zend = zend), 
                    color = 'red', size = 1)

plot of chunk unnamed-chunk-2

  1. marianoju
    • Nicholas Hamilton
  2. Ven
  3. Gin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.