Theory, Examples & Exercises
This is an old revision of the document!
We use data from the variable
eurodist, which is available in R (you don't need to install any library, just type
euro disc). This distance matrix contains real geographical distances among big European cities (driving distance, in km). We will use this matrix to calculate PCoA and draw the PCoA ordination diagram, and also a screeplot of eigenvalues for individual PCoA axes.
To calculate PCoA, use the base R function
cmdscale (note that
vegan contains the function
wcmdscale, which in default setting is doing the same):
Note that I set up the argument
eig = TRUE - in this way, the
cmdscale function returns also the eigenvalues for individual axes (in the default setting this argument is set to
FALSE and the function returns only the data frame of sample scores on individual PCoA axes).
Now we can draw the ordination diagram of the cities:
library (vegan) ordiplot (pcoa, display = 'sites', type = 'text')
You can see that the distances between cities make intuitive sense (Athens are far from Stockholm, for example), and it almost looks like the map of Europe, except that Athens are at north and Stockholm at south. Let's flip the y-axis (the second axis of PCoA) and draw the ordination diagram again. The sample scores in PCoA ordination are in the object
pcoa, in the component
str (pcoa) if you wish to see the structure of
pcoa[,2] <- -pcoa[,2] ordiplot (pcoa, display = 'sites', type = 't')
Now the distribution of cities make better geographical sense!
Finally, let's draw the screeplot with eigenvalues for individual axes; these eigenvalues are stored in the component
$eig of the object
names argument adds the names to tickmarks on horizontal axis,
las argument influences rotation of labels on both x and y axis (see
?par for explanation) and
ylab adds the label to the vertical axis.
eig = FALSE, the structure of
pcoaobject would be simpler, it would be just a data frame with sample scores; with
eig = TRUEthe object became a list with the score data frame nested inside within the component