Nothing
#' Create a base plot with gene expression data on a phylogenetic tree
#'
#' This function creates a base plot using 'ggtree' and 'ggtreeExtra' libraries, adding gene expression
#' data as colored tiles to the plot. It allows for dynamic coloring of the genes and includes
#' adjustments for alpha transparency based on the expression value.
#'
#' @importFrom ggplot2 aes scale_fill_manual scale_alpha_continuous guide_legend
#' @param p A ggtree plot object to which the data will be added.
#' @param data A data frame containing gene expression data with columns for Samples, Genes, and Values.
#' @param gene_colors A named vector of colors for genes.
#' @param gene_label A character string used as a label in the legend for the genes. Default is "Gene".
#' @return A `ggtree` plot object with the gene expression data added.
#' @export
#'
#' @examples
#' \donttest{
#' # Check and load required packages
#' if (requireNamespace("ggtreeExtra", quietly = TRUE) &&
#' requireNamespace("ggplot2", quietly = TRUE)) {
#' library(ggtreeExtra)
#' library(ggplot2)
#'
#' file_path <- system.file("extdata", "p_tree_test.rds", package = "TransProR")
#' p <- readRDS(file_path)
#'
#' # Create gene expression data frame
#' expression_data <- data.frame(
#' Sample = rep(c("Species_A", "Species_B", "Species_C", "Species_D"), each = 5),
#' Gene = rep(paste0("Gene", 1:5), times = 4),
#' Value = runif(20, min = 0, max = 1) # Randomly generate expression values between 0 and 1
#' )
#'
#' # Define gene colors (named vector)
#' gene_colors <- c(
#' Gene1 = "#491588",
#' Gene2 = "#301b8d",
#' Gene3 = "#1a237a",
#' Gene4 = "#11479c",
#' Gene5 = "#0a5797"
#' )
#'
#' # Call create_base_plot function to add gene expression data
#' p <- create_base_plot(p, expression_data, gene_colors)
#' } else {
#' message("Required packages 'ggtreeExtra' and 'ggplot2' are not installed.")
#' }
#' }
#'
create_base_plot <- function(p, data, gene_colors, gene_label="Gene") {
# Define local variables
Sample <- data$Sample
value <- data$value
Gene <- data$Gene
if (!requireNamespace("ggtreeExtra", quietly = TRUE)) {
stop("ggtreeExtra is required for using create_base_plot. Please install it.", call. = FALSE)
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("ggplot2 is required to use geom_tile. Please install it.", call. = FALSE)
}
p <- p +
ggtreeExtra::geom_fruit(
data=data,
geom="geom_tile",
mapping=ggplot2::aes(y=Sample, alpha=value, x=Gene, fill=Gene),
offset=0.001,
pwidth=2
) +
ggplot2::scale_fill_manual(
name=gene_label,
values=gene_colors,
guide=ggplot2::guide_legend(keywidth=0.65, keyheight=0.35, order=1)
) +
# Assuming the function 'adjust_alpha_scale' is defined elsewhere to adjust alpha scale based on the expression values
adjust_alpha_scale(data, gene_label)
return(p)
}
#' Add a boxplot layer to a `ggtree` plot
#'
#' This function adds a boxplot layer to an existing `ggtree` plot object using ggtreeExtra's geom_fruit for boxplots.
#' It is primarily used to display statistical summaries of the data related to gene expressions or other metrics.
#'
#' @importFrom ggplot2 aes
#' @param p An existing ggtree plot object.
#' @param data A data frame containing the data to be plotted. Expected to have columns for 'Sample' and 'value'.
#' @param fill_color A character string specifying the fill color for the boxplots. Default is "#f28131".
#' @param alpha Numeric value for the transparency of the boxplots. Default is 0.6.
#' @param offset Numeric value, the position of the boxplot on the x-axis relative to its gene name. Default is 0.22.
#' @param pwidth Numeric value, the width of the boxplot. Default is 0.5.
#' @return A `ggtree` plot object with the added boxplot layer.
#' @export
#'
#' @examples
#' \donttest{
#' # Check and load required packages
#' if (requireNamespace("ggtreeExtra", quietly = TRUE) &&
#' requireNamespace("ggplot2", quietly = TRUE)) {
#' library(ggtreeExtra)
#' library(ggplot2)
#'
#' file_path <- system.file("extdata", "p_tree_test.rds", package = "TransProR")
#' p <- readRDS(file_path)
#'
#' # Create boxplot data frame
#' boxplot_data <- data.frame(
#' Sample = rep(c("Species_A", "Species_B", "Species_C", "Species_D"), each = 30),
#' value = c(
#' rnorm(30, mean = 5, sd = 1), # Data for Species_A
#' rnorm(30, mean = 7, sd = 1.5), # Data for Species_B
#' rnorm(30, mean = 6, sd = 1.2), # Data for Species_C
#' rnorm(30, mean = 8, sd = 1.3) # Data for Species_D
#' )
#' )
#'
#' # Call add_boxplot function to add boxplot layer
#' p_with_boxplot <- add_boxplot(p, boxplot_data)
#' } else {
#' message("Required packages 'ggtreeExtra' and 'ggplot2' are not installed.")
#' }
#' }
#'
add_boxplot <- function(p, data, fill_color="#f28131", alpha=0.6, offset=0.22, pwidth=0.5) {
# Define local variables
Sample <- data$Sample
value <- data$value
if (!requireNamespace("ggtreeExtra", quietly = TRUE)) {
stop("ggtreeExtra is required for using create_base_plot. Please install it.", call. = FALSE)
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("ggplot2 is required to use geom_boxplot. Please install it.", call. = FALSE)
}
p + ggtreeExtra::geom_fruit(
data=data,
geom="geom_boxplot",
mapping=ggplot2::aes(y=Sample, x=value),
fill=fill_color,
alpha=alpha,
offset=offset,
pwidth=pwidth,
size=0.05,
outlier.size=0.3,
outlier.stroke=0.06,
outlier.shape=21,
show.legend=FALSE
)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.