Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
TuomasBorman committed Oct 7, 2024
1 parent 1a4ce5e commit ee102d6
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions R/plotLoadings.R
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,22 @@ 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, absolute.scale = TRUE, ...) {
.plot_loadings <- function(
df, layout, absolute.scale = TRUE, show.color = TRUE, show.sign = FALSE,
...) {
#
if( !.is_a_bool(absolute.scale) ){
stop("'absolute.scale' must be TRUE or FALSE.", call. = FALSE)
}
#
if( !.is_a_bool(show.color) ){
stop("'show.color' must be TRUE or FALSE.", call. = FALSE)
}
#
if( !.is_a_bool(show.sign) ){
stop("'show.sign' must be TRUE or FALSE.", call. = FALSE)
}
#
# Initialize a plot
plot_out <- ggplot(df)
# Either create a heatmap or barplt
Expand Down Expand Up @@ -318,21 +328,36 @@ setMethod("plotLoadings", signature = c(x = "matrix"),

} else if( layout == "barplot" && absolute.scale ){
# This creates a barplot where bars are in absolute scale and the sing
# is denoted with +/-
# is denoted with +/- sign or color

# Create an aesthetic based on whether to show sign with colors
if( show.color ){
aesthetic <- aes(
x = Value_abs,
y = reorder_within(Feature, -Value_abs, PC),
fill = Sign
)
} else{
aesthetic <- aes(
x = Value_abs, y = reorder_within(Feature, -Value_abs, PC))
}
# Create bars with absolute scale
plot_out <- plot_out +

geom_bar(mapping = aesthetic, stat = "identity"
)
# Add sign that tells whether the value is + or -
if( show.sign ){
plot_out <- plot_out +
geom_text(aes(
x = max(Value_abs) + max(Value_abs)*0.1,
y = reorder_within(Feature, -Value_abs, PC),
label = Sign,
fontface = "bold"
))
}
# Final wrangle, set facets and order the data
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")
Expand Down

0 comments on commit ee102d6

Please sign in to comment.