How to find clusters in a line diagram

Last update: December 3, 2025
  • Choosing the right distance and linkage changes the shape of the clusters and the interpretation of the dendrogram.
  • The ideal cut combines visual inspection with methods such as elbow and gap statistics.
  • Validate groups using PERMANOVA, RDA/db-RDA, and spatial control (MEM/MSR).
  • Model-based methods (multivariate GLMs) shed light on abundance patterns.

grouping in a linear diagram

Finding groups of data points in a linear diagram often means interpreting a dendrogram, which is nothing more than a similarity tree. If your question is how to identify where to cut this diagram to obtain coherent clusters, the answer involves understanding distances, linkage criteria, and cluster quality metrics.Throughout this guide, we'll go from basic to advanced, connecting the concept of 'linear diagrams' to the practical use of dendrograms, as well as other multivariate techniques that help validate and explain the observed groups.

Beyond the theory, I offer a practical perspective with examples, commonly used metrics, a data preparation checklist, and modern alternatives (such as PERMANOVA, RDA, and model-driven methods). The idea is that you can confidently read a dendrogram, objectively choose the number of groups, and, when necessary, supplement it with robust analyses to confirm whether the patterns observed are real and interpretable..

What is hierarchical clustering and why does it help in reading a 'linear diagram'?

In hierarchical clustering, we construct a 'tree' of similarities between observations, the so-called dendrogram, which many informally call a linear diagram because it organizes relationships along a vertical line of distances. There are two main flavors: the agglomerative (bottom-up) and the divisive (top-down)..

In agglomerative mode, each point starts isolated and, in each iteration, we merge the nearest pair of clusters until only one remains. In the divisive method, the opposite happens: we start with a single group containing all the samples and separate the more distant subsets, breaking the set into increasingly smaller branches.In both cases, a hierarchy is obtained that can be 'cut' at different heights to obtain K groups.

The dendrogram shows a distance (or dissimilarity) measurement on the vertical axis: Long vertical lines indicate mergers between very different groups, and short lines indicate junctions between nearby clusters.It is by observing these 'jumps' that we identify natural breaks in clusters.

How the agglomerative method works step by step

Imagine a simple set of points on a plane, with only a few. Initially, each point is a cluster, and its 'center' coincides with itself. We calculate the distance between all pairs of clusters, choose the pair with the smallest distance, and merge them into a new cluster.We repeat the process: we recalculate the distances from the new cluster to the others and continue joining closer pairs, reducing the number of groups from N to N-1, and so on.

To measure proximity, you can use several metrics: Euclidean (most common in continuous spaces), Manhattan (robust against outliers in certain scenarios), and Cosine (good for direction vectors). In ecological and compositional contexts, other methods also appear, such as Bray-Curtis, Jaccard, Sørensen, Hellinger, Chord, Canberra, Mahalanobis, and even the chi-square distance, each suited to a specific type of data and interpretation..

How to calculate the distance between clusters depends on the linkage criterion: Single (nearest neighbor), Complete (furthest neighbor), Average/UPGMA (arithmetic mean), Ward (minimizes the intra-cluster sum of squares), among others.The choice of linkage alters the final shape of the dendrogram and, therefore, how the diagram is interpreted.

How to choose the number of clusters in the dendrogram

There is no 'magic' K. What we do is look for large 'steps' in the dendrogram: a horizontal cut that avoids crossing long branches and preserves short branches. In practical terms, draw a horizontal line at the distance level where there is a sharp jump; the number of intersections with branches determines K..

Related:  How to convert cm² to m²?

In addition to visual inspection, there are useful heuristics: The elbow method for intra-cluster sum-of-squares curves and the gap statistic, which compares the observed WCSS with the expected WCSS under a null distribution.It is also valid to choose a distance threshold consistent with the problem domain.

A good cluster exhibits high similarity within groups and low similarity between groups. The final quality depends on the distance metric, the linkage, and the chosen K-value; it's worth experimenting with options and evaluating which one reveals the most stable and informative patterns.Practical factors to consider: number of plausible groups, statistics per cluster (means, maximums, minimums), impact of outliers, and domain knowledge.

Metrics of (dis)similarity: when to use each one

For continuous data, the Euclidean distance is standard. However, There are alternatives that shift the emphasis of what is considered 'proximate': Manhattan, Canberra, Mahalanobis (considers covariance), Chord, Hellinger, and chi-square.In ecology, zeros are common and require special care.

When dealing with presence/absence, Asymmetric indices such as Jaccard and Sørensen ignore simultaneous absences (double zeros) and work very well for beta diversity.For count/abundance data, coefficients such as Bray-Curtis, Chord, log-Chord, Hellinger, chi-square, and Morisita-Horn are common and generally semi-metric.

If your matrix mixes variable types (continuous, binary, ordinal, circular), Gower's index is the recommended wildcard.In Q-mode (similarity between objects) we use (dis)similarities; in R-mode (between descriptors), correlation/covariance. Standardizations and transformations reduce biases. z-score for scaling variables in different units; Hellinger/Chord for mitigating the effect of extreme abundances and multiple zeros..

Dendrogram linkage methods and quality

UPGMA (arithmetic mean) gives equal weights to objects and calculates average distances between groups; Ward minimizes the sum of squares within clusters (similar to OLS/ANOVA) and tends to form compact groups.Changing the linkage can significantly alter the tree.

To check how well the dendrogram preserves the original (dis)similarities, we use the cophenetic correlation coefficient. Values ​​above ~0,7 usually indicate good representation, remembering that this is a rule of thumb, not a dogma.When statistical support is needed, bootstrap packages like pvclust estimate node stability, although they may limit the accepted distances.

Quick pre-processing checklist: names without spaces; Abundance data often call for a Hellinger-like transformation.If there are many outliers, consider log1p (but avoid applying log and Hellinger at the same time); variables on different scales should be standardized to a mean of 0 and a standard deviation of 1.

Other clustering approaches: K-means and similar approaches

K-means is non-hierarchical: You choose K beforehand, and the algorithm partitions the data by minimizing the intra-cluster sum of squares.It is simple and efficient, but it does not reveal the hierarchy of groups (there is no dendrogram) and may converge to local minima.

To find K using K-means, repeat the fitting for multiple values ​​and evaluate criteria such as Calinski-Harabasz and SSI, or use the elbow method. Tools like cascadeKM help automate the search for optimal K+.Unlike hierarchical methods, K-means does not show smaller groups nested within larger ones.

Applications: from marketing to recommendations

Clustering is ubiquitous. In marketing, We segment customers by purchasing behavior.In search engines, we organize results by thematic similarity. In recommendation systems, we group items to suggest options that are 'close' to what the person likes.

Practical example in Python: from dendrogram to cluster labels

Consider a small two-dimensional array. First, we create the data and visualize it in a scatter plot. Next, we generated the dendrogram with Ward's linkage and trained an AgglomerativeClustering by defining n clusters.Finally, we plot the colored dots according to the predicted label.

Related:  How to get the angle of a triangle?

Outline of steps (illustrative): import matplotlib.pyplot as plt; import pandas as pd; import scipy.cluster.hierarchy as sc; from sklearn.cluster import AgglomerativeClustering. Construct the DataFrame, plot the points, and note their indices to visually identify each observation.For the dendrogram: sc.dendrogram(sc.linkage(dados, method='ward'))For the model: AgglomerativeClustering(n_clusters=3, affinity='euclidean', linkage='ward').

Regarding hyperparameters: n_clusters defines the number of output clusters; affinity is the metric (Euclidean, Manhattan, Cosine, precomputed); linkage can be ward, average, single, or complete.The choice of affinity and linkage should be consistent with your metric/objective. Finally, extract the labels and visualize clusters in distinct colors. If you want to test other Ks, change n_clusters and observe changes in the map.

Ordering: when the point cloud needs to become a legible map.

Unconstrained orderings like PCA and PCoA help to summarize dimensions and visualize patterns. In PCA, we use Euclidean distance; In PCoA, we accept other distances (Bray-Curtis, Jaccard, Gower, etc.), which broadens the range to include categorical, binary, and mixed data..

PCA centralizes data, calculates covariances, and decomposes it into eigenvectors/values: The eigenvalues ​​show how much variation each axis explains; the loadings indicate the 'weight' of the variables on each axis; the scores position objects in space.Caution: compositional data (many zeros) can distort PCA; standardizations such as Hellinger's help.

In PCoA we start with a (dis)similarity matrix appropriate to the data type. Negative eigenvalues ​​may appear; Corrections like Lingoes and Cailliez exist, but in general the first relevant axes are not affected.Use PCoA for mixed data (Gower) or when the Euclidean metric doesn't make sense.

Restricted ordering: RDA, partial RDA, and db-RDA

RDA models linear relationships between a response matrix (Y, e.g., species composition) and predictors (X, e.g., climate). It generates canonical axes that maximize the variation in Y explained by X, using statistics such as adjusted R² and permutation tests.It is, roughly speaking, a 'PCA of the values ​​predicted by multiple regressions'.

Spatial data introduce autocorrelation in the residuals and can inflate type I. Partial RDA circumvents this by including spatial predictors (MEMs) as conditioning factors, isolating the 'pure' effect of the environment.MEMs are derived from neighborhood networks (such as the Minimum Spanning Tree) and a well-chosen spatial weighting matrix (SWM).

If the natural response is a distance (beta diversity, Bray-Curtis, etc.), The db-RDA starts with a PCoA of the dissimilarity matrix, then relates the axes to X, combining the best of both worlds.In real-world applications, db-RDA often outperforms RDA when Euclidean distance is not the correct metric.

PERMANOVA and heterogeneity of dispersions (PERMDISP)

PERMANOVA tests for differences between groups based on distances and a pseudo-F analogous to that of ANOVA: F_pseudo = (SSa/SSr)*((N-g)/(g-1)). It is powerful for multivariate hypotheses without requiring multivariate normality..

However, statistics can be influenced by differences in position (centroid) and/or dispersion (intragroup variance). Combine with PERMDISP (BETADISPER) to check for heterogeneity of dispersions; if significant, the effect detected by PERMANOVA may mainly stem from unequal variances.Together, the two analyses help to distinguish 'change in composition' from 'change in variability'.

Mantel, partial Mantel and modern space alternative

The Mantel test correlates two distance matrices; the partial test controls for a third (e.g., assessing whether environmental dissimilarity explains species dissimilarity by controlling for geographic distance). It is widely used, but has limitations when spatial autocorrelation is present..

Related:  Riempire i numeri mancanti in this part: complete guide with example, mcm and equivalence

One alternative is to construct a null model that preserves global autocorrelation (Moran Spectral Randomization). This procedure uses spatial structure (via MEMs) to shuffle the data while maintaining Moran's I, resulting in more realistic p-values ​​in spatially dependent scenarios.In practice, many 'meaningful' relationships in the common Mantel cease to be so with the spatially restricted null.

Procrustes and PROTEST: agreement between multivariate maps

When you want to compare the agreement between two ordered spaces (for example, PCoA of fish and PCoA of macroinvertebrates), Procrustes analysis aligns, rotates, and scales one matrix to 'fit' another, minimizing the sum of the squares of the deviations.The m12 statistic ranges from 0 (maximum agreement) to 1 (none).

The PROTEST test assesses the significance of this adjustment by randomization. Common workflow: for distance data, run PCoA (or nMDS) on each matrix, apply Procrustes, and then PROTEST; for raw data, use PCA/CA before Procrustes.Arrow graphs help to see 'how far' one set is from imitating the other at each location.

Multivariate model-based methods: when dissimilarity is not enough.

Count data usually show a monotonic relationship between mean and variance (more common species vary more). Dissimilarity-based methods don't always handle this well; that's why multivariate GLM approaches have emerged, such as in the mvabund package..

Along these lines, we model abundances with appropriate distributions (Poisson, Negative Binomial, etc.), testing the effects of factors (e.g., 'field vs. collection') in a multivariate manner. In addition to a global test, it's possible to decompose the deviance species by species, identifying which ones drive the pattern.Another advantage is residual diagnosis, improving confidence in the inference.

Best practices, references, and study paths.

Before diving into the analysis, ensure your data is prepared: Standardize scales, handle zeros, reduce collinearity between predictors, inspect outliers, and check the need to transform variables.In spatial problems, plan for the use of MEMs and constrained null models.

Recommended reading includes: Legendre & Legendre (Numerical Ecology), Borcard et al. (Numerical Ecology with R), Thioulouse et al. (ade4), Ovaskainen & Abrego (JSDM), and guides on model-based clustering.These materials expand on what we've discussed here, with detailed examples and code.

Exercises and ideas for practice

To confirm: perform an hclust with UPGMA and Bray-Curtis, then change the distance and compare the dendrogram. Try RDA, partial RDA (with MEM), db-RDA, and PERMANOVA on the same dataset to see how each one answers different questions.Finally, use the Procrustes/PROTEST test to measure agreement between two communities and a multivariate GLM to investigate factors that jointly change abundances.

If your goal is simply to 'find a cluster in a linear diagram', focus on three steps: identify the most obvious jump in the dendrogram to define the cutoff point; Verify robustness using another metric/linkage and, if possible, bootstrap; and validate the ecological/operational significance of these groups with complementary statistics (PERMANOVA, RDA/db-RDA) and dispersion inspections.Thus, you transform a visual reading into a solid analytical decision.

Reading dendrograms is just the beginning: The key is combining the right choice of distance and linkage, a well-justified cut, and confirmation with orders and tests that resonate with the nature of your data.When this 'ecosystem' of methods works together, the groupings cease to be merely pretty branches on paper and begin to reveal real and useful patterns for decision-making.

Related articles:
Collinear vectors: system and examples