Skip to content

Commit

Permalink
Add 'colors' Parameter to pathway_heatmap and pathway_pca Functions
Browse files Browse the repository at this point in the history
This update introduces a significant enhancement to the `pathway_heatmap` and `pathway_pca` functions by adding a new `colors` parameter. This parameter allows users to customize the color schemes of their heatmaps and PCA plots, providing a more tailored and visually appealing representation of their data.

#### Key Changes:
1. **`pathway_heatmap` Function:**
   - Added `colors` parameter to allow custom color choices for facet label backgrounds.
   - The parameter influences the background of the facet strips generated by `ggh4x::facet_nested`.
   - Default color set is used if `colors` is not specified or NULL.

2. **`pathway_pca` Function:**
   - Introduced `colors` parameter to customize the color scheme of PCA scatter and density plots.
   - Ensured compatibility with existing color mechanisms and added error checks for color vector length.
   - Default color palette applied if `colors` is NULL.

#### Enhanced Examples:
- Updated examples to demonstrate the use of the new `colors` parameter in both functions.
- Provided examples with both default and custom color sets for real and simulated datasets.

This update aims to provide users with greater control over the aesthetics of their plots, thereby enhancing the interpretability and presentation quality of their data analyses.
  • Loading branch information
cafferychen777 committed Dec 15, 2023
1 parent e32e9b7 commit d5c5042
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 36 deletions.
39 changes: 27 additions & 12 deletions R/pathway_heatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#' @param abundance A matrix or data frame of pathway abundance data, where columns correspond to samples and rows correspond to pathways.
#' @param metadata A data frame of metadata, where each row corresponds to a sample and each column corresponds to a metadata variable.
#' @param group A character string specifying the column name in the metadata data frame that contains the group variable.
#' @param colors A vector of colors used for the background of the facet labels in the
#' heatmap. These colors are applied to the background of the facet strips generated by
#' the 'ggh4x::facet_nested' function, allowing customization of the facet label appearance.
#' Each color in the vector corresponds to a group level defined in the 'group' parameter.
#' If NULL or not provided, a default color set is used for the facet strips.
#'
#' @return A ggplot heatmap object. The output is a ggplot object representing the heatmap of the predicted functional pathway abundance data. The heatmap visualizes the z score of pathways in different samples.
#' @export
Expand All @@ -32,26 +37,34 @@
#' metadata_example <- data.frame(sample_name = colnames(kegg_abundance_example),
#' group = factor(rep(c("Control", "Treatment"), each = 5)))
#'
#' # Create a heatmap
#' pathway_heatmap(kegg_abundance_example, metadata_example, "group")
#' # Custom colors for facet strips
#' custom_colors <- c("skyblue", "salmon")
#'
#' # Create a heatmap using custom colors for facet strips
#' pathway_heatmap(kegg_abundance_example, metadata_example, "group", colors = custom_colors)
#'
#' # Use real dataset
#' data("metacyc_abundance")
#' data("metadata")
#' metacyc_daa_results_df <- pathway_daa(abundance = metacyc_abundance %>%
#' column_to_rownames("pathway"),
#' metadata = metadata, group = "Environment", daa_method = "LinDA")
#' column_to_rownames("pathway"),
#' metadata = metadata, group = "Environment", daa_method = "LinDA")
#' annotated_metacyc_daa_results_df <- pathway_annotation(pathway = "MetaCyc",
#' daa_results_df = metacyc_daa_results_df, ko_to_kegg = FALSE)
#' daa_results_df = metacyc_daa_results_df, ko_to_kegg = FALSE)
#' feature_with_p_0.05 <- metacyc_daa_results_df %>% filter(p_adjust < 0.05)
#' pathway_heatmap(abundance = metacyc_abundance %>%
#' right_join(annotated_metacyc_daa_results_df %>%
#' select(all_of(c("feature","description"))), by = c("pathway" = "feature")) %>%
#' filter(pathway %in% feature_with_p_0.05$feature) %>%
#' select(-"pathway") %>%
#' column_to_rownames("description"), metadata = metadata, group = "Environment")
#' right_join(annotated_metacyc_daa_results_df %>%
#' select(all_of(c("feature","description"))), by = c("pathway" = "feature")) %>%
#' filter(pathway %in% feature_with_p_0.05$feature) %>%
#' select(-"pathway") %>%
#' column_to_rownames("description"),
#' metadata = metadata, group = "Environment", colors = custom_colors)
#' }
utils::globalVariables(c("rowname","Sample","Value","quantile","facet_nested","strip_nested","elem_list_rect"))
pathway_heatmap <- function(abundance, metadata, group) {
pathway_heatmap <- function(abundance,
metadata,
group,
colors) {
# Heatmaps use color changes to visualize changes in values. However, if the
# data for plotting the heat map are too different, for example, if the heat
# map is plotted using gene expression data, gene1 is expressed above 1000 in
Expand Down Expand Up @@ -110,7 +123,9 @@ pathway_heatmap <- function(abundance, metadata, group) {
# Compute breaks from the data
breaks <- range(long_df$Value, na.rm = TRUE)

colors <- c("#d93c3e", "#3685bc", "#6faa3e", "#e8a825", "#c973e6", "#ee6b3d", "#2db0a7", "#f25292")
if (is.null(colors)) {
colors <- c("#d93c3e", "#3685bc", "#6faa3e", "#e8a825", "#c973e6", "#ee6b3d", "#2db0a7", "#f25292")
}

# Create the heatmap using ggplot
p <-
Expand Down
39 changes: 28 additions & 11 deletions R/pathway_pca.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,26 @@
#' metadata_example <- data.frame(sample_name = colnames(kegg_abundance_example),
#' group = factor(rep(c("Control", "Treatment"), each = 5)))
#'
#' pca_plot <- pathway_pca(kegg_abundance_example, metadata_example, "group")
#' # Define custom colors for PCA plot
#' custom_colors <- c("blue", "red")
#'
#' # Generate PCA plot with custom colors
#' pca_plot <- pathway_pca(kegg_abundance_example, metadata_example, "group", colors = custom_colors)
#'
#' pca_plot <- pathway_pca(kegg_abundance_example, metadata_example, "group", colors = NULL)
#'
#' print(pca_plot)
#'
#' \donttest{
#' data("metacyc_abundance")
#' data("metadata")
#' pathway_pca(metacyc_abundance %>% column_to_rownames("pathway"), metadata, "Environment")
#' # Generate PCA plot for real dataset with custom colors
#' pathway_pca(metacyc_abundance %>% column_to_rownames("pathway"), metadata, "Environment", colors = c("green", "purple"))
#' }
pathway_pca <- function(abundance, metadata, group){
pathway_pca <- function(abundance,
metadata,
group,
colors = NULL){
# due to NSE notes in R CMD check
PC1 = PC2 = Group = NULL
# Perform PCA on the abundance data, keeping the first two principal components
Expand All @@ -43,18 +54,24 @@ pathway_pca <- function(abundance, metadata, group){

levels <- length(levels(factor(pca$Group)))

# Set default colors if colors are not provided
if (is.null(colors)) {
colors <- c("#d93c3e", "#3685bc","#208A42","#89288F","#F47D2B",
"#FEE500","#8A9FD1","#C06CAB","#E6C2DC","#90D5E4",
"#89C75F","#F37B7D","#9983BD","#D24B27","#3BBCA8",
"#6E4B9E","#0C727C", "#7E1416","#D8A767","#3D3D3D")[1:levels]
}


colors_choices <- c("#d93c3e", "#3685bc","#208A42","#89288F","#F47D2B",
"#FEE500","#8A9FD1","#C06CAB","#E6C2DC","#90D5E4",
"#89C75F","#F37B7D","#9983BD","#D24B27","#3BBCA8",
"#6E4B9E","#0C727C", "#7E1416","#D8A767","#3D3D3D")[1:levels]
# Ensure the number of colors matches the number of levels in Group
if (length(colors) != levels) {
stop("The length of colors vector must match the number of levels in Group")
}


# Create a ggplot object for the PCA scatter plot
Fig1a.taxa.pca <- ggplot2::ggplot(pca,ggplot2::aes(PC1,PC2))+
ggplot2::geom_point(size=2,ggplot2::aes(color=Group),show.legend = T)+
ggplot2::scale_color_manual(values=colors_choices)+
ggplot2::scale_color_manual(values=colors)+
ggplot2::stat_ellipse(ggplot2::aes(color = Group),fill="white",geom = "polygon",
level=0.95,alpha = 0.01,show.legend = F)+
ggplot2::labs(x=paste0("PC1(",round(pca_proportion[1],1),"%)"),y=paste0("PC2(",round(pca_proportion[2],1),"%)"),color = group)+
Expand All @@ -76,7 +93,7 @@ pathway_pca <- function(abundance, metadata, group){
ggplot2::ggplot(pca) +
ggplot2::geom_density(ggplot2::aes(x=PC1, group=Group, fill=Group), # Plot the density of PC1
color="black", alpha=1, position = 'identity',show.legend = F) +
ggplot2::scale_fill_manual(values=colors_choices) + # Manually set the fill color for each group
ggplot2::scale_fill_manual(values=colors) + # Manually set the fill color for each group
ggplot2::theme_classic() + # Set the classic theme
ggplot2::scale_y_discrete(expand = c(0,0.001)) + # Scale the y-axis with a small expansion to improve appearance
ggplot2::labs(x=NULL, y=NULL) + # Remove x and y labels
Expand All @@ -88,7 +105,7 @@ pathway_pca <- function(abundance, metadata, group){
ggplot2::ggplot(pca) +
ggplot2::geom_density(ggplot2::aes(x=PC2, group=Group, fill=Group), # Plot the density of PC2
color="black", alpha=1, position = 'identity',show.legend = F) +
ggplot2::scale_fill_manual(values=colors_choices) + # Manually set the fill color for each group
ggplot2::scale_fill_manual(values=colors) + # Manually set the fill color for each group
ggplot2::theme_classic() + # Set the classic theme
ggplot2::scale_y_discrete(expand = c(0,0.001)) + # Scale the y-axis with a small expansion to improve appearance
ggplot2::labs(x=NULL, y=NULL) + # Remove x and y labels
Expand Down
31 changes: 21 additions & 10 deletions man/pathway_heatmap.Rd

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

14 changes: 11 additions & 3 deletions man/pathway_pca.Rd

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

0 comments on commit d5c5042

Please sign in to comment.