22 August 2012

Ternary Diagram


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!

require("vcd")
read.table("PPS-data", header=TRUE)->simresult  #This loads our data from a tab delimited file
attach(simresult)
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


Cheers

4 comments:

  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/

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

    - request from an amateur.

    ReplyDelete
    Replies
    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

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

    Teah

    ReplyDelete