Show Sidebar Hide Sidebar

# Ternary Contour Plot in R

How to create Ternary Contour Plots in R with Plotly.

### New to Plotly?

Plotly's R library is free and open source!
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

### Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

``````library(plotly)
packageVersion('plotly')
``````
``````## [1] '4.7.0'
``````

### Basic Ternary 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');

p <- 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
}

p,
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
)
}

p <- layout(
p,
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"
)
)
)

# Set up API credentials: https://plot.ly/r/getting-started
``````

Still need help?