Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plotLoadings, absolute scale #152

Merged
merged 2 commits into from
Oct 6, 2024
Merged
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: miaViz
Title: Microbiome Analysis Plotting and Visualization
Version: 1.13.11
Version: 1.13.12
Authors@R:
c(person(given = "Tuomas", family = "Borman", role = c("aut", "cre"),
email = "[email protected]",
Expand Down
44 changes: 40 additions & 4 deletions R/plotLoadings.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#' \itemize{
#' \item \code{n}: \code{Integer scalar}. Number of features to be plotted.
#' Applicable when \code{layout="barplot"}. (Default: \code{10}))
#'
#' \item \code{absolute.scale}: ("barplot") \code{Logical scalar}. Specifies
#' whether a barplot should be visualized in absoltue scale.
#' (Default: \code{TRUE})
#' }
#'
#' @details
Expand Down Expand Up @@ -244,6 +248,11 @@ setMethod("plotLoadings", signature = c(x = "matrix"),
}
# Convert into data.frame
res <- as.data.frame(res)
# Add column that shows the values in absolute scale, and another column
# showing sign
res[["Value_abs"]] <- abs(res[["Value"]])
res[["Sign"]] <- ifelse(
res[["Value"]] > 0, "+", ifelse(res[["Value"]] < 0, "-", ""))
return(res)
}

Expand All @@ -269,7 +278,12 @@ setMethod("plotLoadings", signature = c(x = "matrix"),
# This functions plots a data.frame in barplot or heatmap layout.
#' @importFrom tidytext scale_y_reordered reorder_within
#' @importFrom ggplot2 geom_tile scale_fill_gradient2 geom_bar
.plot_loadings <- function(df, layout, ...) {
.plot_loadings <- function(df, layout, absolute.scale = TRUE, ...) {
#
if( !.is_a_bool(absolute.scale) ){
stop("'absolute.scale' must be TRUE or FALSE.", call. = FALSE)
}
#
# Initialize a plot
plot_out <- ggplot(df)
# Either create a heatmap or barplt
Expand All @@ -286,21 +300,43 @@ setMethod("plotLoadings", signature = c(x = "matrix"),
low = "darkslateblue", mid = "white", high = "darkred"
)

} else if( layout == "barplot" ){
} else if( layout == "barplot" && !absolute.scale ){
# This creates a barplot where values can be negative or positive
# (bars can be in negative and positive side)
plot_out <- plot_out +
# Create a bar plot. Create unique facets for each PC. Each PC can
# have unique set of features. To reorder features by each facet,
# we use reorder_within() and scale_y_reordered().
geom_bar(
mapping = aes(
x = Value, y = reorder_within(Feature, Value, PC)),
stat = "identity",
width = 0.8
stat = "identity"
) +
scale_y_reordered() +
facet_wrap(~ PC, scales = "free") +
labs(x = "Value", y = "Feature")

} else if( layout == "barplot" && absolute.scale ){
# This creates a barplot where bars are in absolute scale and the sing
# is denoted with +/-
plot_out <- plot_out +
# Create bars with absolute scale
geom_bar(
mapping = aes(
x = Value_abs, y = reorder_within(Feature, -Value_abs, PC)),
stat = "identity"
) +
# Add sign that tells whether the value is + or -
geom_text(aes(
x = max(Value_abs) + max(Value_abs)*0.1,
y = reorder_within(Feature, Value_abs, PC),
label = Sign,
fontface = "bold"
)) +
scale_y_reordered() +
facet_wrap(~ PC, scales = "free") +
labs(x = "Value", y = "Feature")

}
# Adjust theme
plot_out <- plot_out +
Expand Down
4 changes: 4 additions & 0 deletions man/plotLoadings.Rd

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

Loading