# Ternary Contour Plot in R

How to create Ternary Contour Plots in R with Plotly.

New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

### Basic Terary Contour Plot

library(plotly)
library(rjson)

df <- fromJSON(file="https://gist.githubusercontent.com/davenquinn/988167471993bc2ece29/raw/f38d9cb3dd86e315e237fde5d65e185c39c931c2/data.json")

'#fb8072','#80b1d3','#fdb462',
'#b3de69','#fccde5','#d9d9d9',
'#bc80bd','#ccebc5','#ffed6f');

fig <- plot_ly()

for (i in 1:length(df)) {
l = c()
m = c()
n = c()

for (j in 1:length(df[[i]])) {
l[[j]] <- df[[i]][[j]]$clay m[[j]] <- df[[i]][[j]]$sand
n[[j]] <- df[[i]][[j]]$silt } fig <- add_trace( fig, type = 'scatterternary', a = l, b = m, c = n, name = names(df[i]), mode = 'lines', line = list( color='#444' ), fill = 'toself', fillcolor = colors[i], showlegend = F ) } fig <- layout( fig, title = "Simple Ternary Contour Plot in R", ternary = list( sum = 100, aaxis = list( title = "clay", ticksuffix = "%", min = 0.01, linewidth = 2, ticks = "outside" ), baxis = list( title = "sand", ticksuffix = "%", min = 0.01, linewidth = 2, ticks = "outside" ), caxis = list( title = "silt", ticksuffix = "%", min = 0.01, linewidth = 2, ticks = "outside" ) ) ) fig  # Reference See https://plotly.com/r/reference/#scatterternary for more information and options! ### What About Dash? Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library. Learn about how to install Dash for R at https://dashr.plot.ly/installation. Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this: library(plotly) fig <- plot_ly() # fig <- fig %>% add_trace( ... ) # fig <- fig %>% layout( ... ) library(dash) library(dashCoreComponents) library(dashHtmlComponents) app <- Dash$new()
app$layout( htmlDiv( list( dccGraph(figure=fig) ) ) ) app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE) 