autoplot_pca: Automatic ggplot for a Principal Component Analysis

Description Usage Arguments Details Value See Also Examples

View source: R/pca_autoplot.R

Description

Extract information from an object returned by a function performing Principal Component Analysis and produce a plot of observations, of variables, or a biplot.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## S3 method for class 'prcomp'
autoplot(object, mapping = aes(), data = NULL,
  dimensions = c(1, 2), which = "rows", scaling = which,
  n.max.labels = 100, ...)

## S3 method for class 'PCA'
autoplot(object, mapping = aes(), data = NULL,
  dimensions = c(1, 2), which = "rows", scaling = which,
  n.max.labels = 100, ...)

## S3 method for class 'rda'
autoplot(object, mapping = aes(), data = NULL,
  dimensions = c(1, 2), which = "rows", scaling = which,
  n.max.labels = 100, ...)

## S3 method for class 'pca'
autoplot(object, mapping = aes(), data = NULL,
  dimensions = c(1, 2), which = "rows", scaling = which,
  n.max.labels = 100, ...)

## S3 method for class 'pcaRes'
autoplot(object, mapping = aes(), data = NULL,
  dimensions = c(1, 2), which = "rows", scaling = which,
  n.max.labels = 100, ...)

Arguments

object

an object returned by a function performing Principal Component Analysis.

mapping

a call to aes() specifying additional mappings between variables and plot aesthetics. By default, positions x and y are mapped to the scores on the principal components.

data

the original dataset, to be concatenated with the output when extracting row scores. When NULL (the default) data will be extracted from the PCA object when it contains it (i.e. for all functions but prcomp).

dimensions

vector giving the indexes of the principal components to extract. Typically two are extracted to create a plot.

which

the plot to produce: either plot "rows", "lines", "observations", "objects", "individuals", "sites" (which are all treated as synonyms), or plot "columns", "variables", "descriptors", "species" (which are, again, synonyms), or produce a "biplot". All can be abbreviated. By default, observations are plotted.

scaling

scaling for the scores. Can be

"none" (or 0)

for raw scores,

"rows" (or 1, or a synonym of "rows")

to scale row scores by the eigenvalues,

"columns" (or 2, or a synonym of "columns")

to scale column scores by the eigenvalues,

"both" (or 3)

to scale both row and column scores.

By default, scaling is adapted to the type of scores extracted (scaling 1 for row scores, scaling 2 for column scores, and scaling 3 when scores are extracted for a biplot).

n.max.labels

maximum number of observation labels to plot. Let n be the number of data rows. For 0 < n < n.max.labels/2, labels are plotted and shifted to avoid over-plotting. For n.max.labels/2 < n < n.max.labels, labels are plotted but not disambiguated. For n.max.labels < n labels are not plotted.

...

passed to the various geoms; can be used to set further aesthetics.

Details

The object is passed to the appropriate augment method, defined in pca_tidiers, which extracts the scores, possibly the original data, and other relevant information from the PCA object. The resulting data.frame is plotted with:

geom_point:

for observations points,

geom_text or geom_text_repel:

for observations labels,

geom_segment:

for variables vectors.

Value

A ggplot2 object defining the plot.

See Also

Functions to perform PCA: prcomp in package stats, PCA in package factoMineR, rda in package vegan, dudi.pca in package ade4, pca in package pcaMethods (on bioconductor).

Other PCA.related.functions: ca_tidiers, pca_tidiers

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
pca <- prcomp(USArrests, scale=TRUE)
autoplot(pca)
autoplot(pca, which="variables")
autoplot(pca, which="biplot") # defaults to scaling=3
autoplot(pca, which="biplot", scaling=1)
autoplot(pca, which="biplot", scaling=2, n.max.labels=0)

# add further aesthetic mappings
names(augment(pca, data=USArrests))
autoplot(pca, data=USArrests, mapping=aes(alpha=.cos2))
autoplot(pca, data=USArrests, mapping=aes(alpha=.cos2, size=.contrib))
# including from the original data
autoplot(pca, data=USArrests, mapping=aes(alpha=.cos2, color=Murder))

# aesthetics can also be set
autoplot(pca, mapping=aes(alpha=.cos2), color="darkblue", size=3)

if (require("FactoMineR")) {
  pca <- FactoMineR::PCA(USArrests, graph=FALSE)
  autoplot(pca)
  autoplot(pca, which="variables")
  # with FactoMineR, the data is present by default and can be mapped
  names(augment(pca))
  autoplot(pca, mapping=aes(alpha=.cos2, color=Murder))
}

if (require("vegan")) {
  pca <- vegan::rda(USArrests, scale=TRUE)
  plot(pca)
  autoplot(pca, which="biplot", scaling=2)
  autoplot(pca)
}

if (require("ade4")) {
  pca <- ade4::dudi.pca(USArrests, nf=4, scannf=FALSE)
  biplot(pca)
  autoplot(pca, which="biplot", scaling=1)
}

if (require("pcaMethods")) {
  pca <- pcaMethods::pca(USArrests, scale="uv", nPcs=4)
  biplot(pca)
  autoplot(pca, which="biplot")
}

jiho/autoplot documentation built on May 19, 2019, 9:29 a.m.