Skip to content

Commit 9aff452

Browse files
committed
Add scale parameter to nice_PCA
1 parent b7186fc commit 9aff452

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

R/nice_PCA.R

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#' @param labels A vector containing the variable to be used as labels (name inside the marker), and the label size. Example: c(var = "patient", size = 2). Default: NULL (no labels).
2929
#' @param name_tags A vector containing the variable to be used as name tags (name outside the marker), tag size, minimum distance in order to add an arrow connecting the tag and the marker, and minimum distance from the tag and the center of the marker. Example: c(var = "label", size = 3, minlen = 2, box = 0.5). Default: NULL (no name tags).
3030
#' @param cluster_data Indicates if the function generates the clusters (TRUE) or not (FALSE). This new cluster variable can be used as fill or shape. Default: FALSE.
31+
#' @param scale Logical. Indicates whether to scale the data to have unit variances or not. Default: FALSE.
3132
#' @param n_clusters Number of cluster categories. Default: 3.
3233
#' @param transform Logical. Indicates whether to log2 transform the input `object` or not. Default: FALSE.
3334
#' @param outPCs Number of Principal Components to keep if `returnData` is TRUE. Default: 50.
@@ -42,23 +43,25 @@ nice_PCA <- function(object, annotations = NULL, PCs = c(1,2), ntop = NULL,
4243
legend_names = c(fill = "Sample Type", shape = "Library"),
4344
size = 5, alpha = 1, colors = NULL, shapes = NULL, title = NULL,
4445
legend_title = 16, legend_elements = 14, legend_pos = NULL,
45-
labels = NULL, name_tags = NULL, cluster_data = FALSE,
46+
labels = NULL, name_tags = NULL, cluster_data = FALSE, scale = FALSE,
4647
n_clusters = 3, transform = FALSE, outPCs = 50, returnData = FALSE)
4748

4849
{
4950
expr <- if (transform) log2(object + 0.001) else object
51+
52+
if (scale) {
53+
expr <- expr[matrixStats::rowVars(expr) > 0, , drop = FALSE]
54+
}
5055

5156
if (!is.null(ntop)) {
5257
# Estimate and select the top variances
5358
top.variances <- order(matrixStats::rowVars(expr), decreasing = TRUE)[1:min(ntop, nrow(expr))]
54-
55-
# Principal Component Analysis
56-
pca <- stats::prcomp(t(expr[top.variances, , drop = FALSE]), scale = TRUE)
57-
58-
} else {
59-
pca <- stats::prcomp(t(expr), scale = TRUE)
59+
expr <- expr[top.variances, , drop = FALSE]
6060
}
6161

62+
# Principal Component Analysis
63+
pca <- stats::prcomp(t(expr), scale. = scale)
64+
6265
# Calculate the percent of variance per component
6366
percentVar <- pca$sdev^2 / sum(pca$sdev^2)
6467

man/nice_PCA.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)