plot_composition_bar: Create barplot of average cell type composition

View source: R/plot_composition_bar.R

plot_composition_barR Documentation

Create barplot of average cell type composition

Description

Given a long formatted data.frame, this function creates a barplot for the average cell type composition among a set of samples (donors) using ggplot2.

Usage

plot_composition_bar(
  prop_long,
  sample_col = "RNum",
  x_col = "ALL",
  prop_col = "prop",
  ct_col = "cell_type",
  add_text = TRUE,
  min_prop_text = 0
)

Arguments

prop_long

A data.frame of cell type portions in long form

sample_col

A character(1) specifying the name of column in prop_long that identifies samples.

x_col

A character(1) specifying the name of column in prop_long that specifies the category to divide samples by.

prop_col

A character(1) specifying the name of column in prop_long that contains proportion values.

ct_col

A character(1) specifying the name of column in prop_long containing cell type names.

add_text

A logical(1) determining whether to add the rounded proportion value to the bars.

min_prop_text

A numeric(1) specifying the minimum proportion to display text. Values greater than (>) min_prop_text will be displayed.

Value

A stacked barplot ggplot2 object representing the mean proportion of cell types for each group.

Examples

# Load example data
data("rse_bulk_test")
data("est_prop")

# extract relevant colData from the example RangedSummarizedExperiment object
pd <- SummarizedExperiment::colData(rse_bulk_test) |>
    as.data.frame()

# combine with the example estimated proportions in a long style table
est_prop_long <- est_prop |>
    tibble::rownames_to_column("RNum") |>
    tidyr::pivot_longer(!RNum, names_to = "cell_type", values_to = "prop") |>
    dplyr::inner_join(pd |> dplyr::select(RNum, Dx))

est_prop_long

# Create composition bar plots
# Mean composition of all samples
plot_composition_bar(est_prop_long)

# Mean composition by Dx
plot_composition_bar(est_prop_long, x_col = "Dx")

# control minimum value of text to add
plot_composition_bar(est_prop_long, x_col = "Dx", min_prop_text = 0.1)

# plot all samples, then facet by Dx
plot_composition_bar(est_prop_long, x_col = "RNum", add_text = FALSE) +
    ggplot2::facet_wrap(~Dx, scales = "free_x")


lahuuki/DeconvoBuddies documentation built on Nov. 2, 2024, 10:40 p.m.