User Request – Middle Segments

[This article was first published on ggtern: ternary diagrams in R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
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)

plot of chunk unnamed-chunk-2

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

To leave a comment for the author, please follow the link and comment on their blog: ggtern: ternary diagrams in R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)