Skip to content

Commit

Permalink
Addition of mStat_get_palette Function and Update of Palette Paramete…
Browse files Browse the repository at this point in the history
…rs in Other Functions

This update introduces significant enhancements in the color palette management across our
visualization functionalities.

### Key Changes:
1. **Addition of `mStat_get_palette` Function**: A new function, `mStat_get_palette`, has been integrated into our codebase. This function provides a more versatile and robust approach to managing color palettes. It supports predefined palette names, custom color vectors, and ensures fallback to a default palette if needed. This enhancement allows for greater flexibility and ease of use in color customization for our users.

2. **Updates in Palette Parameter Usage**: We've updated several functions to utilize the new `mStat_get_palette` function. This change ensures consistency in color palette handling across different parts of the application. The functions with updated palette parameters now benefit from the enhanced capabilities of `mStat_get_palette`, including access to a broader range of color options and more intuitive palette selection.

3. **Improved User Experience and Customization**: These updates collectively aim to enhance the user experience by providing more control over color scheme selections in heatmap visualizations. Users can now easily select from a range of predefined color palettes or define their own, enhancing the visual appeal and interpretability of their heatmaps.
  • Loading branch information
cafferychen777 committed Jan 8, 2024
1 parent 15edb42 commit dd3c0d4
Show file tree
Hide file tree
Showing 78 changed files with 1,137 additions and 730 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export(mStat_filter)
export(mStat_generate_report_long)
export(mStat_generate_report_pair)
export(mStat_generate_report_single)
export(mStat_get_palette)
export(mStat_import_biom_as_data_obj)
export(mStat_import_dada2_as_data_obj)
export(mStat_import_mothur_as_data_obj)
Expand Down
31 changes: 12 additions & 19 deletions R/generate_alpha_boxplot_long.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
#' @param time.var The variable in the metadata table that represents the time.
#' @param t0.level Character or numeric, baseline time point for longitudinal analysis, e.g. "week_0" or 0. Required.
#' @param ts.levels Character vector, names of follow-up time points, e.g. c("week_4", "week_8"). Required.
#' @param palette An optional color palette for the plot. If not provided, a default color palette will be used. The palette should be a vector of color codes in a format accepted by ggplot2 (e.g., hexadecimal color codes). The number of colors in the palette should be at least as large as the number of groups being plotted.
#' @param palette An optional parameter specifying the color palette to be used for the plot.
#' It can be either a character string specifying the name of a predefined
#' palette or a vector of color codes in a format accepted by ggplot2
#' (e.g., hexadecimal color codes). Available predefined palettes include
#' 'npg', 'aaas', 'nejm', 'lancet', 'jama', 'jco', and 'ucscgb', inspired
#' by various scientific publications and the `ggsci` package. If `palette`
#' is not provided or an unrecognized palette name is given, a default color
#' palette will be used. Ensure the number of colors in the palette is at
#' least as large as the number of groups being plotted.
#' @param group.var An optional variable in the metadata table that represents the grouping factor.
#' @param strata.var An optional variable in the metadata table that represents the stratification factor.
#' @param adj.vars A character vector of variable names to be used for adjustment.
Expand Down Expand Up @@ -117,7 +125,7 @@
#' adj.vars = NULL,
#' base.size = 16,
#' theme.choice = "bw",
#' palette = NULL,
#' palette = "lancet",
#' pdf = TRUE,
#' file.ann = NULL,
#' pdf.wid = 20,
Expand Down Expand Up @@ -224,23 +232,8 @@ generate_alpha_boxplot_long <- function (data.obj,
custom.theme else
theme_function

if (is.null(palette)) {
col <-
c(
"#E31A1C",
"#1F78B4",
"#FB9A99",
"#33A02C",
"#FDBF6F",
"#B2DF8A",
"#A6CEE3",
"#BA7A70",
"#9D4E3F",
"#829BAB"
)
} else{
col <- palette
}
# Use mStat_get_palette to set the color palette
col <- mStat_get_palette(palette)

if (is.null(group.var)) {
alpha_df <- alpha_df %>% dplyr::mutate("ALL" = "ALL")
Expand Down
30 changes: 11 additions & 19 deletions R/generate_alpha_boxplot_single.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@
#' ```
#'
#' Then pass `my_theme` to `custom.theme`. Default is NULL, which will use the default theme based on `theme.choice`.
#' @param palette An optional palette to use for the plot. If NULL (default), a pre-defined palette will be used.
#' @param palette An optional parameter specifying the color palette to be used for the plot.
#' It can be either a character string specifying the name of a predefined
#' palette or a vector of color codes in a format accepted by ggplot2
#' (e.g., hexadecimal color codes). Available predefined palettes include
#' 'npg', 'aaas', 'nejm', 'lancet', 'jama', 'jco', and 'ucscgb', inspired
#' by various scientific publications and the `ggsci` package. If `palette`
#' is not provided or an unrecognized palette name is given, a default color
#' palette will be used. Ensure the number of colors in the palette is at
#' least as large as the number of groups being plotted.
#' @param pdf A boolean indicating whether to save the output as a PDF file. Default is TRUE.
#' @param file.ann A string for annotating the output file name.
#' @param pdf.wid The width of the output PDF file. Default is 11.
Expand Down Expand Up @@ -130,7 +138,7 @@
#' adj.vars = "sex",
#' base.size = 16,
#' theme.choice = "bw",
#' palette = NULL,
#' palette = "lancet",
#' pdf = FALSE,
#' file.ann = NULL,
#' pdf.wid = 11,
Expand Down Expand Up @@ -209,23 +217,7 @@ generate_alpha_boxplot_single <- function (data.obj,
else
theme_function

if (is.null(palette)) {
col <-
c(
"#E31A1C",
"#1F78B4",
"#FB9A99",
"#33A02C",
"#FDBF6F",
"#B2DF8A",
"#A6CEE3",
"#BA7A70",
"#9D4E3F",
"#829BAB"
)
} else{
col <- palette
}
col <- mStat_get_palette(palette)

# Create a plot for each alpha diversity index
plot_list <- lapply(alpha.name, function(index) {
Expand Down
30 changes: 11 additions & 19 deletions R/generate_alpha_change_boxplot_pair.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@
#' ```
#'
#' Then pass `my_theme` to `custom.theme`. Default is NULL, which will use the default theme based on `theme.choice`.
#' @param palette An optional color palette for the plot. If not provided, a default color palette will be used. The palette should be a vector of color codes in a format accepted by ggplot2 (e.g., hexadecimal color codes). The number of colors in the palette should be at least as large as the number of groups being plotted.
#' @param palette An optional parameter specifying the color palette to be used for the plot.
#' It can be either a character string specifying the name of a predefined
#' palette or a vector of color codes in a format accepted by ggplot2
#' (e.g., hexadecimal color codes). Available predefined palettes include
#' 'npg', 'aaas', 'nejm', 'lancet', 'jama', 'jco', and 'ucscgb', inspired
#' by various scientific publications and the `ggsci` package. If `palette`
#' is not provided or an unrecognized palette name is given, a default color
#' palette will be used. Ensure the number of colors in the palette is at
#' least as large as the number of groups being plotted.
#' @param pdf.wid The width of the output PDF file. Default is 11.
#' @param pdf.hei The height of the output PDF file. Default is 8.5.
#' @param subject.var The variable in the metadata table that represents the subject.
Expand Down Expand Up @@ -82,7 +90,7 @@
#' alpha.change.func = "absolute change",
#' base.size = 16,
#' theme.choice = "bw",
#' palette = NULL,
#' palette = "lancet",
#' pdf = TRUE,
#' file.ann = NULL,
#' pdf.wid = 11,
Expand Down Expand Up @@ -201,23 +209,7 @@ generate_alpha_change_boxplot_pair <-

combined_alpha <- dplyr::bind_cols(combined_alpha, diff_columns)

if (is.null(palette)) {
col <-
c(
"#E31A1C",
"#1F78B4",
"#FB9A99",
"#33A02C",
"#FDBF6F",
"#B2DF8A",
"#A6CEE3",
"#BA7A70",
"#9D4E3F",
"#829BAB"
)
} else{
col <- palette
}
col <- mStat_get_palette(palette)

facet_formula <-
if (!is.null(strata.var)) {
Expand Down
10 changes: 6 additions & 4 deletions R/generate_alpha_dotplot_long.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@
#' adj.vars = "sample_body_site"
#' )
#' # Visualizing the results for the Type 2 Diabetes dataset
#' dotplot_T2D <- generate_taxa_dotplot_long(
#' dotplot_T2D <- generate_alpha_dotplot_long(
#' data.obj = subset_T2D.obj,
#' test.list = result2,
#' group.var = "subject_race",
#' time.var = "visit_number",
#' t0.level = unique(subset_T2D.obj$meta.dat$visit_number)[1],
#' ts.levels = unique(subset_T2D.obj$meta.dat$visit_number)[-1],
#' ts.levels = unique(subset_T2D.obj$meta.dat$visit_number)[-1]
#' )
#' }
#' @export
Expand All @@ -79,7 +79,7 @@ generate_alpha_dotplot_long <- function(data.obj,
base.size = 16,
theme.choice = "bw",
custom.theme = NULL,
palette = NULL,
palette = rev(c("white", "#92c5de", "#0571b0", "#f4a582", "#ca0020")),
pdf = FALSE,
pdf.wid = 7,
pdf.hei = 5
Expand Down Expand Up @@ -129,6 +129,8 @@ generate_alpha_dotplot_long <- function(data.obj,

p_val_var <- "P.Value"

col <- mStat_get_palette(palette)

plot.list <- lapply(group.names, function(group.names){

data_for_plot <- test.list[[group.names]]
Expand All @@ -140,7 +142,7 @@ generate_alpha_dotplot_long <- function(data.obj,
dotplot <- ggplot(data_for_plot, aes(x = !!sym(time.var), y = Term, size = Estimate)) +
geom_point(aes(color = !!sym(p_val_var)), alpha = 0.6, shape = 19) +
geom_text(aes(label = Significance_Label), vjust = 0.8, show.legend = FALSE, color = "white") +
scale_color_gradientn(colors = rev(c("white", "#92c5de", "#0571b0", "#f4a582", "#ca0020"))) +
scale_color_gradientn(colors = col) +
labs(title = group.names,
x = time.var,
y = "Term",
Expand Down
51 changes: 31 additions & 20 deletions R/generate_alpha_spaghettiplot_long.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
#' @param t0.level Character or numeric, baseline time point for longitudinal analysis, e.g. "week_0" or 0. Required.
#' @param ts.levels Character vector, names of follow-up time points, e.g. c("week_4", "week_8"). Required.
#' @param base.size The base font size for the plot.
#' @param palette An optional color palette for the plot. If not provided, a default color palette will be used. The palette should be a vector of color codes in a format accepted by ggplot2 (e.g., hexadecimal color codes). The number of colors in the palette should be at least as large as the number of groups being plotted.
#' @param palette An optional parameter specifying the color palette to be used for the plot.
#' It can be either a character string specifying the name of a predefined
#' palette or a vector of color codes in a format accepted by ggplot2
#' (e.g., hexadecimal color codes). Available predefined palettes include
#' 'npg', 'aaas', 'nejm', 'lancet', 'jama', 'jco', and 'ucscgb', inspired
#' by various scientific publications and the `ggsci` package. If `palette`
#' is not provided or an unrecognized palette name is given, a default color
#' palette will be used. Ensure the number of colors in the palette is at
#' least as large as the number of groups being plotted.
#' @param theme.choice Plot theme choice. Can be one of:
#' - "prism": ggprism::theme_prism()
#' - "classic": theme_classic()
Expand Down Expand Up @@ -44,7 +52,6 @@
#' \dontrun{
#' data("subset_T2D.obj")
#' T2D.alpha.obj <- mStat_calculate_alpha_diversity(subset_T2D.obj$feature.tab,"shannon")
#'
#' generate_alpha_spaghettiplot_long(
#' data.obj = subset_T2D.obj,
#' alpha.obj = T2D.alpha.obj,
Expand All @@ -57,7 +64,27 @@
#' strata.var = "subject_race",
#' adj.vars = "sample_body_site",
#' theme.choice = "bw",
#' palette = NULL,
#' palette = "lancet",
#' pdf = TRUE,
#' file.ann = NULL,
#' pdf.wid = 11,
#' pdf.hei = 8.5
#' )
#'
#' data("ecam.obj")
#' generate_alpha_spaghettiplot_long(
#' data.obj = ecam.obj,
#' alpha.obj = NULL,
#' alpha.name = c("shannon","simpson", "observed_species"),
#' subject.var = "subject.id",
#' time.var = "month_num",
#' t0.level = NULL,
#' ts.levels = NULL,
#' group.var = "delivery",
#' strata.var = "diet",
#' adj.vars = NULL,
#' theme.choice = "bw",
#' palette = "lancet",
#' pdf = TRUE,
#' file.ann = NULL,
#' pdf.wid = 11,
Expand Down Expand Up @@ -167,23 +194,7 @@ generate_alpha_spaghettiplot_long <-

theme_to_use <- if (!is.null(custom.theme)) custom.theme else theme_function

if (is.null(palette)){
col <-
c(
"#E31A1C",
"#1F78B4",
"#FB9A99",
"#33A02C",
"#FDBF6F",
"#B2DF8A",
"#A6CEE3",
"#BA7A70",
"#9D4E3F",
"#829BAB"
)
} else{
col <- palette
}
col <- mStat_get_palette(palette)

# Create a plot for each alpha diversity index
plot_list <- lapply(alpha.name, function(index) {
Expand Down
30 changes: 11 additions & 19 deletions R/generate_beta_change_boxplot_pair.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@
#' ```
#'
#' Then pass `my_theme` to `custom.theme`. Default is NULL, which will use the default theme based on `theme.choice`.
#' @param palette (Optional) A palette function or character vector with the colors for the plot.
#' @param palette An optional parameter specifying the color palette to be used for the plot.
#' It can be either a character string specifying the name of a predefined
#' palette or a vector of color codes in a format accepted by ggplot2
#' (e.g., hexadecimal color codes). Available predefined palettes include
#' 'npg', 'aaas', 'nejm', 'lancet', 'jama', 'jco', and 'ucscgb', inspired
#' by various scientific publications and the `ggsci` package. If `palette`
#' is not provided or an unrecognized palette name is given, a default color
#' palette will be used. Ensure the number of colors in the palette is at
#' least as large as the number of groups being plotted.
#' @param pdf A logical value indicating whether to save the plot as a PDF file. Default is TRUE
#' @param file.ann A character string specifying a custom annotation for the PDF file name or NULL (default)
#' @param pdf.wid (Optional) The width of the PDF file if `pdf` is set to `TRUE` (default is 11).
Expand Down Expand Up @@ -79,7 +87,7 @@
#' base.size = 20,
#' theme.choice = "bw",
#' custom.theme = NULL,
#' palette = NULL,
#' palette = "lancet",
#' pdf = TRUE,
#' file.ann = NULL,
#' pdf.wid = 11,
Expand Down Expand Up @@ -138,23 +146,7 @@ generate_beta_change_boxplot_pair <-
change.after <-
unique(meta_tab %>% dplyr::select(all_of(c(time.var))))[unique(meta_tab %>% dplyr::select(all_of(c(time.var)))) != change.base]

if (is.null(palette)){
col <-
c(
"#E31A1C",
"#1F78B4",
"#FB9A99",
"#33A02C",
"#FDBF6F",
"#B2DF8A",
"#A6CEE3",
"#BA7A70",
"#9D4E3F",
"#829BAB"
)
} else {
col <- palette
}
col <- mStat_get_palette(palette)

theme_function <- switch(
theme.choice,
Expand Down
28 changes: 10 additions & 18 deletions R/generate_beta_change_spaghettiplot_long.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@
#' ```
#'
#' Then pass `my_theme` to `custom.theme`. Default is NULL, which will use the default theme based on `theme.choice`.
#' @param palette (Optional) A palette function or character vector with the colors for the plot.
#' @param palette An optional parameter specifying the color palette to be used for the plot.
#' It can be either a character string specifying the name of a predefined
#' palette or a vector of color codes in a format accepted by ggplot2
#' (e.g., hexadecimal color codes). Available predefined palettes include
#' 'npg', 'aaas', 'nejm', 'lancet', 'jama', 'jco', and 'ucscgb', inspired
#' by various scientific publications and the `ggsci` package. If `palette`
#' is not provided or an unrecognized palette name is given, a default color
#' palette will be used. Ensure the number of colors in the palette is at
#' least as large as the number of groups being plotted.
#' @param pdf (Optional) A boolean indicating whether to save the output as a PDF file (default is TRUE).
#' @param file.ann (Optional) A string for annotating the output file name.
#' @param pdf.wid (Optional) The width of the PDF file if `pdf` is set to `TRUE` (default is 11).
Expand Down Expand Up @@ -131,23 +139,7 @@ generate_beta_change_spaghettiplot_long <-
!is.character(strata.var))
stop("`strata.var` should be a character string or NULL.")

if (is.null(palette)){
col <-
c(
"#E31A1C",
"#1F78B4",
"#FB9A99",
"#33A02C",
"#FDBF6F",
"#B2DF8A",
"#A6CEE3",
"#BA7A70",
"#9D4E3F",
"#829BAB"
)
} else{
col <- palette
}
col <- mStat_get_palette(palette)

theme_function <- switch(theme.choice,
prism = ggprism::theme_prism(),
Expand Down
Loading

0 comments on commit dd3c0d4

Please sign in to comment.