22 August 2012

Ternary Diagram

I have a more recent post on these types of plots here:  ternary diagrams

Sometimes you want to show the proportion of items in 3 different states.  This is the perfect use for a ternary diagram.  I needed to do this to show how many taxa had been simulated to have one of 3 possible character states.  Basically the diagram is triangle and as a point moves towards any of the vertices the proportion of taxa in that state is increasing.  This type of graph would be incredibly complicated to set up in excel but in R we can do it in a flash.  It comes from the package VCD if you want the sample data set and the script file that I used they are on dropbox

I was using this in conjunction with the simulations described in my last post.  And this type of diagram was actually inspired by its use in a recent paper by Sam Price et al.  The figure is actually back in the supplemental material but it shows the results of posterior predictive simulations modeling the number taxa with different types of diets.  I am doing something very similar but with sex chromosomes.  One cool opportunity I haven’t seen people take advantage of is to add another variable in the mix and color the points by it.  For instance in my example I color coded the data points by the root state used in the simulation.  You could use a clustering algorithm on your trees and then color by the tree cluster that you simulated with. The possibilities are pretty cool!

read.table("PPS-data", header=TRUE)->simresult  #This loads our data from a tab delimited file
colors <- c("black","red","green", "orange")    #This sets up a vector of colors
pch <- substr(levels(Root), 1, 3)                          #This assigns the colors to the Root states column

ternaryplot(                                                  #This is the actual plotting of our data
  simresult[,2:4],                                          #Here I provide the file and columns to plot
  pch = 20,                                                     #This is choosing the shape of data points (simple circle here)
  cex = .5,                                                       #This is the size of the data points
  col = colors[as.numeric(Root)],               #Telling it to color the points
  main = "PPS - 3-State Model"                  #Provides a title for the graph
grid_legend(0.9, 0.9, pch, colors, levels(Root),title = "Root States")   #Sets up a legend



  1. I replotted your data using a package that I have written to extend ggplot2 for ternary diagrams. http://ggtern.com/2013/12/16/pps-3-state-model/

  2. The dataset on dropbox (mention in the article) is missing. Could you please update it? Cheers.

    - request from an amateur.

    1. Sorry about that it must have gotten lost in my last dropbox purge.. I created some new sample data and updated the link. If you want to create your own data; the script is set up to run with a tab delimited file where the first column is a category denoted by a text string and the remaining 3 columns are counts in each of the three categories on the axes. Thanks for the catch and cheers

  3. Thanks, sincerely appreciate it. I will try it later.


  4. can you post the data link (PPS data) again

    1. This page is over 4 years old and I lost that orginal data somehow.... Try taking a look at this page it includes some code to simulate data: http://coleoguy.blogspot.com/2015/09/ternary-diagram-working-example.html