showme_PCA2D: Given a matrix show me the principal components and other...

View source: R/dim_reduction.R

showme_PCA2DR Documentation

Given a matrix show me the principal components and other neat things.

Description

Given a matrix show me the principal components and other neat things.

Usage

showme_PCA2D(
  mat,
  x = 1,
  y = x + 1,
  mt,
  mcol,
  m_fill = mcol,
  m_label = FALSE,
  n_top_var = 500,
  filt_mat = FALSE,
  NA_filt_thrshld = 0.95,
  show_variance = FALSE,
  show_stats = FALSE,
  n_loadings = NULL,
  return_data = FALSE,
  real_aspect_ratio = TRUE,
  ...
)

Arguments

mat

A matrix

x

Integer indicating principal component on X-axis. Default 1.

y

Integer indicating principal component on Y-axis. Default x + 1.

mt

data.frame containing mat additional information (i.e. metadata). Not required but necessary if one wants label or colour according to specific parameters. When not specified the function will create a simple metadata based on the mat column names.

mcol

Character specifying one column of mt that contains the matrix column names.

m_fill

Character specifying one column name of mt to use for colouring the samples.

m_label

Character or logical specifying one column name of mt to use for labelling the points in the PCA. Set to FALSE (default) for omitting labels in plot. Use 'mat_col' if mt is not defined but you want to use the mat column names for labelling.

n_top_var

Integer: Number of most variable matrix rows to use for prcomp.

filt_mat

Logical. Whether or not to remove certain rows from mat that contain to many NA. See NA_filt_thrshld to specify the threshold for removing.

NA_filt_thrshld

Integer between 0 and 1: maximum % of NA accepted on mat rows.

show_variance

Logical. Show an extra plot with the variance on each component.

show_stats

Logical. Show an extra plot with the summary statistics for the data in mat.

n_loadings

Integer indicating how many top and bottom loadings to plot.

return_data

Logical. If TRUE returns rotated data used for plotting instead of the actual plot. Can be used with n_loadings equal to any positive integer to return all components loadings.

...

Set extra parameter for the prcomp function like scale. (default FALSE) and center (default TRUE).

real_aspect_raio

Logical. If TRUE (deafault) represent the distances between samples as faithfully as possible. Take into account that the second component is always smaller than the first, sometimes considerably so, thus TRUE normalize the axis aspect ratio to the relevant ratio for the PCA plot. Adapted from by: https://f1000research.com/articles/5-1492/v2 .

Value

Either a plot (created with ggplot2), a combination of plots ( created with patchwork) or a data.frame.

Examples

showme_PCA2D(mat)

showme_PCA2D(mat = mat,  mt = mt, mcol = "sample_name", m_fill = "replicate",
             x = 3, show_stats = T, m_label = F)
             
showme_PCA2D(mat = mat, mt = mt, mcol = "sample_name", n_loadings = 12)  

# Working with metadata
data.frame(sample_name = paste0("Sample", 0:9),
           replicate = c(rep(c(1:3), 3), 1), 
           condition = c(rep("A", 5), rep("Z", 5)),
           stringsAsFactors = FALSE) -> mt            

showme_PCA2D(mat = mat, mt = mt) # does not work cause `mcol` is not specified.

# This works instead because the `mcol` is specified.
showme_PCA2D(mat = mat, mt = mt, mcol = "sample_name", m_label = "replicate")

# This throws a warning dropping `mcol` as there's no metadata provided.
showme_PCA2D(mat = mat, mcol = "sample_name") 

Ni-Ar/niar documentation built on Feb. 3, 2025, 9:25 a.m.