User Request – Middle Segments

January 9, 2014

(This article was first published on ggtern: ternary diagrams in R, and kindly contributed to R-bloggers)

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

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. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


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)