Chapter 6: Exploratory Visualisation with PCA
Preamble
Learning Objectives
The objective of this chapter is to introduce you to the options available for visualising structure in SNP and SilicoDArT datasets using a dimension-reduction technique called Principal Components Analysis (PCA).
After completing this chapter, you should have a basic intuitive understanding of PCA and how it can be used to condense and examine genetic structure visually. This chapter is a basic introduction to PCA, a topic that will be developed further in a future eBook on Advanced Topics in dartRverse.
You also will have skills in adjusting your graphics, saving your graphics, and familiarity with options for refining your graphics outside dartRverse using ggplot.
Prerequisites
RStudio is an integrated development environment (IDE) with a graphical user interface (GUI) used to manage R workflow including for PCA and so to work through this chapter, you will need to have already completed Chapter 1. RStudio Refresher. For more advanced treatment, refer to the official documentation.
![]()
You should have completed Chapters 3-5 on data entry and data structures, manipulating data and filtering data before attempting this chapter.
![]()
You may also find it helpful to listen to the podcast summary of the overview material in this chapter:
Exploratory Visualization with PCA (mp3)
Workflow
![]()
We assume that you have established the RStudio project eBook1 and downloaded the datasets required for all chapters. Package eBook_Startup has been installed. All ready to go.
The workflow from this point again follows a formula.
- Theory, unencumbered by competing options and technical asides;
- Worked Examples where you are led through analyses with sample code;
- Exercises where you are left on your own to integrate the knowledge you have gained in a problem-solving context.
- Review the learning outcomes and the section on Where have we come? to confirm that you are moving forward with all that is required under your belt.
Accessing Worked Examples and Exercises
We covered how to access the worked examples and exercises in Chapter 1. Briefly, if you have not done so already:
source("http://georges.biomatix.org/storage/app/media/eBook1/eBook_data_download.txt")
install.packages("dartRverse")
library(dartRverse)
gl.set.verbosity(3)
install.packages(c("remotes", "learner")
remotes::install_github("Biomatix/eBook1")
library(eBook1)
Session 6-1: Introduction to PCA
Overview of Principal Components Analysis
![]()
Once the data are entered to dartRverse, manipulated and filtered, we are still confronted with a very substantial body of data. There could be hundreds of individuals and thousands of loci scored for those individuals. Typically, we are looking for evidence of genetic structure in the data, but visually perusing the dataset will provide little insight. We need a technique to separate out signal from noise.
By far the most common approach to visualising structure in a multivariable dataset, such as a SNP or SilicoDArT dataset, is Principal Components Analysis (Jolliffe, 2002) or PCA. There are at least four computational approaches to calculating a PCA, differing in computational efficiency and tractability (Wu et al., 1997). Essentially PCA takes a data matrix (SNP genotypes or SilicoDArT) and considers the individuals to be entities and the SNP loci to be attributes. Each individual is defined by the SNP genotype scores across the loci. PCA is a very intuitive technique that can be understood in its essentials without reference to the underlying mathematics.
Geometric Representation
First, the position of each individual is placed in a space defined by the SNP loci (each locus representing a dimension in a physical space). If we consider the three individuals A, B and C in the space defined by SNP locus 1 and locus 2, we have individual A located at point (1,1) indicating that it is heterozygous at both loci (Figure 6-1). Individual B is heterozygous at locus 1 and homozygous for the alternate allele at locus 2, and individual C is homozygous for the alternate allele at both locus 1 and 2. An individual homozygous for the reference allele at both loci would lie at the origin.
It takes a little stretch of the imagination to extend this to three dimensions with the addition of a third SNP locus , whereby the positions of individuals A, B and C defined by their coordinates (0, 1 or 2) along each of the three dimensions. We can extend this thinking further to define the positions of individuals A, B and C in a space defined by their scores on all available SNP loci, that is, in a multivariable space. So, all the individuals are represented by a cloud of points in a multivariable space with the axes defined by the SNP loci.
Orthogonal Basis
The challenge now addressed by PCA is that the dimensions defined by the SNP loci do not represent a particularly good basis for representing the individuals in a space. This is because the information contained in one SNP locus is not necessarily independent of the information contained in a second SNP locus. They might be linked in the genome or there might be genetic structure in the dataset. If two loci are tightly linked, or both drawn from a highly divergent population, the information on the position of individual A along the dimension corresponding to the first locus will carry with it almost all the information on the position of individual A along the dimension corresponding to the second locus. The two dimensions defining the space are not orthogonal but rather are interrelated. Movement along one mandates movement along the other. They are not orthogonal axes.
PCA analysis centres the data on the multivariable mean, then takes linear combinations of the SNP axes to generate orthogonal composite axes (Principal Components) that form a typical set of Cartesian axes. Each individual is represented in the new space by their new scores on the orthogonal Principal Components.
Ordination
The next step in the analysis is to rotate the Principal Component axes to form an ordinated space. The SNP locus space is realigned by rotation (linear transformation) to yield new orthogonal axes ordered on the contribution of variance (represented by their eigenvalues) in the direction of the axes (defined by their eigenvector). So, the first Principal Component is aligned in the direction of maximum variance in the data, the second Principal Component is aligned in the direction of maximum variance orthogonal to the first, and so on. This process maintains the relative positions of the individuals.
Dimensional Reduction
Because the resultant axes are ordered on the amount of information they contain (the variance), the first few axes, preferably the first two or three, tend to contain information on any structure in the data (signal), and later axes tend to contain only noise (Gauch, 1982) and can be ignored. Classical PCA is thus a powerful visual technique for examining structure in the SNP dataset.
Example
An example of a PCA is provided in Figure 6-2. This is from a study of genetic variation among individuals in the genus Mixophyes, the barred frogs. After some preliminary filtering for call rate (< 0.90), secondaries, and other parameters, the dataset contained 19,680 loci scored for each of 77 individuals. Clearly, perusing a data matrix with 77 entities scored for 19,680 attributes is not going to yield any insight to whatever genetic structure may be present. By ordinating a space defined by new orthogonal axes calculated from the 19,680 loci scores across individuals, we are able to disentangle signal from noise. The first two Principal Components explain 45% of the variation in the data, so we set aside remaining variation (mainly noise) to interpret the genetic structure within the dataset.
Clearly there are three groupings. Two represent existing well-defined species: Mixophyes balbus and Mixophyes fleayi. A third grouping is unexpected and represents a putative new species, previously considered to be Mixophyes balbus. Further analysis following the exploratory PCA confirms that this third grouping is indeed a new species, and it is named as Mixophyes australis. Refer to the published paper for a full analysis (Mahony et al., 2023).

Worked Example
![]()
Now let us consider a worked example. This worked example will run through the application of functions used for exploratory visualisation using PCA prior to more serious analyses. The genlight object contains data from a real-world example, the earless grassland dragon. To access the Worked Examples, navigate to the RStudio Tutorial Pane and then open the session eBook. You will be asked again to load the earless dragon dataset that was created in Chapter 3, namely tympo_SNP_raw.RData. Undertake Worked Example 6-1.
Exercises
![]()
Try some more of what you have learned so far on some exercises and your own data. These exercises ask you to analyse real datasets to apply what you have learned on exploratory visualisation.
The Exercises are also found by navigating to the RStudio Tutorial Pane and then opening the session eBook. Navigate to the Exercises for Chapter 6.
Winding up
Where have we come?
![]()
In this Session, you have learnt how to reduce the dimensionality with a technique called classical Principal Components Analysis (PCA) and how to visualise the results.
You should now have:
- a basic appreciation of the rationale of PCA
- practical experience in conducting a PCA on SNP and SilicoDArT data
- an appreciation that PCA is an exploratory tool for examining genetic structure within the dataset as a prelude to further analysis to address interesting hypotheses arising.
PCA is a complex topic, and this chapter gives only a brief introduction. Further reading will be required to appreciate and apply the full power of the analysis on real datasets.
Ende
Jolliffe IT (2002) Principal Component Analysis, 2nd edn, Springer-Verlag, New York.
Wu, W, Massart, DL and de Jong, S (1997) ‘The kernel PCA algorithms for wide data. Part 1: Theory and algorithms’, Chemometrics and Intelligent Laboratory Systems, 36(2): 165-172. https://doi.org/10.1016/S0169-7439(97)00010-5
Gauch, HG (1982) Multivariate Analysis in Community Ecology, Cambridge University Press, Cambridge.
Mahony M, Bertozzi T, Guzinski T and Donnellan SC (2023) ‘A new species of barred frog, Mixophyes (Anura: Myobatrachidae) from south-eastern Australia identified by molecular genetic analyses’, Zootaxa, 5297(3): 301–336, https://doi.org/10.11646/zootaxa.5297.3.1.
