Skip to contents

Create a scatterplot from a PCA object

Usage

plotPCA(
  pca,
  pcX = 1,
  pcY = 2,
  groups = NULL,
  individuals = TRUE,
  loadings = FALSE,
  nLoadings = NULL
)

Arguments

pca

prcomp object

pcX

Character: name of the X axis of interest from the PCA

pcY

Character: name of the Y axis of interest from the PCA

groups

Matrix: groups to plot indicating the index of interest of the samples (use clinical or sample groups)

individuals

Boolean: plot PCA individuals

loadings

Boolean: plot PCA loadings/rotations

nLoadings

Integer: Number of variables to plot, ordered by those that most contribute to selected principal components (this allows for faster performance as only the most contributing variables are rendered); if NULL, all variables are plotted

Value

Scatterplot as an highchart object

See also

Other functions to analyse principal components: calculateLoadingsContribution(), performPCA(), plotPCAvariance()

Examples

pca <- prcomp(USArrests, scale=TRUE)
plotPCA(pca)
#> Error in (function (classes, fdef, mtable) {    methods <- .findInheritedMethods(classes, fdef, mtable)    if (length(methods) == 1L)         return(methods[[1L]])    else if (length(methods) == 0L) {        cnames <- paste0("\"", vapply(classes, as.character,             ""), "\"", collapse = ", ")        stop(gettextf("unable to find an inherited method for function %s for signature %s",             sQuote(fdef@generic), sQuote(cnames)), domain = NA)    }    else stop("Internal error in finding inherited methods; didn't return a unique method",         domain = NA)})(list("prcomp"), new("nonstandardGenericFunction", .Data = function (object,     ...) {    standardGeneric("plotPCA")}, generic = structure("plotPCA", package = "BiocGenerics"),     package = "BiocGenerics", group = list(), valueClass = character(0),     signature = "object", default = NULL, skeleton = (function (object,         ...)     stop(gettextf("invalid call in method dispatch to '%s' (no default method)",         "plotPCA"), domain = NA))(object, ...)), <environment>): unable to find an inherited method for function ‘plotPCA’ for signature ‘"prcomp"’
plotPCA(pca, pcX=2, pcY=3)
#> Error in (function (classes, fdef, mtable) {    methods <- .findInheritedMethods(classes, fdef, mtable)    if (length(methods) == 1L)         return(methods[[1L]])    else if (length(methods) == 0L) {        cnames <- paste0("\"", vapply(classes, as.character,             ""), "\"", collapse = ", ")        stop(gettextf("unable to find an inherited method for function %s for signature %s",             sQuote(fdef@generic), sQuote(cnames)), domain = NA)    }    else stop("Internal error in finding inherited methods; didn't return a unique method",         domain = NA)})(list("prcomp"), new("nonstandardGenericFunction", .Data = function (object,     ...) {    standardGeneric("plotPCA")}, generic = structure("plotPCA", package = "BiocGenerics"),     package = "BiocGenerics", group = list(), valueClass = character(0),     signature = "object", default = NULL, skeleton = (function (object,         ...)     stop(gettextf("invalid call in method dispatch to '%s' (no default method)",         "plotPCA"), domain = NA))(object, ...)), <environment>): unable to find an inherited method for function ‘plotPCA’ for signature ‘"prcomp"’

# Plot both individuals and loadings
plotPCA(pca, pcX=2, pcY=3, loadings=TRUE)
#> Error in (function (classes, fdef, mtable) {    methods <- .findInheritedMethods(classes, fdef, mtable)    if (length(methods) == 1L)         return(methods[[1L]])    else if (length(methods) == 0L) {        cnames <- paste0("\"", vapply(classes, as.character,             ""), "\"", collapse = ", ")        stop(gettextf("unable to find an inherited method for function %s for signature %s",             sQuote(fdef@generic), sQuote(cnames)), domain = NA)    }    else stop("Internal error in finding inherited methods; didn't return a unique method",         domain = NA)})(list("prcomp"), new("nonstandardGenericFunction", .Data = function (object,     ...) {    standardGeneric("plotPCA")}, generic = structure("plotPCA", package = "BiocGenerics"),     package = "BiocGenerics", group = list(), valueClass = character(0),     signature = "object", default = NULL, skeleton = (function (object,         ...)     stop(gettextf("invalid call in method dispatch to '%s' (no default method)",         "plotPCA"), domain = NA))(object, ...)), <environment>): unable to find an inherited method for function ‘plotPCA’ for signature ‘"prcomp"’

# Only plot loadings
plotPCA(pca, pcX=2, pcY=3, loadings=TRUE, individuals=FALSE)
#> Error in (function (classes, fdef, mtable) {    methods <- .findInheritedMethods(classes, fdef, mtable)    if (length(methods) == 1L)         return(methods[[1L]])    else if (length(methods) == 0L) {        cnames <- paste0("\"", vapply(classes, as.character,             ""), "\"", collapse = ", ")        stop(gettextf("unable to find an inherited method for function %s for signature %s",             sQuote(fdef@generic), sQuote(cnames)), domain = NA)    }    else stop("Internal error in finding inherited methods; didn't return a unique method",         domain = NA)})(list("prcomp"), new("nonstandardGenericFunction", .Data = function (object,     ...) {    standardGeneric("plotPCA")}, generic = structure("plotPCA", package = "BiocGenerics"),     package = "BiocGenerics", group = list(), valueClass = character(0),     signature = "object", default = NULL, skeleton = (function (object,         ...)     stop(gettextf("invalid call in method dispatch to '%s' (no default method)",         "plotPCA"), domain = NA))(object, ...)), <environment>): unable to find an inherited method for function ‘plotPCA’ for signature ‘"prcomp"’