Ternary Plots in R using Plotly

May 2, 2016
By

(This article was first published on R – Modern Data, and kindly contributed to R-bloggers)

Plotly now supports ternary plots ! and in this post we’ll showcase how to make such charts in R.

library(plotly)
library(jsonlite)
 
URL <- "https://gist.githubusercontent.com/davenquinn/988167471993bc2ece29/raw/f38d9cb3dd86e315e237fde5d65e185c39c931c2/data.json"
ds <- fromJSON(txt = "URL")
 
colors = c('#8dd3c7','#ffffb3','#bebada',
          '#fb8072','#80b1d3','#fdb462',
          '#b3de69','#fccde5','#d9d9d9',
          '#bc80bd','#ccebc5','#ffed6f')

Markers

p <- plot_ly()
for(i in 1:length(ds)){
  p <- add_trace(p, data = ds[[i]], a = clay, b = sand, c = silt,
                 type = "scatterternary",
                 mode = "markers",
                 evaluate = T,
                 line = list(color = "black"))
}
 
p <- layout(p, title ="", showlegend = F,
            xaxis = list(title = "", showgrid = F, zeroline = F, showticklabels = F),
            yaxis = list(title = "", showgrid = F, zeroline = F, showticklabels = F),
            sum = 100,
            ternary = list(
              aaxis = list(title = "Clay", tickformat = ".0%", tickfont = list(size = 10)),
              baxis = list(title = "Sand", tickformat = ".0%", tickfont = list(size = 10)),
              caxis = list(title = "Silt", tickformat = ".0%", tickfont = list(size = 10))),
            annotations = list(
              list(xref = "paper", yref = "paper", align = "center",
                   x = 0.1, y = 1, text = "Ternary Plot in R
(Markers)", ax = 0, ay = 0, font = list(family = "serif", size = 15, color = "white"), bgcolor = "#b3b3b3", bordercolor = "black", borderwidth = 2))) p

Lines

p <- plot_ly()
for(i in 1:length(ds)){
  p <- add_trace(p, data = ds[[i]], a = clay, b = sand, c = silt,
                 type = "scatterternary",
                 mode = "lines",
                 evaluate = T,
                 line = list(color = "black"))
}
 
p <- layout(p, title ="", showlegend = F,
            xaxis = list(title = "", showgrid = F, zeroline = F, showticklabels = F),
            yaxis = list(title = "", showgrid = F, zeroline = F, showticklabels = F),
            sum = 100,
            ternary = list(
              aaxis = list(title = "Clay", tickformat = ".0%", tickfont = list(size = 10)),
              baxis = list(title = "Sand", tickformat = ".0%", tickfont = list(size = 10)),
              caxis = list(title = "Silt", tickformat = ".0%", tickfont = list(size = 10))),
            annotations = list(
              list(xref = "paper", yref = "paper", align = "center",
                   x = 0.1, y = 1, text = "Ternary Plot in R
(Lines)", ax = 0, ay = 0, font = list(family = "serif", size = 15, color = "white"), bgcolor = "#b3b3b3", bordercolor = "black", borderwidth = 2))) p

Contour

p <- plot_ly()
for(i in 1:length(ds)){
  p <- add_trace(p, data = ds[[i]], a = clay, b = sand, c = silt,
                 type = "scatterternary",
                 mode = "lines",
                 fill = "toself",
                 fillcolor = colors[i],
                 evaluate = T,
                 line = list(color = "black"))
}
 
p <- layout(p, title ="", showlegend = F,
       xaxis = list(title = "", showgrid = F, zeroline = F, showticklabels = F),
       yaxis = list(title = "", showgrid = F, zeroline = F, showticklabels = F),
       sum = 100,
       ternary = list(
         aaxis = list(title = "Clay", tickformat = ".0%", tickfont = list(size = 10)),
         baxis = list(title = "Sand", tickformat = ".0%", tickfont = list(size = 10)),
         caxis = list(title = "Silt", tickformat = ".0%", tickfont = list(size = 10))),
       annotations = list(
         list(xref = "paper", yref = "paper", align = "center",
              x = 0.1, y = 1, text = "Ternary Plot in R
(Contour)", ax = 0, ay = 0, font = list(family = "serif", size = 15, color = "white"), bgcolor = "#b3b3b3", bordercolor = "black", borderwidth = 2))) p

To leave a comment for the author, please follow the link and comment on their blog: R – Modern Data.

R-bloggers.com 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.

Search R-bloggers


Sponsors

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)