StatFas.com

How to Make a Dendrogram in StatFas, SPSS, R, Python and MATLAB

By StatFas · Updated 2026-11-09

This tutorial draws the same dendrogram, with Ward linkage and the quality checks, in five tools. The example table has a sample label column, a condition column (Control, Treated, Resistant) and ten numeric gene expression columns, gene1 to gene10.

Open the free Dendrogram GeneratorRead the full guide

What you will make

A publication-ready dendrogram 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

ColumnContents
sampleSample label (S1 … S30)
conditionControl, Treated or Resistant (optional known groups)
gene1 … gene10Ten numeric expression values

Save your table as dendrogram.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)

  1. Open the Dendrogram Generator and click Load Example Data, or paste/upload your own file.
  2. Choose the numeric variables, the leaf label column, optionally the known-group column, the distance (Euclidean) and linkage (Ward), and keep Standardise variables ticked.
  3. Leave Clusters to cut at 0 to use the best silhouette width, or set the number yourself.
  4. Run the analysis. Read the cluster sizes, silhouette table, cophenetic correlation, Hopkins statistic and the figure interpretation.
  5. Style the figure, then log in (free) to download it (PNG or SVG), tables (CSV) and the Word report.
Analysis needs no login. Downloads of figures, tables and Word reports need a free StatFas account.

Method 2: SPSS

SPSS draws dendrograms natively: Analyze > Classify > Hierarchical Cluster, choose the variables, set Method: Ward’s method, Measure: Squared Euclidean distance, and under Plots tick Dendrogram. Standardize first (Descriptives > Save standardized values). Syntax:

DESCRIPTIVES VARIABLES=gene1 TO gene10 /SAVE.

CLUSTER Zgene1 TO Zgene10
  /METHOD WARD
  /MEASURE=SEUCLID
  /PRINT SCHEDULE CLUSTER(2,6)
  /PLOT DENDROGRAM VICICLE
  /SAVE CLUSTER(3).

SPSS rescales merge distances to a 0–25 scale in the dendrogram. It does not report silhouette width, cophenetic correlation or the Hopkins statistic.

Method 3: R

library(cluster); library(factoextra); library(hopkins)
df <- read.csv("dendrogram.csv")
X <- scale(df[, grep("gene", names(df))]); rownames(X) <- df$sample

hc <- hclust(dist(X), method = "ward.D2")
cor(cophenetic(hc), dist(X))                              # cophenetic correlation
sil <- sapply(2:10, function(k) mean(silhouette(cutree(hc, k), dist(X))[, 3]))
best <- which.max(sil) + 1; best; max(sil)                # best k and its silhouette
hopkins(X)                                                # clustering tendency

fviz_dend(hc, k = best, rect = TRUE, cex = 0.6)
ggplot2::ggsave("dendrogram.png", width = 8, height = 5, dpi = 300)

Packages: install.packages(c("cluster","factoextra","hopkins")). "ward.D2" is the correct implementation of Ward’s criterion on (non-squared) distances.

Method 4: Python

import pandas as pd, numpy as np, matplotlib.pyplot as plt
from scipy.cluster.hierarchy import linkage, dendrogram, fcluster, cophenet
from scipy.spatial.distance import pdist
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import NearestNeighbors

df = pd.read_csv("dendrogram.csv")               # sample, condition, gene1..gene10
X = StandardScaler().fit_transform(df.filter(like="gene"))
Zl = linkage(X, method="ward", metric="euclidean")
print("Cophenetic r =", round(cophenet(Zl, pdist(X))[0], 3))

sil = {k: silhouette_score(X, fcluster(Zl, k, "maxclust")) for k in range(2, 11)}
best = max(sil, key=sil.get); print("Best k by silhouette:", best, round(sil[best], 3))

def hopkins(X, m=None, seed=1):
    rng = np.random.default_rng(seed); n, d = X.shape; m = m or max(1, int(0.1 * n))
    nn = NearestNeighbors(n_neighbors=2).fit(X)
    u = nn.kneighbors(rng.uniform(X.min(0), X.max(0), (m, d)), 1)[0].sum()
    w = nn.kneighbors(X[rng.choice(n, m, replace=False)], 2)[0][:, 1].sum()
    return u / (u + w)
print("Hopkins H =", round(hopkins(X), 3))

cut = Zl[-(best - 1), 2] - 1e-9
dendrogram(Zl, labels=df["sample"].values, color_threshold=cut, leaf_rotation=90)
plt.axhline(cut, ls="--", c="k"); plt.ylabel("Ward distance"); plt.tight_layout(); plt.savefig("dendrogram.png", dpi=300)

Requires pandas, numpy, scipy, scikit-learn and matplotlib. Use method="average" or "complete" for other linkages.

Method 5: MATLAB, OriginPro, Excel and others

MATLAB

T = readtable("dendrogram.csv"); X = zscore(T{:, 3:end});
Z = linkage(X, "ward");
dendrogram(Z, 0, "Labels", T.sample);
c = cophenet(Z, pdist(X));

OriginPro / JASP

OriginPro: Statistics > Multivariate Analysis > Hierarchical Cluster Analysis outputs a dendrogram. JASP: Machine Learning > Hierarchical Clustering includes a dendrogram option (menu names vary by version).

Which method should you choose?

ToolCost and skillPlotStatistical checksReport-ready output
StatFasFree, in the browserYes (cut line, coloured clusters)Silhouette, cophenetic, Hopkins, ARIYes, to Word
SPSSLicence neededYes (rescaled distances)Agglomeration schedule onlyTables only
RFree, codingYes (factoextra)cluster, hopkinsManual
PythonFree, codingYes (scipy)scikit-learn, manual HopkinsManual

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

State the distance, the linkage, the standardization, how the number of clusters was chosen (silhouette), and report the mean silhouette width, the cophenetic correlation and the Hopkins statistic. StatFas writes this paragraph and the tables for you.

See the guide for an example paragraph.

Troubleshooting

Frequently asked questions

Does SPSS make dendrograms?

Yes, via Analyze > Classify > Hierarchical Cluster with the Dendrogram plot option, but it reports no silhouette or Hopkins statistic.

Which method is easiest?

The StatFas generator needs no coding and reports all quality checks.

Try it now. Run the analysis in your browser with the free Dendrogram Generator. No account is needed to analyze; a free StatFas account is needed only to download figures, tables and the Word report.

Related tools, guides and tutorials

References

  1. American Psychological Association. (2020). Publication manual of the American Psychological Association (7th ed.).
  2. Eisen, M. B., Spellman, P. T., Brown, P. O., & Botstein, D. (1998). Cluster analysis and display of genome-wide expression patterns. Proceedings of the National Academy of Sciences, 95(25), 14863–14868.
  3. Hopkins, B., & Skellam, J. G. (1954). A new method for determining the type of distribution of plant individuals. Annals of Botany, 18(2), 213–227.
  4. Hubert, L., & Arabie, P. (1985). Comparing partitions. Journal of Classification, 2(1), 193–218.
  5. Rousseeuw, P. J. (1987). Silhouettes: A graphical aid to the interpretation and validation of cluster analysis. Journal of Computational and Applied Mathematics, 20, 53–65.