From c29a8371a3b19bf39c999fba039053ee008fd4a8 Mon Sep 17 00:00:00 2001 From: zerostwo Date: Sun, 8 Oct 2023 12:11:43 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7Adjusted=20parameters=20of=20some?= =?UTF-8?q?=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DESCRIPTION | 5 + NAMESPACE | 3 + R/cat_modify_plot.R | 698 +++++++++++++++++------------------ R/color.R | 2 + R/scrna-seq.R | 166 +++++++-- R/utilities.R | 496 ++++++++++++++----------- catplot.Rproj | 40 +- man/cat_dimplot.Rd | 65 +++- man/cat_dotplot.Rd | 39 +- man/cat_featureplot.Rd | 2 + man/cat_save.Rd | 4 +- man/cat_set_path.Rd | 22 ++ man/cat_vlnplot.Rd | 2 + man/create_ap.Rd | 9 +- man/dot-get_ggsci_palette.Rd | 2 + 15 files changed, 923 insertions(+), 632 deletions(-) create mode 100644 man/cat_set_path.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 62ac556..38b7ce3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,7 +23,12 @@ Imports: ggsignif, magrittr, monochromeR, + RColorBrewer, rlang Suggests: + ggsci, + Seurat, + SeuratObject, + stringr, testthat (>= 3.0.0) Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index 2eb9949..51227ee 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,12 +9,14 @@ export(cat_modify_color) export(cat_modify_data) export(cat_modify_plot) export(cat_save) +export(cat_set_path) export(cat_vlnplot) export(create_ap) export(generate_colors) export(scale_color_catplot) export(scale_fill_catplot) export(theme_cat) +importFrom(RColorBrewer,brewer.pal) importFrom(dplyr,arrange) importFrom(dplyr,case_when) importFrom(dplyr,desc) @@ -71,3 +73,4 @@ importFrom(magrittr,"%>%") importFrom(rlang,"%||%") importFrom(rlang,arg_match0) importFrom(rlang,list2) +importFrom(utils,write.csv) diff --git a/R/cat_modify_plot.R b/R/cat_modify_plot.R index 08d7426..3421ff5 100644 --- a/R/cat_modify_plot.R +++ b/R/cat_modify_plot.R @@ -1,349 +1,349 @@ -#' Custom theme for catplot -#' -#' @param font_size Numeric, font size (default: 8) -#' @param linewidth Numeric, line width (default: 0.5) -#' @param aspect_ratio Numeric, aspect ratio of the plot (default: NULL) -#' @param frame Character, frame type, "closed" or "open" (default: "closed") -#' @param show_panel_grid Character, panel grid visibility (default: "both_not") -#' @param show_title Character, axis title visibility (default: "both") -#' @param show_text Character, axis text visibility (default: "both") -#' @param text_italic Character, axis text italicization (default: "both_not") -#' @param show_ticks Character, axis tick visibility (default: "both") -#' @param ticks_length Numeric, tick length (default: 4) -#' @param legend_position Character, position of legend (default: "right") -#' @param legend_direction Character, direction of legend (default: NULL) -#' @return A ggplot2 theme -#' @export -#' @examples -#' library(catplot) -#' library(ggplot2) -#' -#' p <- ggplot(mpg, aes(x = manufacturer, y = hwy)) + -#' geom_point() -#' p + theme_cat() -theme_cat <- function(font_size = 8, - linewidth = 0.5, - aspect_ratio = NULL, - frame = "closed", - show_panel_grid = "both_not", - show_title = "both", - show_text = "both", - text_italic = "both_not", - show_ticks = "both", - ticks_length = 4, - legend_position = "right", - legend_direction = NULL) { - linewidth <- linewidth * 0.5 / 1.07 - - arg_list <- list( - frame = c("closed", "open"), - show_panel_grid = c("x", "y", "both", "both_not"), - show_title = c("x", "y", "both", "both_not"), - show_ticks = c("x", "y", "both", "both_not"), - show_text = c("x", "y", "both", "both_not"), - text_italic = c("x", "y", "both", "both_not"), - legend_position = c("right", "left", "bottom", "top") - ) - - frame <- match.arg(frame, arg_list$frame) - show_panel_grid <- - match.arg(show_panel_grid, arg_list$show_panel_grid) - show_title <- match.arg(show_title, arg_list$show_title) - show_ticks <- match.arg(show_ticks, arg_list$show_ticks) - show_text <- match.arg(show_text, arg_list$show_text) - text_italic <- match.arg(text_italic, arg_list$text_italic) - legend_position <- - match.arg(legend_position, arg_list$legend_position) - - legend_margin <- switch(legend_position, - "right" = margin(l = -8), - "top" = margin(b = -8), - "bottom" = margin(t = -8), - "left" = margin(r = -8) - ) - legend_direction <- legend_direction %||% "default" - legend_direction <- switch(legend_position, - "h" = "horizontal", - "horizontal" = "horizontal", - "v" = "vertical", - "vertical" = "vertical", - "default" = "NULL" - ) - - tick_element <- - element_line( - linewidth = linewidth, - lineend = "square", - colour = "black" - ) - title_element <- - element_text( - size = font_size, - face = "plain", - colour = "black" - ) - grid_element <- element_line( - colour = "lightgrey", - linewidth = linewidth, - lineend = "square" - ) - theme_base <- theme( - aspect.ratio = aspect_ratio, - axis.ticks.length = unit(ticks_length, "pt"), - panel.background = element_blank(), - panel.grid = element_blank(), - legend.background = element_blank(), - legend.title = title_element, - legend.text = title_element, - legend.key = element_blank(), - legend.key.height = unit(font_size, "pt"), - legend.key.width = unit(font_size, "pt"), - legend.position = legend_position, - legend.margin = legend_margin, - legend.direction = legend_direction, - plot.background = element_blank(), - plot.title = element_text( - size = font_size, - hjust = 0.5, - vjust = -1, - face = "plain", - colour = "black" - ), - plot.subtitle = element_text( - hjust = 0.5, - size = font_size, - face = "plain", - colour = "black" - ), - strip.background = element_blank(), - strip.text = title_element - ) - - # Frame - frame_theme <- switch(frame, - "open" = theme( - axis.line = tick_element, - panel.border = element_blank() - ), - "closed" = theme( - axis.line = element_blank(), - panel.border = element_rect( - linewidth = linewidth, - fill = NA, - colour = "black" - ) - ) - ) - # Title - title_theme <- switch(show_title, - "x" = theme( - axis.title.x = title_element, - axis.title.y = element_blank() - ), - "y" = theme( - axis.title.y = title_element, - axis.title.x = element_blank() - ), - "both" = theme(axis.title = title_element), - "both_not" = theme(axis.title = element_blank()) - ) - # Ticks - ticks_theme <- switch(show_ticks, - "x" = theme( - axis.ticks.x = tick_element, - axis.ticks.y = element_blank() - ), - "y" = theme( - axis.ticks.y = tick_element, - axis.ticks.x = element_blank() - ), - "both" = theme(axis.ticks = tick_element), - "both_not" = theme(axis.ticks = element_blank()) - ) - # Fontface - x_fontface <- ifelse(text_italic == "x" | - text_italic == "both", "italic", "plain") - y_fontface <- ifelse(text_italic == "y" | - text_italic == "both", "italic", "plain") - text_theme <- switch(show_text, - "x" = theme( - axis.text.x = element_text( - size = font_size, - face = x_fontface, - colour = "black" - ), - axis.text.y = element_blank() - ), - "y" = theme( - axis.text.y = element_text( - size = font_size, - face = y_fontface, - colour = "black" - ), - axis.text.x = element_blank() - ), - "both" = theme( - axis.text.x = element_text( - size = font_size, - face = x_fontface, - colour = "black" - ), - axis.text.y = element_text( - size = font_size, - face = x_fontface, - colour = "black" - ) - ), - "both_not" = theme(axis.text = element_blank()) - ) - - # Panel grid - grid_theme <- switch(show_panel_grid, - "x" = theme( - panel.grid.major.x = grid_element, - panel.grid.major.y = element_blank() - ), - "y" = theme( - panel.grid.major.y = grid_element, - panel.grid.major.x = element_blank() - ), - "both" = theme(panel.grid = grid_element), - "both_not" = theme(panel.grid = element_blank()) - ) - theme_modifications <- list( - theme_base, - frame_theme, - title_theme, - ticks_theme, - text_theme, - grid_theme - ) - theme_modifications <- Reduce("+", theme_modifications) - return(theme_modifications) -} - -#' Modify a ggplot2 plot with custom theme, title, axis labels, and ggsignif -#' comparisons -#' -#' This function modifies a ggplot2 plot by adding a custom theme, plot title, -#' axis labels, and ggsignif comparisons. The function returns a list of ggplot2 -#' objects that can be used to modify the plot. -#' -#' @inheritParams theme_cat -#' @param plot_title A character string representing the title of the plot -#' @param x_axis_title A character string representing the title of the x-axis -#' @param y_axis_title A character string representing the title of the y-axis -#' @param x_text_angle An integer representing the angle of the x-axis text -#' labels -#' @param y_text_angle An integer representing the angle of the y-axis text -#' labels -#' @param legend_title A character string representing the title of the plot -#' legend -#' @param comparisons A list of comparison groups for ggsignif -#' @param test A character string representing the statistical test to use for -#' ggsignif -#' @param step_increase A numeric value representing the step increase for -#' ggsignif -#' @param map_signif_level A logical value representing whether to map the -#' significance level for ggsignif -#' @return A list of ggplot2 objects that can be used to modify the plot -#' @export -#' @examples -#' library(ggplot2) -#' library(catplot) -#' -#' # Create a plot -#' data(mtcars) -#' p <- -#' ggplot(mtcars, aes( -#' x = factor(cyl), -#' y = mpg, -#' fill = factor(gear) -#' )) + -#' geom_boxplot() + -#' labs( -#' title = "Miles per Gallon by Cylinder and Gear", -#' x = "Cylinder", -#' y = "Miles per Gallon", -#' fill = "Gear" -#' ) -#' -#' # Modify the plot with custom theme, title, and axis labels -#' p + cat_modify_plot( -#' plot_title = "Miles per Gallon by Cylinder and Gear", -#' x_axis_title = "Cylinder", -#' y_axis_title = "Miles per Gallon" -#' ) -#' -#' # Modify the plot with custom ggsignif comparisons -#' p + cat_modify_plot( -#' comparisons = list(c("4", "6"), c("4", "8"), c("6", "8")), -#' test = "t.test" -#' ) -#' -#' # Modify the plot with custom theme and ggsignif comparisons -#' p + cat_modify_plot( -#' font_size = 12, -#' linewidth = 1, -#' comparisons = list(c("4", "6"), c("4", "8"), c("6", "8")), -#' test = "t.test" -#' ) -cat_modify_plot <- function(plot_title = waiver(), - x_axis_title = waiver(), - y_axis_title = waiver(), - x_text_angle = NULL, - y_text_angle = NULL, - legend_title = waiver(), - font_size = 8, - linewidth = 0.5, - aspect_ratio = NULL, - frame = "closed", - show_panel_grid = "both_not", - show_title = "both", - show_text = "both", - text_italic = "both_not", - show_ticks = "both", - ticks_length = 4, - legend_position = "right", - legend_direction = NULL, - comparisons = NULL, - test = "wilcox.test", - step_increase = 0.1, - map_signif_level = TRUE) { - list( - # Plot title and axis title - labs(title = plot_title, x = x_axis_title, y = y_axis_title), - # Plot legend - guides( - x = guide_axis(angle = x_text_angle), - y = guide_axis(angle = y_text_angle), - fill = guide_legend(title = legend_title) - ), - # Plot ggsignif - if (!is.null(comparisons)) { - geom_signif( - comparisons = comparisons, - size = 0.5 * 0.5 * 0.93, - textsize = 0.36 * 6, - test = test, - step_increase = step_increase, - tip_length = 0.02, - map_signif_level = map_signif_level - ) - }, - theme_cat( - font_size = font_size, - linewidth = linewidth, - aspect_ratio = aspect_ratio, - frame = frame, - show_panel_grid = show_panel_grid, - show_title = show_title, - show_text = show_text, - text_italic = text_italic, - show_ticks = show_ticks, - ticks_length = ticks_length, - legend_position = legend_position, - legend_direction = legend_direction - ) - ) -} +#' Custom theme for catplot +#' +#' @param font_size Numeric, font size (default: 8) +#' @param linewidth Numeric, line width (default: 0.5) +#' @param aspect_ratio Numeric, aspect ratio of the plot (default: NULL) +#' @param frame Character, frame type, "closed" or "open" (default: "closed") +#' @param show_panel_grid Character, panel grid visibility (default: "both_not") +#' @param show_title Character, axis title visibility (default: "both") +#' @param show_text Character, axis text visibility (default: "both") +#' @param text_italic Character, axis text italicization (default: "both_not") +#' @param show_ticks Character, axis tick visibility (default: "both") +#' @param ticks_length Numeric, tick length (default: 4) +#' @param legend_position Character, position of legend (default: "right") +#' @param legend_direction Character, direction of legend (default: NULL) +#' @return A ggplot2 theme +#' @export +#' @examples +#' library(catplot) +#' library(ggplot2) +#' +#' p <- ggplot(mpg, aes(x = manufacturer, y = hwy)) + +#' geom_point() +#' p + theme_cat() +theme_cat <- function(font_size = 8, + linewidth = 0.5, + aspect_ratio = NULL, + frame = "closed", + show_panel_grid = "both_not", + show_title = "both", + show_text = "both", + text_italic = "both_not", + show_ticks = "both", + ticks_length = 4, + legend_position = "right", + legend_direction = NULL) { + linewidth <- linewidth * 0.5 / 1.07 + + arg_list <- list( + frame = c("closed", "open", "none"), + show_panel_grid = c("x", "y", "both", "both_not"), + show_title = c("x", "y", "both", "both_not"), + show_ticks = c("x", "y", "both", "both_not"), + show_text = c("x", "y", "both", "both_not"), + text_italic = c("x", "y", "both", "both_not"), + legend_position = c("right", "left", "bottom", "top") + ) + + frame <- match.arg(frame, arg_list$frame) + show_panel_grid <- + match.arg(show_panel_grid, arg_list$show_panel_grid) + show_title <- match.arg(show_title, arg_list$show_title) + show_ticks <- match.arg(show_ticks, arg_list$show_ticks) + show_text <- match.arg(show_text, arg_list$show_text) + text_italic <- match.arg(text_italic, arg_list$text_italic) + legend_position <- + match.arg(legend_position, arg_list$legend_position) + + legend_margin <- switch(legend_position, + "right" = margin(l = -8), + "top" = margin(b = -8), + "bottom" = margin(t = -8), + "left" = margin(r = -8) + ) + legend_direction <- legend_direction %||% "default" + legend_direction <- switch(legend_position, + "h" = "horizontal", + "horizontal" = "horizontal", + "v" = "vertical", + "vertical" = "vertical", + "default" = "NULL" + ) + + tick_element <- + element_line( + linewidth = linewidth, + lineend = "square", + colour = "black" + ) + title_element <- + element_text( + size = font_size, + face = "plain", + colour = "black" + ) + grid_element <- element_line( + colour = "lightgrey", + linewidth = linewidth, + lineend = "square" + ) + theme_base <- theme( + aspect.ratio = aspect_ratio, + axis.ticks.length = unit(ticks_length, "pt"), + panel.background = element_blank(), + panel.grid = element_blank(), + legend.background = element_blank(), + legend.title = title_element, + legend.text = title_element, + legend.key = element_blank(), + legend.key.height = unit(font_size, "pt"), + legend.key.width = unit(font_size, "pt"), + legend.position = legend_position, + legend.margin = legend_margin, + legend.direction = legend_direction, + plot.background = element_blank(), + plot.title = element_text( + size = font_size, + hjust = 0.5, + vjust = -1, + face = "plain", + colour = "black" + ), + plot.subtitle = element_text( + hjust = 0.5, + size = font_size, + face = "plain", + colour = "black" + ), + strip.background = element_blank(), + strip.text = title_element + ) + + # Frame + frame_theme <- switch(frame, + "open" = theme( + axis.line = tick_element, + panel.border = element_blank() + ), + "closed" = theme( + axis.line = element_blank(), + panel.border = element_rect( + linewidth = linewidth, + fill = NA, + colour = "black" + ) + ) + ) + # Title + title_theme <- switch(show_title, + "x" = theme( + axis.title.x = title_element, + axis.title.y = element_blank() + ), + "y" = theme( + axis.title.y = title_element, + axis.title.x = element_blank() + ), + "both" = theme(axis.title = title_element), + "both_not" = theme(axis.title = element_blank()) + ) + # Ticks + ticks_theme <- switch(show_ticks, + "x" = theme( + axis.ticks.x = tick_element, + axis.ticks.y = element_blank() + ), + "y" = theme( + axis.ticks.y = tick_element, + axis.ticks.x = element_blank() + ), + "both" = theme(axis.ticks = tick_element), + "both_not" = theme(axis.ticks = element_blank()) + ) + # Fontface + x_fontface <- ifelse(text_italic == "x" | + text_italic == "both", "italic", "plain") + y_fontface <- ifelse(text_italic == "y" | + text_italic == "both", "italic", "plain") + text_theme <- switch(show_text, + "x" = theme( + axis.text.x = element_text( + size = font_size, + face = x_fontface, + colour = "black" + ), + axis.text.y = element_blank() + ), + "y" = theme( + axis.text.y = element_text( + size = font_size, + face = y_fontface, + colour = "black" + ), + axis.text.x = element_blank() + ), + "both" = theme( + axis.text.x = element_text( + size = font_size, + face = x_fontface, + colour = "black" + ), + axis.text.y = element_text( + size = font_size, + face = x_fontface, + colour = "black" + ) + ), + "both_not" = theme(axis.text = element_blank()) + ) + + # Panel grid + grid_theme <- switch(show_panel_grid, + "x" = theme( + panel.grid.major.x = grid_element, + panel.grid.major.y = element_blank() + ), + "y" = theme( + panel.grid.major.y = grid_element, + panel.grid.major.x = element_blank() + ), + "both" = theme(panel.grid = grid_element), + "both_not" = theme(panel.grid = element_blank()) + ) + theme_modifications <- list( + theme_base, + title_theme, + ticks_theme, + text_theme, + grid_theme, + frame_theme + ) + theme_modifications <- Reduce("+", theme_modifications) + return(theme_modifications) +} + +#' Modify a ggplot2 plot with custom theme, title, axis labels, and ggsignif +#' comparisons +#' +#' This function modifies a ggplot2 plot by adding a custom theme, plot title, +#' axis labels, and ggsignif comparisons. The function returns a list of ggplot2 +#' objects that can be used to modify the plot. +#' +#' @inheritParams theme_cat +#' @param plot_title A character string representing the title of the plot +#' @param x_axis_title A character string representing the title of the x-axis +#' @param y_axis_title A character string representing the title of the y-axis +#' @param x_text_angle An integer representing the angle of the x-axis text +#' labels +#' @param y_text_angle An integer representing the angle of the y-axis text +#' labels +#' @param legend_title A character string representing the title of the plot +#' legend +#' @param comparisons A list of comparison groups for ggsignif +#' @param test A character string representing the statistical test to use for +#' ggsignif +#' @param step_increase A numeric value representing the step increase for +#' ggsignif +#' @param map_signif_level A logical value representing whether to map the +#' significance level for ggsignif +#' @return A list of ggplot2 objects that can be used to modify the plot +#' @export +#' @examples +#' library(ggplot2) +#' library(catplot) +#' +#' # Create a plot +#' data(mtcars) +#' p <- +#' ggplot(mtcars, aes( +#' x = factor(cyl), +#' y = mpg, +#' fill = factor(gear) +#' )) + +#' geom_boxplot() + +#' labs( +#' title = "Miles per Gallon by Cylinder and Gear", +#' x = "Cylinder", +#' y = "Miles per Gallon", +#' fill = "Gear" +#' ) +#' +#' # Modify the plot with custom theme, title, and axis labels +#' p + cat_modify_plot( +#' plot_title = "Miles per Gallon by Cylinder and Gear", +#' x_axis_title = "Cylinder", +#' y_axis_title = "Miles per Gallon" +#' ) +#' +#' # Modify the plot with custom ggsignif comparisons +#' p + cat_modify_plot( +#' comparisons = list(c("4", "6"), c("4", "8"), c("6", "8")), +#' test = "t.test" +#' ) +#' +#' # Modify the plot with custom theme and ggsignif comparisons +#' p + cat_modify_plot( +#' font_size = 12, +#' linewidth = 1, +#' comparisons = list(c("4", "6"), c("4", "8"), c("6", "8")), +#' test = "t.test" +#' ) +cat_modify_plot <- function(plot_title = waiver(), + x_axis_title = waiver(), + y_axis_title = waiver(), + x_text_angle = NULL, + y_text_angle = NULL, + legend_title = waiver(), + font_size = 8, + linewidth = 0.5, + aspect_ratio = NULL, + frame = "closed", + show_panel_grid = "both_not", + show_title = "both", + show_text = "both", + text_italic = "both_not", + show_ticks = "both", + ticks_length = 4, + legend_position = "right", + legend_direction = NULL, + comparisons = NULL, + test = "wilcox.test", + step_increase = 0.1, + map_signif_level = TRUE) { + list( + # Plot title and axis title + labs(title = plot_title, x = x_axis_title, y = y_axis_title), + # Plot legend + guides( + x = guide_axis(angle = x_text_angle), + y = guide_axis(angle = y_text_angle), + fill = guide_legend(title = legend_title) + ), + # Plot ggsignif + if (!is.null(comparisons)) { + geom_signif( + comparisons = comparisons, + size = 0.5 * 0.5 * 0.93, + textsize = 0.36 * 6, + test = test, + step_increase = step_increase, + tip_length = 0.02, + map_signif_level = map_signif_level + ) + }, + theme_cat( + font_size = font_size, + linewidth = linewidth, + aspect_ratio = aspect_ratio, + frame = frame, + show_panel_grid = show_panel_grid, + show_title = show_title, + show_text = show_text, + text_italic = text_italic, + show_ticks = show_ticks, + ticks_length = ticks_length, + legend_position = legend_position, + legend_direction = legend_direction + ) + ) +} diff --git a/R/color.R b/R/color.R index a9b38e7..f230cb5 100644 --- a/R/color.R +++ b/R/color.R @@ -143,9 +143,11 @@ okabeito <- c( #' specified. #' @return A vector of colors from the selected ggsci palette #' @examples +#' \dontrun{ #' palette <- catplot:::.get_ggsci_palette("npg", 6) #' #' barplot(1:6, col = palette) +#' } .get_ggsci_palette <- function( palette = c( "npg", diff --git a/R/scrna-seq.R b/R/scrna-seq.R index 1a0b6b8..7c3215e 100644 --- a/R/scrna-seq.R +++ b/R/scrna-seq.R @@ -3,81 +3,139 @@ #' This function creates a dimensionality reduction plot for categorical data using Seurat and ggplot2. It allows for the selection of the reduction method, grouping, and splitting variables, as well as the visualization of labels, rasterization, and color palette. The cat_dimplot() function is intended to be used as a wrapper around Seurat's DimPlot() function. #' #' @param object A Seurat object containing categorical data. +#' @param dims The dimensions to plot. Default is c(1, 2). +#' @param cells The cells to plot. Default is NULL. +#' @param cols The columns to plot. Default is NULL. +#' @param pt_size The size of the points on the plot. Default is 1. #' @param reduction The dimensionality reduction method. Default is NULL. #' @param group_by The variable to group data by. Default is NULL. #' @param split_by The variable to split data by. Default is NULL. +#' @param shape_by The variable to shape data by. Default is NULL. +#' @param order The order to plot the data in. Default is NULL. +#' @param shuffle Logical value indicating whether to shuffle the data before plotting. Default is FALSE. +#' @param seed The random seed to use for shuffling the data. Default is 1. #' @param label Logical value indicating whether to show labels on the plot. Default is FALSE. +#' @param label_size The size of the labels on the plot. Default is 8 * 0.36. +#' @param label_color The color of the labels on the plot. Default is "black". +#' @param label_box Logical value indicating whether to show a box around the labels on the plot. Default is FALSE. +#' @param repel Logical value indicating whether to use point repulsion to avoid overlapping labels. Default is TRUE. +#' @param cells_highlight The cells to highlight on the plot. Default is NULL. +#' @param cols_highlight The columns to highlight on the plot. Default is NULL. +#' @param sizes_highlight The sizes to highlight on the plot. Default is NULL. +#' @param na_value The value to use for missing data. Default is "grey50". +#' @param ncol The number of columns to use for the plot. Default is NULL. +#' @param combine Logical value indicating whether to combine the plots into a single plot. Default is TRUE. #' @param raster Logical value indicating whether to use rasterization for improved performance. Default is TRUE. +#' @param raster_dpi The DPI to use for rasterization. Default is c(512, 512). #' @param show_legend Logical value indicating whether to show the legend on the plot. Default is TRUE. #' @param show_axis Logical value indicating whether to show the axis on the plot. Default is TRUE. #' @param show_border Logical value indicating whether to show the panel and axis borders on the plot. Default is TRUE. #' @param title The title for the plot. Default is NULL. -#' @param pt_size The size of the points on the plot. Default is 1. -#' @param repel Logical value indicating whether to use point repulsion to avoid overlapping labels. Default is TRUE. #' @param palette The color palette to use for the plot. Default is "Paired". #' @param ... Additional parameters to be passed to the DimPlot() function in Seurat. #' @return A ggplot2 object containing the dimensionality reduction plot. #' #' @examples +#' \dontrun{ #' library(Seurat) #' library(ggplot2) #' data("pbmc_small") #' pbmc_small <- CreateSeuratObject(counts = pbmc_small) #' Idents(pbmc_small) <- pbmc_small$percent.mt > 10 #' cat_dimplot(object = pbmc_small, reduction = "tsne", group_by = "ident", palette = "Set1") +#' } #' #' @importFrom ggplot2 theme element_blank element_text margin scale_color_manual labs #' @importFrom grDevices colorRampPalette -#' +#' @importFrom RColorBrewer brewer.pal #' @export -cat_dimplot <- function(object, - reduction = NULL, - group_by = NULL, - split_by = NULL, - label = FALSE, - raster = TRUE, - show_legend = TRUE, - show_axis = TRUE, - show_border = TRUE, - title = NULL, - pt_size = 1, - repel = TRUE, - palette = "Paired", - ...) { +cat_dimplot <- function( + object, + dims = c(1, 2), + cells = NULL, + cols = NULL, + pt_size = 1, + reduction = NULL, + group_by = NULL, + split_by = NULL, + shape_by = NULL, + order = NULL, + shuffle = FALSE, + seed = 1, + label = FALSE, + label_size = 8 * 0.36, + label_color = "black", + label_box = FALSE, + repel = TRUE, + cells_highlight = NULL, + cols_highlight = "#DE2D26", + sizes_highlight = 1, + na_value = "grey50", + ncol = NULL, + combine = TRUE, + raster = TRUE, + raster_dpi = c(512, 512), + show_legend = TRUE, + show_axis = TRUE, + show_border = TRUE, + title = NULL, + palette = "Paired", + ...) { p <- Seurat::DimPlot( object = object, + dims = dims, + cells = cells, + cols = cols, + pt.size = pt_size, reduction = reduction, - label = label, - repel = repel, group.by = group_by, split.by = split_by, - label.size = 8 * 0.36, - pt.size = pt_size, + shape.by = shape_by, + order = NULL, + shuffle = FALSE, + seed = 1, + label = FALSE, + label.size = label_size, + label.color = label_color, + label.box = label_box, + repel = repel, + cells.highlight = cells_highlight, + cols.highlight = cols_highlight, + sizes.highlight = sizes_highlight, + na.value = na_value, + ncol = ncol, + combine = combine, raster = raster, - seed = 717, + raster.dpi = raster_dpi, ... ) if (!show_legend) { p <- p + Seurat::NoLegend() } - reduction <- reduction %||% DefaultDimReduc(object = object) + reduction <- reduction %||% SeuratObject::DefaultDimReduc(object = object) object[["ident"]] <- Seurat::Idents(object = object) group_by <- group_by %||% "ident" n <- length(table(object[[group_by]])) + x <- ifelse(reduction == "tsne", "tSNE 1", + paste0(stringr::str_to_upper(reduction), " 1") + ) + y <- ifelse(reduction == "tsne", "tSNE 2", + paste0(stringr::str_to_upper(reduction), " 2") + ) p <- p + theme_cat(aspect_ratio = 1) + theme(legend.margin = margin(l = -8)) + labs( - x = paste0(stringr::str_to_upper(reduction), " 1"), - y = paste0(stringr::str_to_upper(reduction), " 2"), + x = x, + y = y, title = title ) if (!is.null(palette)) { if (n > 12) { - values <- colorRampPalette(RColorBrewer::brewer.pal(12, palette))(n) + values <- colorRampPalette(brewer.pal(12, palette))(n) } else { - values <- RColorBrewer::brewer.pal(12, palette) + values <- brewer.pal(12, palette) } p <- p + scale_color_manual(values = values) } @@ -114,9 +172,12 @@ cat_dimplot <- function(object, #' #' @importFrom ggplot2 expansion guide_axis scale_fill_manual #' @importFrom grDevices colorRampPalette +#' @importFrom RColorBrewer brewer.pal #' #' @examples +#' \dontrun{ #' cat_vlnplot(object = mySeuratObject, features = c("CD3D", "CD8A", "CD4")) +#' } #' #' @export cat_vlnplot <- function(object, @@ -143,9 +204,9 @@ cat_vlnplot <- function(object, group_by <- group_by %||% "ident" n <- length(table(object[[group_by]])) if (n > 12) { - values <- colorRampPalette(RColorBrewer::brewer.pal(12, "Paired"))(n) + values <- colorRampPalette(brewer.pal(12, "Paired"))(n) } else { - values <- RColorBrewer::brewer.pal(12, "Paired") + values <- brewer.pal(12, "Paired") } p <- p + theme_cat(aspect_ratio = aspect_ratio) + theme( @@ -163,34 +224,63 @@ cat_vlnplot <- function(object, #' This function plots a dot plot with categorical groups using the DotPlot function from the Seurat package. #' #' @param x A Seurat object containing the data to plot. +#' @param assay The assay to plot. Defaults to the default assay. #' @param features A character vector of feature names to plot. -#' @param dot_scale The size of the dots to plot. #' @param col_min The minimum value for the color scale. Defaults to -2.5. #' @param col_max The maximum value for the color scale. Defaults to 2.5. +#' @param dot_min The minimum value for the dot size scale. Defaults to 0. +#' @param dot_scale The size of the dots to plot. +#' @param idents A character vector of identities to plot. Defaults to all identities. +#' @param group_by A character vector specifying the grouping variable. Defaults to "ident". +#' @param split_by A character vector specifying the splitting variable. +#' @param cluster_idents Whether to cluster the identities or not. Defaults to FALSE. +#' @param scale Whether to scale the dot size or not. Defaults to TRUE. +#' @param scale_by The variable to scale the dot size by. Defaults to "radius". +#' @param scale_min The minimum value for the dot size scale. Defaults to NA. +#' @param scale_max The maximum value for the dot size scale. Defaults to NA. #' #' @return A ggplot2 object. #' #' @importFrom ggplot2 coord_fixed guides theme element_text margin scale_color_distiller scale_y_discrete guide_axis guide_legend guide_colorbar #' #' @examples +#' \dontrun{ #' cat_dotplot(x = mySeuratObject, features = c("CD3D", "CD8A", "CD4")) +#' } #' #' @export cat_dotplot <- function(x, + assay = NULL, features, - dot_scale = 4, col_min = -2.5, col_max = 2.5, - assay = NULL) { + dot_min = 0, + dot_scale = 4, + idents = NULL, + group_by = NULL, + split_by = NULL, + cluster_idents = FALSE, + scale = TRUE, + scale_by = "radius", + scale_min = NA, + scale_max = NA) { p <- Seurat::DotPlot( - x, - dot.scale = dot_scale, + object = x, + assay = assay, features = features, col.min = col_min, col.max = col_max, - assay = assay, - scale = TRUE + dot.min = dot_min, + dot.scale = dot_scale, + idents = idents, + group.by = group_by, + split.by = split_by, + cluster.idents = cluster_idents, + scale = TRUE, + scale.by = scale_by, + scale.min = scale_min, + scale.max = scale_max ) + coord_fixed() + guides( x = guide_axis(angle = 90), @@ -248,7 +338,9 @@ cat_dotplot <- function(x, #' @importFrom ggplot2 theme element_blank element_text margin #' #' @examples +#' \dontrun{ #' cat_featureplot(x = mySeuratObject, features = c("CD3D", "CD8A", "CD4"), reduction = "UMAP") +#' } #' #' @export cat_featureplot <- @@ -287,7 +379,7 @@ cat_featureplot <- if (!show_legend) { p <- p + Seurat::NoLegend() } - reduction <- reduction %||% DefaultDimReduc(object = object) + reduction <- reduction %||% SeuratObject::DefaultDimReduc(object = object) object[["ident"]] <- Seurat::Idents(object = object) title <- title %||% features p <- p + diff --git a/R/utilities.R b/R/utilities.R index bf96bf6..8b8aa3e 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -1,212 +1,284 @@ -#' @importFrom ggplot2 element_blank element_line element_rect element_text -#' margin unit aes as_label enquo geom_boxplot ggplot guide_axis guide_legend -#' guides labs scale_x_continuous scale_y_continuous quo_name geom_jitter -#' position_jitter scale_fill_manual scale_color_manual position_jitterdodge -#' theme expansion waiver layer GeomPoint -#' GeomBoxplot GeomDensity GeomRug GeomViolin StatSummary ggsave last_plot geom_hline mean_se position_dodge2 -#' @importFrom ggsignif geom_signif -#' @importFrom rlang %||% list2 arg_match0 -#' @importFrom dplyr group_by summarise arrange desc pull case_when mutate -#' @importFrom grDevices col2rgb -NULL - -#' @importFrom magrittr %>% -##' @export -magrittr::`%>%` - -#' Check if an object is a character vector or a factor -#' -#' @description Check if a given object is a character vector or a factor. -#' This function is used internally by other functions. -#' -#' @param x An object to check -#' -#' @return A logical value indicating whether the object is a character vector -#' or a factor -#' -#' @examples -#' x <- c("a", "b", "c") -#' y <- factor(x) -#' catplot:::.is_char_or_factor(x) -#' catplot:::.is_char_or_factor(y) -.is_char_or_factor <- function(x) { - class(x) %in% c("character", "factor") -} - - -#' Check if a string is a valid color value -#' -#' This function takes a string and attempts to parse it as a color value using -#' the \code{col2rgb()} function. If the color value is valid, \code{TRUE} is -#' returned. Otherwise, \code{FALSE} is returned. -#' -#' @param str A character string specifying the color value to be checked. -#' @return A logical value indicating whether the input string is a valid color -#' value. -#' @examples -#' catplot:::.is_color("red") # returns TRUE -#' catplot:::.is_color("#FFA500") # returns TRUE -#' catplot:::.is_color("fdafd") # returns FALSE -.is_color <- function(str) { - rgb_val <- tryCatch( - expr = col2rgb(str), - error = function(e) { - NULL - } - ) - is_valid_color <- !is.null(rgb_val) - return(is_valid_color) -} - -#' Construct aesthetic mappings or parameters for a plot -#' -#' Create a mapping and parameters for a plot with a given \code{color} and -#' \code{fill}. The function checks if the color and fill arguments are valid -#' colors and, if so, adds them to the list of \code{params}. Otherwise, they -#' are added to the \code{mapping_props} list. Additional parameter can be -#' provided to customize the mapping and parameters. -#' -#' @param color A character string specifying the color to be used in the plot -#' @param fill A character string specifying the fill color to be used in the -#' plot -#' -#' @return A list containing the mapping and parameters for the plot -#' -#' @export -create_ap <- function(color = NULL, - fill = NULL, - shape = NULL, - alpha = NULL, - size = NULL) { - color <- enquo(color) - color_quo_name <- quo_name(color) - fill <- enquo(fill) - fill_quo_name <- quo_name(fill) - shape <- enquo(shape) - shape_quo_name <- quo_name(shape) - alpha <- enquo(alpha) - alpha_quo_name <- quo_name(alpha) - size <- enquo(size) - size_quo_name <- quo_name(size) - - mapping_props <- list() - params <- list2() - - if (color_quo_name != "NULL") { - if (!is.na(color_quo_name) && - .is_color(color_quo_name)) { - params$color <- color_quo_name - } else if (is.na(color_quo_name)) { - params$color <- NA - } else if (!.is_color(color_quo_name)) { - mapping_props$color <- color - } - } - if (fill_quo_name != "NULL") { - if (!is.na(fill_quo_name) && - .is_color(fill_quo_name)) { - params$fill <- fill_quo_name - } else if (is.na(fill_quo_name)) { - params$fill <- NA - } else { - mapping_props$fill <- fill - } - } - if (shape_quo_name != "NULL") { - print(shape_quo_name) - is_numeric <- grepl("^[0-9]+([.][0-9]+)?$", shape_quo_name) - print(is_numeric) - if (is_numeric) { - params$shape <- as.numeric(shape_quo_name) - } else { - mapping_props$shape <- shape - } - } - if (alpha_quo_name != "NULL") { - is_numeric <- grepl("^[0-9]+([.][0-9]+)?$", alpha_quo_name) - if (is_numeric) { - params$alpha <- as.numeric(alpha_quo_name) - } else { - mapping_props$alpha <- alpha - } - } - if (size_quo_name != "NULL") { - is_numeric <- grepl("^[0-9]+([.][0-9]+)?$", size_quo_name) - if (is_numeric) { - params$size <- as.numeric(size_quo_name) - } else { - mapping_props$size <- size - } - } - - mapping <- aes(!!!mapping_props) - return(list(mapping = mapping, params = params)) -} - - -#' Save a ggplot object to a file -#' -#' This function saves a ggplot object to a file. It provides an option to save -#' the raw data used to create the plot as a separate CSV file. -#' -#' @param filename A character string specifying the name of the file to save -#' the plot to -#' @param plot A ggplot object to be saved -#' @param device A character string specifying the graphics device to use -#' @param path A character string specifying the directory to save the file to -#' @param scale A numeric value specifying the scaling factor for the plot -#' @param width A numeric value specifying the width of the plot -#' @param height A numeric value specifying the height of the plot -#' @param units A character string specifying the units of the width and height -#' arguments -#' @param dpi A numeric value specifying the resolution of the plot -#' @param limitsize A logical value specifying whether to limit the size of the -#' plot to the device size -#' @param bg A character string specifying the background color of the plot -#' @param raw_data A logical value specifying whether to save the raw data used -#' to create the plot as a separate CSV file -#' @param ... Additional arguments to be passed to ggsave -#' -#' @export -cat_save <- function(filename, - plot = last_plot(), - device = NULL, - path = NULL, - scale = 1, - width = NA, - height = NA, - units = c("in", "cm", "mm", "px"), - dpi = 300, - limitsize = TRUE, - bg = NULL, - raw_data = FALSE, - ...) { - if (raw_data) { - if (is.null(path)) { - path <- dirname(filename) - } - basename <- basename(filename) - prefix <- tools::file_path_sans_ext(basename) - data <- plot$data - data_file <- file.path(path, paste0(prefix, ".data.csv")) - write_csv(x = data, - file = data_file) - plot_file <- file.path(path, paste0(prefix, ".rds")) - saveRDS(object = plot, - file = plot_file) - } - ggsave( - filename = filename, - plot = plot, - device = device, - path = path, - scale = scale, - width = width, - height = height, - units = units, - dpi = dpi, - limitsize = limitsize, - bg = bg, - ... - ) -} +#' @importFrom ggplot2 element_blank element_line element_rect element_text +#' margin unit aes as_label enquo geom_boxplot ggplot guide_axis guide_legend +#' guides labs scale_x_continuous scale_y_continuous quo_name geom_jitter +#' position_jitter scale_fill_manual scale_color_manual position_jitterdodge +#' theme expansion waiver layer GeomPoint +#' GeomBoxplot GeomDensity GeomRug GeomViolin StatSummary ggsave last_plot geom_hline mean_se position_dodge2 +#' @importFrom ggsignif geom_signif +#' @importFrom rlang %||% list2 arg_match0 +#' @importFrom dplyr group_by summarise arrange desc pull case_when mutate +#' @importFrom grDevices col2rgb +NULL + +#' @importFrom magrittr %>% +##' @export +magrittr::`%>%` + +#' Check if an object is a character vector or a factor +#' +#' @description Check if a given object is a character vector or a factor. +#' This function is used internally by other functions. +#' +#' @param x An object to check +#' +#' @return A logical value indicating whether the object is a character vector +#' or a factor +#' +#' @examples +#' x <- c("a", "b", "c") +#' y <- factor(x) +#' catplot:::.is_char_or_factor(x) +#' catplot:::.is_char_or_factor(y) +.is_char_or_factor <- function(x) { + class(x) %in% c("character", "factor") +} + + +#' Check if a string is a valid color value +#' +#' This function takes a string and attempts to parse it as a color value using +#' the \code{col2rgb()} function. If the color value is valid, \code{TRUE} is +#' returned. Otherwise, \code{FALSE} is returned. +#' +#' @param str A character string specifying the color value to be checked. +#' @return A logical value indicating whether the input string is a valid color +#' value. +#' @examples +#' catplot:::.is_color("red") # returns TRUE +#' catplot:::.is_color("#FFA500") # returns TRUE +#' catplot:::.is_color("fdafd") # returns FALSE +.is_color <- function(str) { + rgb_val <- tryCatch( + expr = col2rgb(str), + error = function(e) { + NULL + } + ) + is_valid_color <- !is.null(rgb_val) + return(is_valid_color) +} + +#' Construct aesthetic mappings or parameters for a plot +#' +#' Create a mapping and parameters for a plot with a given \code{color} and +#' \code{fill}. The function checks if the color and fill arguments are valid +#' colors and, if so, adds them to the list of \code{params}. Otherwise, they +#' are added to the \code{mapping_props} list. Additional parameter can be +#' provided to customize the mapping and parameters. +#' +#' @param color A character string specifying the color to be used in the plot +#' @param fill A character string specifying the fill color to be used in the plot +#' @param shape A character string specifying the shape to be used in the plot +#' @param alpha A character string specifying the alpha value to be used in the plot +#' @param size A character string specifying the size to be used in the plot +#' @return A list containing the mapping and parameters for the plot +#' +#' @export +create_ap <- function(color = NULL, + fill = NULL, + shape = NULL, + alpha = NULL, + size = NULL) { + color <- enquo(color) + color_quo_name <- quo_name(color) + fill <- enquo(fill) + fill_quo_name <- quo_name(fill) + shape <- enquo(shape) + shape_quo_name <- quo_name(shape) + alpha <- enquo(alpha) + alpha_quo_name <- quo_name(alpha) + size <- enquo(size) + size_quo_name <- quo_name(size) + + mapping_props <- list() + params <- list2() + + if (color_quo_name != "NULL") { + if (!is.na(color_quo_name) && + .is_color(color_quo_name)) { + params$color <- color_quo_name + } else if (is.na(color_quo_name)) { + params$color <- NA + } else if (!.is_color(color_quo_name)) { + mapping_props$color <- color + } + } + if (fill_quo_name != "NULL") { + if (!is.na(fill_quo_name) && + .is_color(fill_quo_name)) { + params$fill <- fill_quo_name + } else if (is.na(fill_quo_name)) { + params$fill <- NA + } else { + mapping_props$fill <- fill + } + } + if (shape_quo_name != "NULL") { + print(shape_quo_name) + is_numeric <- grepl("^[0-9]+([.][0-9]+)?$", shape_quo_name) + print(is_numeric) + if (is_numeric) { + params$shape <- as.numeric(shape_quo_name) + } else { + mapping_props$shape <- shape + } + } + if (alpha_quo_name != "NULL") { + is_numeric <- grepl("^[0-9]+([.][0-9]+)?$", alpha_quo_name) + if (is_numeric) { + params$alpha <- as.numeric(alpha_quo_name) + } else { + mapping_props$alpha <- alpha + } + } + if (size_quo_name != "NULL") { + is_numeric <- grepl("^[0-9]+([.][0-9]+)?$", size_quo_name) + if (is_numeric) { + params$size <- as.numeric(size_quo_name) + } else { + mapping_props$size <- size + } + } + + mapping <- aes(!!!mapping_props) + return(list(mapping = mapping, params = params)) +} + + +#' Save a ggplot object to a file +#' +#' This function saves a ggplot object to a file. It provides an option to save +#' the raw data used to create the plot as a separate CSV file. +#' +#' @param filename A character string specifying the name of the file to save +#' the plot to +#' @param plot A ggplot object to be saved +#' @param device A character string specifying the graphics device to use +#' @param path A character string specifying the directory to save the file to +#' @param scale A numeric value specifying the scaling factor for the plot +#' @param width A numeric value specifying the width of the plot +#' @param height A numeric value specifying the height of the plot +#' @param units A character string specifying the units of the width and height +#' arguments +#' @param dpi A numeric value specifying the resolution of the plot +#' @param limitsize A logical value specifying whether to limit the size of the +#' plot to the device size +#' @param bg A character string specifying the background color of the plot +#' @param source_data A logical value specifying whether to save the raw data used +#' to create the plot as a separate CSV file +#' @param ... Additional arguments to be passed to ggsave +#' @importFrom utils write.csv +#' @export +cat_save <- function(filename, + plot = last_plot(), + device = NULL, + path = NULL, + scale = 1, + width = NA, + height = NA, + units = c("in", "cm", "mm", "px"), + dpi = 300, + limitsize = TRUE, + bg = NULL, + source_data = FALSE, + ...) { + if (is.null(path)) { + # cat_save(filename="~/figure1a.pdf", path = NULL) + path <- cat_set_path(dirname(filename)) + filename <- basename(filename) + } else { + # cat_save(filename="figure1a.pdf", path = "~/") + path <- cat_set_path(path) + } + + if (source_data) { + prefix <- tools::file_path_sans_ext(filename) + data <- plot$data + data_file <- file.path(path, paste0(prefix, ".data.csv")) + write.csv( + x = data, + file = data_file + ) + plot_file <- file.path(path, paste0(prefix, ".plot.rds")) + saveRDS( + object = plot, + file = plot_file + ) + } + ggsave( + filename = filename, + plot = plot, + device = device, + path = path, + scale = scale, + width = width, + height = height, + units = units, + dpi = dpi, + limitsize = limitsize, + bg = bg, + ... + ) +} + +#' Create a directory if it does not exist and return the path +#' +#' This function takes a path as input and creates a directory if it does not +#' exist. It then returns the path. +#' +#' @param path A character string specifying the path to be created. +#' +#' @return A character string specifying the path. +#' +#' @examples +#' cat_set_path("~/my_folder") +#' +#' @export +cat_set_path <- function(path) { + if (!file.exists(path)) { + dir.create(path, recursive = TRUE) + } + return(path) +} + +create_new_project <- function(path) { + project_name <- basename(path) + # code: scripts, functions, figures + cat_set_path(file.path(path, "code", "scripts")) + cat_set_path(file.path(path, "code", "functions")) + cat_set_path(file.path(path, "code", "figures")) + # data: raw, intermediate, processed + cat_set_path(file.path(path, "data", "raw")) + cat_set_path(file.path(path, "data", "intermediate")) + cat_set_path(file.path(path, "data", "processed")) + # results: figures, tables, reports + cat_set_path(file.path(path, "results", "figures")) + cat_set_path(file.path(path, "results", "tables")) + cat_set_path(file.path(path, "results", "reports")) + # logs + cat_set_path(file.path(path, "logs")) + # Rproj + writeLines( + sprintf( + "Version: 1.0\nRestoreWorkspace: Default\nSaveWorkspace: Default\nAlwaysSaveHistory: Default\n\nRCodeVersion: %s\nShowHiddenFiles: Default\nLibs: %s\n", + R.version$version.string, + .libPaths() + ), + file.path(path, sprintf("%s.Rproj", project_name)) + ) + # README + writeLines( + sprintf( + "# %s\n\n## Description\n\n## Usage\n\n## Author\n\n## License\n\n## References", + project_name + ), + file.path(path, "README.md") + ) + # Congradulations + message( + sprintf( + "Congradulations! You have created a new project at %s", + path + ) + ) +} diff --git a/catplot.Rproj b/catplot.Rproj index b9255bc..497f8bf 100644 --- a/catplot.Rproj +++ b/catplot.Rproj @@ -1,20 +1,20 @@ -Version: 1.0 - -RestoreWorkspace: Default -SaveWorkspace: Default -AlwaysSaveHistory: Default - -EnableCodeIndexing: Yes -UseSpacesForTab: Yes -NumSpacesForTab: 2 -Encoding: UTF-8 - -RnwWeave: Sweave -LaTeX: pdfLaTeX - -AutoAppendNewline: Yes -StripTrailingWhitespace: Yes - -BuildType: Package -PackageUseDevtools: Yes -PackageInstallArgs: --no-multiarch --with-keep.source +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source diff --git a/man/cat_dimplot.Rd b/man/cat_dimplot.Rd index 4387f25..0f01b42 100644 --- a/man/cat_dimplot.Rd +++ b/man/cat_dimplot.Rd @@ -6,17 +6,34 @@ \usage{ cat_dimplot( object, + dims = c(1, 2), + cells = NULL, + cols = NULL, + pt_size = 1, reduction = NULL, group_by = NULL, split_by = NULL, + shape_by = NULL, + order = NULL, + shuffle = FALSE, + seed = 1, label = FALSE, + label_size = 8 * 0.36, + label_color = "black", + label_box = FALSE, + repel = TRUE, + cells_highlight = NULL, + cols_highlight = "#DE2D26", + sizes_highlight = 1, + na_value = "grey50", + ncol = NULL, + combine = TRUE, raster = TRUE, + raster_dpi = c(512, 512), show_legend = TRUE, show_axis = TRUE, show_border = TRUE, title = NULL, - pt_size = 1, - repel = TRUE, palette = "Paired", ... ) @@ -24,16 +41,54 @@ cat_dimplot( \arguments{ \item{object}{A Seurat object containing categorical data.} +\item{dims}{The dimensions to plot. Default is c(1, 2).} + +\item{cells}{The cells to plot. Default is NULL.} + +\item{cols}{The columns to plot. Default is NULL.} + +\item{pt_size}{The size of the points on the plot. Default is 1.} + \item{reduction}{The dimensionality reduction method. Default is NULL.} \item{group_by}{The variable to group data by. Default is NULL.} \item{split_by}{The variable to split data by. Default is NULL.} +\item{shape_by}{The variable to shape data by. Default is NULL.} + +\item{order}{The order to plot the data in. Default is NULL.} + +\item{shuffle}{Logical value indicating whether to shuffle the data before plotting. Default is FALSE.} + +\item{seed}{The random seed to use for shuffling the data. Default is 1.} + \item{label}{Logical value indicating whether to show labels on the plot. Default is FALSE.} +\item{label_size}{The size of the labels on the plot. Default is 8 * 0.36.} + +\item{label_color}{The color of the labels on the plot. Default is "black".} + +\item{label_box}{Logical value indicating whether to show a box around the labels on the plot. Default is FALSE.} + +\item{repel}{Logical value indicating whether to use point repulsion to avoid overlapping labels. Default is TRUE.} + +\item{cells_highlight}{The cells to highlight on the plot. Default is NULL.} + +\item{cols_highlight}{The columns to highlight on the plot. Default is NULL.} + +\item{sizes_highlight}{The sizes to highlight on the plot. Default is NULL.} + +\item{na_value}{The value to use for missing data. Default is "grey50".} + +\item{ncol}{The number of columns to use for the plot. Default is NULL.} + +\item{combine}{Logical value indicating whether to combine the plots into a single plot. Default is TRUE.} + \item{raster}{Logical value indicating whether to use rasterization for improved performance. Default is TRUE.} +\item{raster_dpi}{The DPI to use for rasterization. Default is c(512, 512).} + \item{show_legend}{Logical value indicating whether to show the legend on the plot. Default is TRUE.} \item{show_axis}{Logical value indicating whether to show the axis on the plot. Default is TRUE.} @@ -42,10 +97,6 @@ cat_dimplot( \item{title}{The title for the plot. Default is NULL.} -\item{pt_size}{The size of the points on the plot. Default is 1.} - -\item{repel}{Logical value indicating whether to use point repulsion to avoid overlapping labels. Default is TRUE.} - \item{palette}{The color palette to use for the plot. Default is "Paired".} \item{...}{Additional parameters to be passed to the DimPlot() function in Seurat.} @@ -57,11 +108,13 @@ A ggplot2 object containing the dimensionality reduction plot. This function creates a dimensionality reduction plot for categorical data using Seurat and ggplot2. It allows for the selection of the reduction method, grouping, and splitting variables, as well as the visualization of labels, rasterization, and color palette. The cat_dimplot() function is intended to be used as a wrapper around Seurat's DimPlot() function. } \examples{ +\dontrun{ library(Seurat) library(ggplot2) data("pbmc_small") pbmc_small <- CreateSeuratObject(counts = pbmc_small) Idents(pbmc_small) <- pbmc_small$percent.mt > 10 cat_dimplot(object = pbmc_small, reduction = "tsne", group_by = "ident", palette = "Set1") +} } diff --git a/man/cat_dotplot.Rd b/man/cat_dotplot.Rd index 7898061..594c0fc 100644 --- a/man/cat_dotplot.Rd +++ b/man/cat_dotplot.Rd @@ -6,23 +6,52 @@ \usage{ cat_dotplot( x, + assay = NULL, features, - dot_scale = 4, col_min = -2.5, col_max = 2.5, - assay = NULL + dot_min = 0, + dot_scale = 4, + idents = NULL, + group_by = NULL, + split_by = NULL, + cluster_idents = FALSE, + scale = TRUE, + scale_by = "radius", + scale_min = NA, + scale_max = NA ) } \arguments{ \item{x}{A Seurat object containing the data to plot.} -\item{features}{A character vector of feature names to plot.} +\item{assay}{The assay to plot. Defaults to the default assay.} -\item{dot_scale}{The size of the dots to plot.} +\item{features}{A character vector of feature names to plot.} \item{col_min}{The minimum value for the color scale. Defaults to -2.5.} \item{col_max}{The maximum value for the color scale. Defaults to 2.5.} + +\item{dot_min}{The minimum value for the dot size scale. Defaults to 0.} + +\item{dot_scale}{The size of the dots to plot.} + +\item{idents}{A character vector of identities to plot. Defaults to all identities.} + +\item{group_by}{A character vector specifying the grouping variable. Defaults to "ident".} + +\item{split_by}{A character vector specifying the splitting variable.} + +\item{cluster_idents}{Whether to cluster the identities or not. Defaults to FALSE.} + +\item{scale}{Whether to scale the dot size or not. Defaults to TRUE.} + +\item{scale_by}{The variable to scale the dot size by. Defaults to "radius".} + +\item{scale_min}{The minimum value for the dot size scale. Defaults to NA.} + +\item{scale_max}{The maximum value for the dot size scale. Defaults to NA.} } \value{ A ggplot2 object. @@ -31,6 +60,8 @@ A ggplot2 object. This function plots a dot plot with categorical groups using the DotPlot function from the Seurat package. } \examples{ +\dontrun{ cat_dotplot(x = mySeuratObject, features = c("CD3D", "CD8A", "CD4")) +} } diff --git a/man/cat_featureplot.Rd b/man/cat_featureplot.Rd index 7d5a3db..3b1ee6f 100644 --- a/man/cat_featureplot.Rd +++ b/man/cat_featureplot.Rd @@ -72,6 +72,8 @@ A ggplot2 object. This function plots feature expression in reduced dimensions using the FeaturePlot function from the Seurat package. } \examples{ +\dontrun{ cat_featureplot(x = mySeuratObject, features = c("CD3D", "CD8A", "CD4"), reduction = "UMAP") +} } diff --git a/man/cat_save.Rd b/man/cat_save.Rd index f4b4fba..2304bfb 100644 --- a/man/cat_save.Rd +++ b/man/cat_save.Rd @@ -16,7 +16,7 @@ cat_save( dpi = 300, limitsize = TRUE, bg = NULL, - raw_data = FALSE, + source_data = FALSE, ... ) } @@ -46,7 +46,7 @@ plot to the device size} \item{bg}{A character string specifying the background color of the plot} -\item{raw_data}{A logical value specifying whether to save the raw data used +\item{source_data}{A logical value specifying whether to save the raw data used to create the plot as a separate CSV file} \item{...}{Additional arguments to be passed to ggsave} diff --git a/man/cat_set_path.Rd b/man/cat_set_path.Rd new file mode 100644 index 0000000..c71b110 --- /dev/null +++ b/man/cat_set_path.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities.R +\name{cat_set_path} +\alias{cat_set_path} +\title{Create a directory if it does not exist and return the path} +\usage{ +cat_set_path(path) +} +\arguments{ +\item{path}{A character string specifying the path to be created.} +} +\value{ +A character string specifying the path. +} +\description{ +This function takes a path as input and creates a directory if it does not +exist. It then returns the path. +} +\examples{ +cat_set_path("~/my_folder") + +} diff --git a/man/cat_vlnplot.Rd b/man/cat_vlnplot.Rd index c0ad788..2a7b212 100644 --- a/man/cat_vlnplot.Rd +++ b/man/cat_vlnplot.Rd @@ -42,6 +42,8 @@ A ggplot2 object. This function plots a violin plot with categorical groups using the VlnPlot function from the Seurat package. } \examples{ +\dontrun{ cat_vlnplot(object = mySeuratObject, features = c("CD3D", "CD8A", "CD4")) +} } diff --git a/man/create_ap.Rd b/man/create_ap.Rd index d905b0a..51a7ed2 100644 --- a/man/create_ap.Rd +++ b/man/create_ap.Rd @@ -9,8 +9,13 @@ create_ap(color = NULL, fill = NULL, shape = NULL, alpha = NULL, size = NULL) \arguments{ \item{color}{A character string specifying the color to be used in the plot} -\item{fill}{A character string specifying the fill color to be used in the -plot} +\item{fill}{A character string specifying the fill color to be used in the plot} + +\item{shape}{A character string specifying the shape to be used in the plot} + +\item{alpha}{A character string specifying the alpha value to be used in the plot} + +\item{size}{A character string specifying the size to be used in the plot} } \value{ A list containing the mapping and parameters for the plot diff --git a/man/dot-get_ggsci_palette.Rd b/man/dot-get_ggsci_palette.Rd index b72b194..a7873c9 100644 --- a/man/dot-get_ggsci_palette.Rd +++ b/man/dot-get_ggsci_palette.Rd @@ -27,7 +27,9 @@ This function returns a color palette from the ggsci package by name, with the option to specify the number of colors in the palette. } \examples{ +\dontrun{ palette <- catplot:::.get_ggsci_palette("npg", 6) barplot(1:6, col = palette) } +}