How to Make a Ternary Plot in StatFas, SPSS, R, Python and Other Tools
This tutorial draws a ternary plot and tests group differences with PERMANOVA on Aitchison distance. The example table has a group column (Healthy, IBD, Obese) and three non-negative columns: firmicutes, bacteroidetes and other.
What you will make
A publication-ready ternary plot with the compulsory checks and statistics that go with it, produced in five ways. The StatFas generator needs no coding and no account to run; the code versions are for readers who want a scripted workflow. For the theory behind the plot and how to read it, see the full guide.
Example data
| Column | Contents |
|---|---|
group | Healthy, IBD or Obese (optional) |
firmicutes | Component A |
bacteroidetes | Component B |
other | Component C |
Save your table as ternary.csv with a header row. The StatFas generator has a Load Example Data button with simulated data, so you can follow along without your own file. The example data are invented for illustration and are not real findings.
Method 1: StatFas (free, no coding)
- Open the Ternary Plot Generator and click Load Example Data, or paste/upload your own file.
- Select the three component columns and the group column. Keep Compositional centre per group and 95% log-ratio ellipses ticked if you want them.
- Run the analysis. Read the group centres, descriptive statistics and the PERMANOVA table with its interpretation.
- Change colors, fonts, gridlines, line widths and sizes in the figure style panel.
- Log in (free) to download the figure (PNG or SVG), tables (CSV) and the Word report.
Method 2: SPSS
SPSS has no ternary chart. You can run the group comparison on log-ratio coordinates, but you have to compute them yourself. Compute two ilr coordinates and test them with a MANOVA (Analyze > General Linear Model > Multivariate); for a permutation test use R, Python or StatFas.
COMPUTE ilr1 = (1/SQRT(2)) * LN(firmicutes / bacteroidetes).
COMPUTE ilr2 = (1/SQRT(6)) * LN(firmicutes * bacteroidetes / (other * other)).
EXECUTE.
GLM ilr1 ilr2 BY group /PRINT=DESCRIPTIVE.Check that no component is zero before taking logs. Draw the triangle in another tool.
Method 3: R
library(ggtern); library(compositions); library(vegan)
df <- read.csv("ternary.csv")
ggtern(df, aes(firmicutes, bacteroidetes, other, colour = group)) +
geom_point(size = 2) + theme_bw()
ggsave("ternary.png", width = 6, height = 5, dpi = 300)
comp <- acomp(df[, c("firmicutes", "bacteroidetes", "other")])
ilr_co <- ilr(comp) # Euclidean distance here = Aitchison distance
adonis2(dist(ilr_co) ~ group, data = df, permutations = 9999)Packages: install.packages(c("ggtern","compositions","vegan")). adonis2 reports pseudo-F, R² and the permutation p-value.
Method 4: Python
import numpy as np, pandas as pd, plotly.express as px
df = pd.read_csv("ternary.csv") # group + three non-negative parts
parts = ["firmicutes", "bacteroidetes", "other"]
df[parts] = df[parts].div(df[parts].sum(axis=1), axis=0) * 100 # close to 100 %
px.scatter_ternary(df, a=parts[0], b=parts[1], c=parts[2], color="group").write_html("ternary.html")
# ilr coordinates and PERMANOVA on Aitchison distance (permutation test)
x = df[parts].values / 100; l = np.log(x); clr = l - l.mean(axis=1, keepdims=True)
H = np.array([[1/np.sqrt(2), -1/np.sqrt(2), 0], [1/np.sqrt(6), 1/np.sqrt(6), -2/np.sqrt(6)]])
ilr = clr @ H.T
g = df["group"].values
def pseudo_f(y, lab):
n, k = len(y), len(set(lab)); grand = y.mean(0)
ssb = sum((lab == c).sum() * ((y[lab == c].mean(0) - grand) ** 2).sum() for c in set(lab))
ssw = sum(((y[lab == c] - y[lab == c].mean(0)) ** 2).sum() for c in set(lab))
return (ssb / (k - 1)) / (ssw / (n - k)), ssb / (ssb + ssw)
F, r2 = pseudo_f(ilr, g); rng = np.random.default_rng(1)
perm = [pseudo_f(ilr, rng.permutation(g))[0] for _ in range(9999)]
print(f"pseudo-F = {F:.2f}, R2 = {r2:.3f}, p = {(1 + sum(f >= F for f in perm)) / 10000:.4f}")Requires numpy, pandas and plotly. The script writes an interactive ternary.html; use fig.write_image (needs kaleido) for a PNG. The package python-ternary gives matplotlib ternary axes.
Method 5: MATLAB, OriginPro, Excel and others
OriginPro
Plot > Ternary (menu names vary by version) draws ternary plots from three columns and supports contour and symbol options.Excel
Excel has no ternary chart. Use a ternary add-in or an XY scatter chart with the coordinates x = 0.5 × (2B + C)/(A + B + C) and y = (√3/2) × C/(A + B + C), then draw the triangle outline manually.
Which method should you choose?
| Tool | Cost and skill | Ternary plot | Compositional statistics | Report-ready output |
|---|---|---|---|---|
| StatFas | Free, in the browser | Yes (centres, ellipses) | Log-ratio centres, PERMANOVA | Yes, to Word |
| SPSS | Licence needed | No | MANOVA on ilr by hand | Tables only |
| R | Free, coding | Yes (ggtern) | compositions, vegan | Manual |
| Python | Free, coding | Yes (plotly) | Manual | Manual |
Choose StatFas for speed and APA7-ready output, SPSS if your department already licenses it and you only need the statistics, and R or Python when the analysis must be scripted and reproduced in a pipeline.
How to report the result in APA7
Describe how the data were closed to 100%, the transformation (ilr), the distance (Aitchison), the test (PERMANOVA) and the number of permutations, then report pseudo-F with degrees of freedom, R² and p. StatFas writes this paragraph and the tables for you.
See the guide for an example paragraph.
Troubleshooting
- Error: zeros in the data. Log-ratios are undefined at zero. Replace zeros with a small positive value or merge components, and state this in the methods.
- Rows do not sum to 100. This is fine; they are re-scaled. Check that your units are consistent.
- Points pile into one corner. The composition is dominated by one part. Consider a log-ratio plot or merge minor parts.
- PERMANOVA significant but ellipses overlap. Check dispersion; unequal spread can cause significance without a difference in centres.
Frequently asked questions
Can SPSS make a ternary plot?
No. SPSS has no ternary chart; use StatFas, R, Python or OriginPro.
Is a ternary plot only for three variables?
Yes. For more parts, combine them into three groups or use PCA on log-ratio transformed data.
Related tools, guides and tutorials
- Guide: Ternary Plot Explained: How to Make, Read and Analyze Compositional Data
- Pie chart maker
- Bar plot maker
- Pie chart tutorial
- Descriptive statistics calculator
- MANOVA explained
- Principal component analysis explained
- Frequency analysis explained
- One-way ANOVA calculator
- PCA Biplot Generator
- Parallel Coordinates Plot Generator
- 3D Scatter Plot Generator
- All tutorials
- All blog posts
References
- Aitchison, J. (1986). The statistical analysis of compositional data. Chapman & Hall.
- American Psychological Association. (2020). Publication manual of the American Psychological Association (7th ed.).
- Anderson, M. J. (2001). A new method for non-parametric multivariate analysis of variance. Austral Ecology, 26(1), 32–46.
- Egozcue, J. J., Pawlowsky-Glahn, V., Mateu-Figueras, G., & Barceló-Vidal, C. (2003). Isometric logratio transformations for compositional data analysis. Mathematical Geology, 35(3), 279–300.