Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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
library(ggtern)

#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)


The post User Request – Middle Segments appeared first on ggtern: ternary diagrams in R.