Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 72 additions & 11 deletions R/plotLoadings.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
#' \item \code{absolute.scale}: ("barplot", "lollipop") \code{Logical scalar}.
#' Specifies whether a barplot or a lollipop plot should be visualized in
#' absolute scale. (Default: \code{TRUE})
#'
#' \item \code{sort.method}: ("heatmap") \code{Character scalar} or
#' \code{function}. Specifies sorting method. If it specifies column, i.e.,
#' principal coordinate, features are ordered in dcreasing order based on the
#' specified coordinate. If \code{"neatsort"}, \code{getNeatOrder()} is
#' utilized. If \code{function}, the argument is passed to
#' \code{\link[bluster:clusterRows]{bluster:clusterRows()}} \code{BLUSPARAM}
#' parameter. (Default: \code{NULL})
#' }
#'
#' @details
Expand Down Expand Up @@ -65,7 +73,7 @@
#' plotLoadings(tse, dimred = "PCA", layout = "heatmap", add.tree = TRUE) |>
#' # Remove this line to see messages
#' suppressMessages()
#'
#'
#'
#' # Plotting matrix as a barplot
#' loadings_matrix <- attr(reducedDim(tse, "PCA"), "rotation")
Expand All @@ -78,7 +86,10 @@
#' plotLoadings(loadings_matrix, layout = "heatmap")
#'
#' # Plot with less components
#' plotLoadings(tse, "PCA", layout = "heatmap", ncomponents = 2)
#' plotLoadings(
#' tse, "PCA", layout = "heatmap",
#' ncomponents = 2, sort.method = "neatsort"
#' )
#'
NULL

Expand Down Expand Up @@ -230,8 +241,6 @@

# This function manipulates the loadings data into correct format. The output
# is data.frame in long format directly usable for ggplot.
#' @importFrom tibble rownames_to_column
#' @importFrom tidyr pivot_longer
.get_loadings_plot_data <- function(df, layout, ncomponents, n = 10, ...) {
# Transform into a dataframe
df <- as.data.frame(df)
Expand All @@ -244,13 +253,7 @@
res <- do.call(rbind, res)
} else{
# For heatmap, the whole data.frame is just converted into long format.
components <- colnames(df)
res <- df %>%
rownames_to_column(var = "Feature") %>%
pivot_longer(
cols = components,
names_to = "PC",
values_to = "Value")
res <- .process_heatmap_component(df, ...)
}
# Convert into data.frame
res <- as.data.frame(res)
Expand Down Expand Up @@ -283,6 +286,64 @@
return(df)
}

# This function modifies the data so that it is ready to be plotted as
# heatmap.
#' @importFrom tibble rownames_to_column
#' @importFrom tidyr pivot_longer
.process_heatmap_component <- function(
df, sort.method = BLUSPARAM, BLUSPARAM = NULL, ...){
components <- colnames(df)
supported_methods <- c("neatsort", components)
if( !(is.null(sort.method) ||
(.is_a_string(sort.method) && sort.method %in% supported_methods) ||
is.function(sort.method) ) ){
stop("'sort.method' must be NULL, a single character value from the ",
"following options '", paste0(supported_methods, collapse = "', '"),
"' or a BLUSPARAM function (please see bluster::clusterRows() for ",
"details.", call. = FALSE)

Check warning on line 303 in R/plotLoadings.R

View check run for this annotation

Codecov / codecov/patch

R/plotLoadings.R#L300-L303

Added lines #L300 - L303 were not covered by tests
}
#
# Get feature order
feat_order <- NULL
if( !is.null(sort.method) ){
feat_order <- .get_feature_order(
df = df, sort.method = sort.method, ...)

Check warning on line 310 in R/plotLoadings.R

View check run for this annotation

Codecov / codecov/patch

R/plotLoadings.R#L309-L310

Added lines #L309 - L310 were not covered by tests
}
# Put data into long format
res <- df |>
rownames_to_column(var = "Feature") |>
pivot_longer(
cols = components,
names_to = "PC",
values_to = "Value")
# Sort the data
if( !is.null(feat_order) ){
res[["Feature"]] <- factor(res[["Feature"]], levels = feat_order)

Check warning on line 321 in R/plotLoadings.R

View check run for this annotation

Codecov / codecov/patch

R/plotLoadings.R#L321

Added line #L321 was not covered by tests
}
return(res)
}

# This function gets feature order based on clustering, principal component
# values or neatsort.
.get_feature_order <- function(df, sort.method, ...){
df <- as.matrix(df)

Check warning on line 329 in R/plotLoadings.R

View check run for this annotation

Codecov / codecov/patch

R/plotLoadings.R#L329

Added line #L329 was not covered by tests
# Get order based on single column
if( sort.method %in% colnames(df) ){
feat_order <- order(df[[sort.method]], decreasing = TRUE)
} else if( sort.method %in% c("neatsort") ){

Check warning on line 333 in R/plotLoadings.R

View check run for this annotation

Codecov / codecov/patch

R/plotLoadings.R#L331-L333

Added lines #L331 - L333 were not covered by tests
# Get order based on neatsort. It can take only 2 columns as input.
feat_order <- getNeatOrder(
df[, seq_len(min(2, ncol(df))), drop = FALSE], ...)

Check warning on line 336 in R/plotLoadings.R

View check run for this annotation

Codecov / codecov/patch

R/plotLoadings.R#L335-L336

Added lines #L335 - L336 were not covered by tests
} else{
# Get order based on clustering
.require_package("bluster")
feat_order <- clusterRows(df, BLUSPARAM = sort.method) |> order()

Check warning on line 340 in R/plotLoadings.R

View check run for this annotation

Codecov / codecov/patch

R/plotLoadings.R#L339-L340

Added lines #L339 - L340 were not covered by tests
}
# Now we have order as indices. Get the feature names in correct order.
feat_order <- rownames(df)[ feat_order ]
return(feat_order)

Check warning on line 344 in R/plotLoadings.R

View check run for this annotation

Codecov / codecov/patch

R/plotLoadings.R#L343-L344

Added lines #L343 - L344 were not covered by tests
}

# This function calculates place for +/- sign in barplot/lollipop plot
#' @importFrom dplyr %>% group_by mutate case_when ungroup
.calculate_max_and_min_for_loadings <- function(df){
Expand Down
13 changes: 12 additions & 1 deletion man/plotLoadings.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.