dimr: Dimension reduction

View source: R/plot.R

dimrR Documentation

Dimension reduction

Description

Wrapper function to perform and plot methods of dimensionality reduction including Rtsne, rpca, prcomp, andumap, with optional kmeans clustering.

Usage

dimr(data, type = c("tsne", "pca", "rpca", "umap"), k = NULL, ...)

## S3 method for class 'dimr'
plot(
  x,
  group = TRUE,
  group2 = NULL,
  k = NULL,
  col = NULL,
  labels = FALSE,
  vegan = FALSE,
  loadings = 0L,
  legend = TRUE,
  args.legend = list(),
  plot3d = FALSE,
  iplot = FALSE,
  ...
)

Arguments

data

an r x c matrix-like object with r features and c samples

type

the type of algorithm to run

k

an integer representing the number of desired clusters, passed to kmeans; note this is only used for convenience and does not affect the algorithm determied by type

note, if k is passed in plot.dimr, a new k-means clustering is performed; this is useful for on-the-fly clustering

...

for dimr, additional arguments passed to the algorithm determined by type; for plot.dimr, additional arguments passed to plotting functions or further to par

x

an object of class "dimr"

group

a length c vector identifying the group of each sample; use FALSE for none or TRUE to use k-means clusters

group2

a list of data to plot, usually samples in data, each to be shown as a scatter plot

col

a vector of colors for each group

labels

argument controlling labels for points; if FALSE, no labels are drawn; if a numeric value in (0,1), points outside quantile are labelled; otherwise, a vector of labels for each point

vegan

logical; if TRUE, ordination diagrams are drawn for each k-means cluster; see ordihull

loadings

for PCA objects, a length 1 or 2 vector giving the number of loadings to plot for each component; the highest loadings will be shown as a proportion of the axes with the sign of the eigenvector

legend

logical; if TRUE, a legend will be drawn

args.legend

a named list of legend arguments to override defaults

plot3d

logical; if TRUE, the first three dimensions are drawn as a 3D scatterplot; see plot3d

iplot

logical; if TRUE, the first two dimensions are drawn as an interactive scatter plot; see iscatter

Value

dimr returns a list of class "dimr" with the following components:

object

the object return by the algorithm determined via type

x

a matrix with the first three dimensions

kmeans

a k-means clustering performed on the first two dimensions of object

See Also

Rtsne; rpca; prcomp; umap, kmeans

Examples

## feature x sample matrix
dat <- t(unique(iris[, 1:4]))
grp <- unique(iris)$Species

tsne <- dimr(dat)
umap <- dimr(dat, type = 'umap')
pca  <- dimr(dat, type = 'pca')
rpca <- dimr(dat, type = 'rpca')
pca2 <- dimr(dat, type = 'pca', scale. = TRUE) ## same as rpca

plot(tsne)        ## use k-means cluster groups
plot(tsne, grp)   ## use groups from data
plot(tsne, k = 3) ## perform new k-means

plot(pca, vegan = TRUE)
plot(pca, loadings = c(3, 4))
plot(rpca, loadings = c(3, 4))
plot(pca, k = 4, vegan = TRUE)$kmeans$cluster


## use group2 to show scatter plots of numeric data
plot(pca, group2 = as.list(data.frame(t(dat))))
plot(pca, pch = 16, cex = 3, col = adjustcolor(palette()[1:3], alpha.f = 0.5),
     group2 = as.list(data.frame(t(rbind(dat, dat)))))


## use plot3d = TRUE for a 3D, interactive figure
## Not run: 
plot(tsne, plot3d = TRUE)

plot(tsne, iplot = TRUE, labels = data.frame(t(dat)))


## make a gridplot (called from plot.dimr)
n <- 9
d <- as.list(mtcars[, rep_len(c('mpg', 'wt', 'hp'), n)])
par(mfrow = n2mfrow(n), oma = c(5, 5, 4, 2))
plotr:::gridplot(d, mtcars$mpg, mtcars$wt, legend = TRUE)
title(xlab = 'MPG', ylab = 'Weight', outer = TRUE, cex.lab = 2)

## End(Not run)


raredd/plotr documentation built on Nov. 19, 2023, 4:09 a.m.