pca_plot: Plot PCA results

View source: R/pca_plot.R

pca_plotR Documentation

Plot PCA results

Description

Plots results of a PCA, e.g., scatterplot, screeplot, and cumulative explained variance plots. Takes prcomp object as the main input.

Usage

pca_plot(
  pcaObj,
  type = "scatter",
  axisIndex = NULL,
  pops = NULL,
  plotColours = NULL,
  plotLook = "ggplot",
  legendPos = "top"
)

Arguments

pcaObj

Prcomp object: A PCA of genotype data fitted using the prcomp function. Either manually fitted, or using genomalicious::pca_genos.

type

Character: What type of plot to make: a scatterplot ('scatter'), a screeplot of explained variances ('scree'), or the cumulative explained variance ('cumvar').

axisIndex

Integer: The PC axes to plot. If type=='scatter', then must be exactly 2 values, the two PC axes to plot as a scatterplot. If either type=='scree' or type=='cumvar', then can be of length from 1 to p, where p is the number of PC axes, and values again represent the desired PC axes to plot.

pops

Character: A vector of population IDs, should match the rows in pcaObj$x, but is an optional argument. Default = NULL. The function will search for pcaObj$pops to assign to this argument if not specified. Only valid when type=='scatter'.

plotColours

Character: A vector of colours to use for plotting, but is an optional argument. Default = NULL. When type=='scatter', this must be a named list with one colour per population. When type=='scree' or type=='cumvar', only a single colour is required, which is the colour of bars in the screeplot or cumulative variance plot, respectively, and will default to 'grey20' if unspecified.

plotLook

Character: The look of the plot. Default = 'ggplot', the typical gray background with gridlines produced by ggplot2. Alternatively, when set to 'classic', produces a base R style plot.

legendPos

Character: Where should the legend be positioned? Default is 'top', but could also be one of, 'right', 'bottom', 'left', or 'none'.

Value

Returns a ggplot object.

Examples

library(genomalicious)
data(data_Genos)

# Conduct the PCA with Patterson et al.'s (2006) normalisation, and
# population specified
PCA <- pca_genos(dat=data_Genos, scaling='patterson', popCol='POP')

# Plot the PCA
pca_plot(PCA)

# Plot axies 2 and 3, custom colours, and a classic look.
pca_plot(
   PCA,
   axisIndex=c(2,3),
   plotColours=c(Pop1='gray30', Pop2='royalblue', Pop3='palevioletred3', Pop4='plum2'),
   plotLook='classic'
   )

# Explained variance
pca_plot(PCA, type='scree')

# Cumulative variance for the first 10 axes with custom colour
pca_plot(PCA, type='cumvar', axisIndex=1:10, plotColours='royalblue')


j-a-thia/genomalicious documentation built on Oct. 19, 2024, 7:51 p.m.