Skip to contents

BoxPlot

# Create sample data
set.seed(123)
data <- data.frame(
  group = rep(LETTERS[1:4], each = 25),
  value = c(
    rnorm(25, 10, 2), rnorm(25, 12, 2),
    rnorm(25, 11, 3), rnorm(25, 14, 2)
  ),
  treatment = rep(c("Control", "Treatment"), 50),
  gender = sample(c("Male", "Female"), 100, replace = TRUE)
)

# Simple box plot
BoxPlot(data, x = "group", y = "value")


# Box plot with custom labels
BoxPlot(data,
  x = "group", y = "value",
  title = "Distribution by Group",
  xlab = "Experimental Group",
  ylab = "Measurement Value"
)

Grouped Box Plots

# Side-by-side (dodged) box plots by treatment
BoxPlot(data, x = "group", y = "value", group_by = "treatment")


# With custom group legend name
BoxPlot(data,
  x = "group", y = "value",
  group_by = "treatment",
  group_name = "Treatment Group"
)


# With custom color palette
BoxPlot(data,
  x = "group", y = "value",
  group_by = "treatment",
  palette = "Set1"
)

Adding Data Points

# Add jittered points to show individual observations
BoxPlot(data, x = "group", y = "value", add_point = TRUE)


# Customize point appearance
BoxPlot(data,
  x = "group", y = "value",
  add_point = TRUE,
  pt_color = "darkblue",
  pt_size = 1.5,
  pt_alpha = 0.6
)


# With grouped data
BoxPlot(data,
  x = "group", y = "value",
  group_by = "treatment",
  add_point = TRUE,
  pt_alpha = 0.5
)

Highlighting Specific Points

# Highlight outliers or specific observations
BoxPlot(data,
  x = "group", y = "value",
  add_point = TRUE,
  highlight = "value > 15",
  highlight_color = "red",
  highlight_size = 3
)


# Highlight by row indices
BoxPlot(data,
  x = "group", y = "value",
  add_point = TRUE,
  highlight = c(1, 5, 10, 15),
  highlight_color = "orange"
)

Statistical Comparisons

# Compare specific pairs
BoxPlot(data,
  x = "group", y = "value",
  comparisons = list(c("A", "B"), c("A", "D"))
)


# Compare all pairs
BoxPlot(data,
  x = "group", y = "value",
  comparisons = TRUE
)


# Use t-test instead of Wilcoxon
BoxPlot(data,
  x = "group", y = "value",
  comparisons = list(c("A", "D")),
  pairwise_method = "t.test"
)


# Show significance symbols instead of p-values
BoxPlot(data,
  x = "group", y = "value",
  comparisons = TRUE,
  sig_label = "p.signif"
)


# Hide non-significant comparisons
BoxPlot(data,
  x = "group", y = "value",
  comparisons = TRUE,
  hide_ns = TRUE
)


# With grouped data - compare groups within each x category
BoxPlot(data,
  x = "group", y = "value",
  group_by = "treatment",
  comparisons = TRUE
)


# Multiple group comparison (Kruskal-Wallis)
BoxPlot(data,
  x = "group", y = "value",
  multiplegroup_comparisons = TRUE
)

Paired Data Analysis

# Create paired data (before/after measurements)
paired_data <- data.frame(
  time = factor(rep(c("Before", "After"), each = 20)),
  subject = factor(rep(1:20, 2)),
  score = c(rnorm(20, 50, 10), rnorm(20, 55, 10))
)

# Paired box plot with connecting lines
BoxPlot(paired_data,
  x = "time", y = "score",
  paired_by = "subject",
  add_point = TRUE
)


# With paired statistical test
BoxPlot(paired_data,
  x = "time", y = "score",
  paired_by = "subject",
  comparisons = list(c("Before", "After")),
  pairwise_method = "t.test"
)

Sorting and Orientation

# Sort by mean value (ascending)
BoxPlot(data, x = "group", y = "value", sort_x = "mean_asc")


# Sort by median value (descending)
BoxPlot(data, x = "group", y = "value", sort_x = "median_desc")


# Horizontal box plot
BoxPlot(data, x = "group", y = "value", flip = TRUE)

Visual Enhancements

# Add trend line connecting medians
BoxPlot(data, x = "group", y = "value", add_trend = TRUE)


# Add reference line
BoxPlot(data,
  x = "group", y = "value",
  add_line = 12,
  line_color = "red",
  line_type = 2
)


# Add alternating background
BoxPlot(data,
  x = "group", y = "value",
  add_bg = TRUE,
  bg_alpha = 0.1
)


# Add mean indicator
BoxPlot(data,
  x = "group", y = "value",
  add_stat = mean,
  stat_name = "Mean",
  stat_color = "red",
  stat_shape = 18
)

Faceting

# Facet by another variable
BoxPlot(data,
  x = "group", y = "value",
  facet_by = "gender"
)


# Control facet layout
BoxPlot(data,
  x = "group", y = "value",
  facet_by = "gender",
  facet_ncol = 2
)


# Free y-axis scales per facet
BoxPlot(data,
  x = "group", y = "value",
  facet_by = "gender",
  facet_scales = "free_y"
)

Fill Modes

# Fill by x-axis category (default when no group_by)
BoxPlot(data, x = "group", y = "value", fill_mode = "x")


# Fill by mean value (gradient)
BoxPlot(data,
  x = "group", y = "value",
  fill_mode = "mean",
  palette = "RdYlBu"
)


# Fill by median value (gradient)
BoxPlot(data,
  x = "group", y = "value",
  fill_mode = "median",
  palette = "viridis"
)

Wide Format Data

# Wide format: each column is a group
wide_data <- data.frame(
  GroupA = rnorm(30, 10, 2),
  GroupB = rnorm(30, 12, 2),
  GroupC = rnorm(30, 11, 3)
)

BoxPlot(wide_data,
  x = c("GroupA", "GroupB", "GroupC"),
  in_form = "wide"
)

Splitting into Multiple Plots

# Create separate plots by a variable
BoxPlot(data,
  x = "group", y = "value",
  split_by = "gender",
  combine = TRUE,
  ncol = 2
)


ViolinPlot

# Create sample data with different distributions
set.seed(456)
vdata <- data.frame(
  category = rep(c("Normal", "Bimodal", "Skewed", "Uniform"), each = 100),
  value = c(
    rnorm(100, 50, 10), # Normal
    c(rnorm(50, 30, 5), rnorm(50, 70, 5)), # Bimodal
    rexp(100, 0.1), # Skewed
    runif(100, 20, 80) # Uniform
  ),
  group = rep(c("A", "B"), 200)
)

# Simple violin plot
ViolinPlot(vdata, x = "category", y = "value")


# With title and labels
ViolinPlot(vdata,
  x = "category", y = "value",
  title = "Distribution Shapes Comparison",
  xlab = "Distribution Type",
  ylab = "Value"
)

Violin with Box Plot Overlay

# Add box plot inside violin (shows quartiles)
ViolinPlot(vdata,
  x = "category", y = "value",
  add_box = TRUE
)


# Customize box overlay
ViolinPlot(vdata,
  x = "category", y = "value",
  add_box = TRUE,
  box_color = "white",
  box_width = 0.15,
  box_ptsize = 3
)

Adding Data Points

# Add jittered points to show individual observations
ViolinPlot(vdata,
  x = "category", y = "value",
  add_point = TRUE
)


# Combine box overlay with points
ViolinPlot(vdata,
  x = "category", y = "value",
  add_box = TRUE,
  add_point = TRUE,
  pt_alpha = 0.3
)


# Customize point appearance
ViolinPlot(vdata,
  x = "category", y = "value",
  add_point = TRUE,
  pt_color = "navy",
  pt_size = 0.8,
  pt_alpha = 0.5,
  jitter_width = 0.3
)

Grouped Violin Plots

# Side-by-side violins by group
ViolinPlot(vdata,
  x = "category", y = "value",
  group_by = "group"
)


# With box overlay
ViolinPlot(vdata,
  x = "category", y = "value",
  group_by = "group",
  add_box = TRUE
)


# Custom palette
ViolinPlot(vdata,
  x = "category", y = "value",
  group_by = "group",
  palette = "Set2",
  add_box = TRUE
)

Statistical Comparisons

# Compare specific distributions
ViolinPlot(vdata,
  x = "category", y = "value",
  comparisons = list(
    c("Normal", "Bimodal"),
    c("Normal", "Skewed")
  )
)


# All pairwise comparisons
ViolinPlot(vdata,
  x = "category", y = "value",
  comparisons = TRUE,
  sig_label = "p.signif"
)


# Grouped comparisons
ViolinPlot(vdata,
  x = "category", y = "value",
  group_by = "group",
  comparisons = TRUE
)

Paired Data Analysis

# Create paired data
paired_data2 <- data.frame(
  condition = factor(rep(c("Baseline", "Treatment"), each = 30)),
  patient = factor(rep(1:30, 2)),
  response = c(rnorm(30, 100, 20), rnorm(30, 120, 20))
)

# Paired violin with connecting lines
ViolinPlot(paired_data2,
  x = "condition", y = "response",
  paired_by = "patient",
  add_box = TRUE
)


# With paired t-test
ViolinPlot(paired_data2,
  x = "condition", y = "response",
  paired_by = "patient",
  comparisons = list(c("Baseline", "Treatment")),
  pairwise_method = "t.test"
)

Visual Enhancements

# Highlight extreme values
ViolinPlot(vdata,
  x = "category", y = "value",
  add_point = TRUE,
  highlight = "value > 80 | value < 10",
  highlight_color = "red",
  highlight_size = 2
)


# Add trend line
ViolinPlot(vdata,
  x = "category", y = "value",
  add_trend = TRUE,
  trend_linewidth = 1.5
)


# Add reference line
ViolinPlot(vdata,
  x = "category", y = "value",
  add_line = 50,
  line_color = "darkgreen",
  line_type = 1
)


# Add mean indicator
ViolinPlot(vdata,
  x = "category", y = "value",
  add_stat = mean,
  stat_name = "Mean",
  stat_color = "red",
  stat_shape = 18,
  stat_size = 3
)

Fill Modes

# Fill by x category
ViolinPlot(vdata,
  x = "category", y = "value",
  fill_mode = "x",
  palette = "Pastel1"
)


# Fill by mean (gradient coloring)
ViolinPlot(vdata,
  x = "category", y = "value",
  fill_mode = "mean",
  palette = "RdYlGn"
)


# Fill by median with reversed gradient
ViolinPlot(vdata,
  x = "category", y = "value",
  fill_mode = "median",
  palette = "Blues",
  fill_reverse = TRUE
)

Sorting and Orientation

# Sort by mean value
ViolinPlot(vdata,
  x = "category", y = "value",
  sort_x = "mean_desc",
  add_box = TRUE
)


# Horizontal violin plot
ViolinPlot(vdata,
  x = "category", y = "value",
  flip = TRUE,
  add_box = TRUE
)

Faceting

# Add faceting variable
vdata$experiment <- sample(c("Exp1", "Exp2"), nrow(vdata), replace = TRUE)

# Facet by experiment
ViolinPlot(vdata,
  x = "category", y = "value",
  facet_by = "experiment",
  add_box = TRUE
)


# Free scales
ViolinPlot(vdata,
  x = "category", y = "value",
  facet_by = "experiment",
  facet_scales = "free_y"
)

Wide Format Data

# Wide format input
wide_data2 <- data.frame(
  Control = rnorm(50, 100, 15),
  LowDose = rnorm(50, 110, 15),
  HighDose = rnorm(50, 130, 20)
)

ViolinPlot(wide_data2,
  x = c("Control", "LowDose", "HighDose"),
  in_form = "wide",
  add_box = TRUE,
  xlab = "Treatment",
  ylab = "Response"
)


ScatterPlot

# Create sample data
set.seed(8525)
sdata <- data.frame(
  x = rnorm(100),
  y = rnorm(100),
  size_val = abs(rnorm(100)),
  category = sample(c("A", "B", "C"), 100, replace = TRUE),
  group = sample(c("Group1", "Group2"), 100, replace = TRUE)
)

# Basic scatter plot
ScatterPlot(sdata, x = "x", y = "y")


# Highlight specific points
ScatterPlot(sdata, x = "x", y = "y", highlight = "x > 1 & y > 0")


# Size by numeric column
ScatterPlot(sdata, x = "x", y = "y", size_by = "size_val")


# Color by numeric column with continuous gradient
ScatterPlot(sdata, x = "x", y = "y", color_by = "size_val", palette = "RdYlBu")


# Color by categorical column
ScatterPlot(sdata, x = "x", y = "y", color_by = "category")


# Combine size and color mapping with custom border
ScatterPlot(sdata,
  x = "x", y = "y", size_by = "size_val", color_by = "category",
  border_color = "black"
)


# Border color matches fill color (for cohesive look)
ScatterPlot(sdata,
  x = "x", y = "y", color_by = "category",
  border_color = TRUE
)


# Use shape without fill (solid points)
ScatterPlot(sdata,
  x = "x", y = "y", color_by = "category",
  shape = 16, palette = "Set1"
)


# Split by group to create separate panels
ScatterPlot(sdata,
  x = "x", y = "y", color_by = "category",
  split_by = "group", combine = TRUE
)


# Facet by group (alternative to split_by)
ScatterPlot(sdata, x = "x", y = "y", color_by = "category", facet_by = "group")


# Log transformation of axes
data_pos <- data.frame(
  x = 10^rnorm(100, 2, 1),
  y = 10^rnorm(100, 3, 0.5)
)
ScatterPlot(data_pos, x = "x", y = "y", xtrans = "log10", ytrans = "log10")


BarPlot

# Simple bar plot (counts)
bar_data1 <- data.frame(
  category = c("A", "B", "C", "D", "A", "B")
)
BarPlot(bar_data1, x = "category")


# Bar plot with values
bar_data2 <- data.frame(
  category = c("A", "B", "C", "D"),
  value = c(10, 15, 8, 12)
)
BarPlot(bar_data2, x = "category", y = "value")


# Grouped bar plot (dodged)
bar_data3 <- data.frame(
  category = rep(c("A", "B", "C"), 2),
  value = c(10, 15, 8, 12, 18, 14),
  group = rep(c("G1", "G2"), each = 3)
)
BarPlot(bar_data3, x = "category", y = "value", group_by = "group")


# Stacked bar plot
BarPlot(bar_data3, x = "category", y = "value", group_by = "group", position = "stack")


# With labels and customization
BarPlot(
  bar_data3,
  x = "category", y = "value", group_by = "group",
  label = TRUE, palette = "Set2", flip = TRUE
)


# With splits (multiple plots)
bar_data3$experiment <- rep(c("Exp1", "Exp2"), 3)
BarPlot(bar_data3, x = "category", y = "value", split_by = "experiment")


# With faceting (single plot, multiple panels)
BarPlot(bar_data3, x = "category", y = "value", facet_by = "experiment")


LinePlot

# Basic line plot
line_data <- data.frame(
  time = rep(c("T1", "T2", "T3", "T4"), 2),
  value = c(10, 15, 13, 20, 8, 12, 11, 18),
  group = rep(c("Control", "Treatment"), each = 4)
)
LinePlot(line_data, x = "time", y = "value")


# Grouped line plot
LinePlot(line_data, x = "time", y = "value", group_by = "group")


# With background stripes
LinePlot(line_data, x = "time", y = "value", group_by = "group", add_bg = TRUE)


# With error bars
line_data$sd <- c(2, 2.5, 2, 3, 1.5, 2, 1.8, 2.5)
LinePlot(
  line_data,
  x = "time",
  y = "value",
  group_by = "group",
  add_errorbars = TRUE,
  errorbar_sd = "sd"
)


# With horizontal reference line
LinePlot(
  line_data,
  x = "time",
  y = "value",
  group_by = "group",
  add_hline = 15,
  hline_type = "dashed"
)


# Split by another variable
line_data$batch <- rep(c("Batch1", "Batch2"), each = 4)
LinePlot(line_data, x = "time", y = "value", group_by = "group", split_by = "batch")


# Faceted plot
LinePlot(line_data, x = "time", y = "value", group_by = "group", facet_by = "batch")


DensityPlot

set.seed(8525)
dens_data <- data.frame(
  x = c(rnorm(500, -1), rnorm(500, 1)),
  group = rep(c("A", "B"), each = 500),
  facet = sample(c("F1", "F2"), 1000, replace = TRUE)
)

DensityPlot(dens_data, x = "x")

DensityPlot(dens_data, x = "x", group_by = "group")

DensityPlot(dens_data, x = "x", group_by = "group", facet_by = "facet")

DensityPlot(dens_data, x = "x", split_by = "facet", add_bars = TRUE)


DotPlot

mtcars_data <- datasets::mtcars
mtcars_data$carb <- factor(mtcars_data$carb)
mtcars_data$gear <- factor(mtcars_data$gear)

DotPlot(mtcars_data,
  x = "carb", y = "gear", size_by = "wt",
  fill_by = "mpg", fill_cutoff = 18
)


DotPlot(mtcars_data,
  x = "carb", y = "gear", size_by = "wt",
  fill_by = "mpg", fill_cutoff = 18, add_bg = TRUE
)


# Scatter plot (both axes numeric)
DotPlot(mtcars_data,
  x = "qsec", y = "drat", size_by = "wt",
  fill_by = "mpg", fill_cutoff = 18
)


JitterPlot

set.seed(8525)
n <- 200
x_jit <- sample(LETTERS[1:5], n, replace = TRUE)
group_jit <- sample(c("G1", "G2"), n, replace = TRUE)
size_jit <- rexp(n, rate = 1)
id_jit <- paste0("pt", seq_len(n))
y_jit <- rnorm(n, mean = ifelse(group_jit == "G1", 0.5, -0.5)) +
  as.numeric(factor(x_jit, levels = LETTERS[1:5])) / 10
jit_df <- data.frame(
  x = factor(x_jit, levels = LETTERS[1:5]),
  y = y_jit,
  group = group_jit,
  size = size_jit,
  id = id_jit
)

# Basic
JitterPlot(jit_df, x = "x", y = "y")


# Map size with transform; legend shows original values
JitterPlot(jit_df,
  x = "x", y = "y", size_by = "size", size_name = "Abundance",
  size_trans = sqrt, order_by = "-y^2"
)


# Dodge by group and add a horizontal line
JitterPlot(jit_df,
  x = "x", y = "y", group_by = "group",
  add_hline = 0, hline_type = "dashed", hline_color = "red2"
)


# Label top points by distance (y^2 + size^2)
JitterPlot(jit_df, x = "x", y = "y", size_by = "size", label_by = "id", nlabel = 3)


# Flip axes
JitterPlot(jit_df, x = "x", y = "y", flip = TRUE)


AreaPlot

area_data <- data.frame(
  x = rep(c("A", "B", "C", "D"), 2),
  y = c(1, 3, 6, 4, 2, 5, 7, 8),
  group = rep(c("F1", "F2"), each = 4),
  split = rep(c("X", "Y"), 4)
)

AreaPlot(area_data, x = "x", y = "y", group_by = "group")


AreaPlot(area_data,
  x = "x", y = "y", group_by = "group",
  scale_y = TRUE
)


AreaPlot(area_data, x = "x", y = "y", split_by = "group")


AreaPlot(area_data,
  x = "x", y = "y", split_by = "group",
  palette = c(F1 = "Blues", F2 = "Reds")
)


AreaPlot(area_data,
  x = "x", y = "y", group_by = "group", split_by = "split",
  legend.direction = c(X = "horizontal", Y = "vertical"),
  legend.position = c(X = "top", Y = "right")
)


RidgePlot

set.seed(8525)
ridge_data <- data.frame(
  x = c(rnorm(250, -1), rnorm(250, 1)),
  group = rep(LETTERS[1:5], each = 100)
)

RidgePlot(ridge_data, x = "x") # fallback to density plot

RidgePlot(ridge_data, x = "x", add_vline = 0, vline_color = "black")

RidgePlot(ridge_data, x = "x", group_by = "group")

RidgePlot(ridge_data, x = "x", group_by = "group", reverse = TRUE)

RidgePlot(ridge_data, x = "x", group_by = "group", add_vline = TRUE, vline_color = TRUE)


# wide form
ridge_wide <- data.frame(
  A = rnorm(100),
  B = rnorm(100),
  C = rnorm(100),
  D = rnorm(100),
  E = rnorm(100),
  group = sample(letters[1:4], 100, replace = TRUE)
)
RidgePlot(ridge_wide, group_by = LETTERS[1:5], in_form = "wide")

RidgePlot(ridge_wide, group_by = LETTERS[1:5], in_form = "wide", facet_by = "group")


EnrichMap

data(enrich_example)

EnrichMap(enrich_example)

EnrichMap(enrich_example, label = "feature")

EnrichMap(enrich_example, show_keyword = TRUE, label = "term")

EnrichMap(enrich_example, show_keyword = TRUE, label = "feature")


data(enrich_multidb_example)
EnrichMap(enrich_multidb_example, split_by = "Database")

EnrichMap(enrich_multidb_example,
  split_by = "Database",
  palette = list(DB1 = "Paired", DB2 = "Set1")
)


GSEAPlot

data(gsea_example)

# Plot single gene set
GSEAPlot(gsea_example, gene_sets = attr(gsea_example, "gene_sets")[1])

# Plot multiple gene sets
GSEAPlot(gsea_example, gene_sets = attr(gsea_example, "gene_sets")[1:4])

# Label core genes
GSEAPlot(
  gsea_example,
  gene_sets = attr(gsea_example, "gene_sets")[1],
  n_coregenes = 5
)

# Customize line appearance
GSEAPlot(
  gsea_example,
  gene_sets = attr(gsea_example, "gene_sets")[1],
  line_width = 2,
  line_color = "#FF6B6B"
)

# Return separate plots instead of combined
plots <- GSEAPlot(
  gsea_example,
  gene_sets = attr(gsea_example, "gene_sets")[1:3],
  combine = FALSE
)

VolcanoPlot

set.seed(8525)
deg_data <- data.frame(
  avg_log2FC = c(
    -3.69, -4.10, -2.68, -3.51, -3.09, -2.52, -3.53, -3.35, -2.82, -2.71,
    3.67, 4.79, 10.14, 5.36, 4.56, 4.62, 3.31, 4.72, 3.01, 3.86
  ),
  p_val_adj = c(
    3.82e-09, 1.52e-07, 1.79e-07, 4.68e-07, 4.83e-07, 6.26e-07, 2.61e-06,
    1.33e-05, 1.79e-05, 3.71e-05, 8.93e-04, 9.61e-04, 1.47e-03, 4.35e-03,
    4.85e-03, 5.12e-03, 1.90e-02, 2.13e-02, 3.80e-02, 6.72e-02
  ),
  gene = c(
    "HLA-DPB1", "LYZ", "HLA-DRA", "TYMP", "HLA-DPA1", "HLA-DRB1", "CST3",
    "HLA-DQB1", "HLA-DRB5", "LST1", "CCL5", "LCK", "MS4A6A", "CD3D", "CD7",
    "CD3E", "CTSW", "GZMM", "GZMA", "IL32"
  ),
  group = sample(LETTERS[1:2], 20, replace = TRUE)
)

VolcanoPlot(deg_data, x = "avg_log2FC", y = "p_val_adj", y_cutoff_name = "-log10(0.05)")


VolcanoPlot(deg_data,
  x = "avg_log2FC", y = "p_val_adj",
  label_by = "gene", y_cutoff_name = "-log10(0.05)"
)


VolcanoPlot(deg_data,
  x = "avg_log2FC", y = "p_val_adj", y_cutoff_name = "none",
  flip_negatives = TRUE, label_by = "gene"
)


VolcanoPlot(deg_data,
  x = "avg_log2FC", y = "p_val_adj", y_cutoff_name = "none",
  flip_negatives = TRUE, facet_by = "group", label_by = "gene"
)


VolcanoPlot(deg_data,
  x = "avg_log2FC", y = "p_val_adj", y_cutoff_name = "none",
  flip_negatives = TRUE, split_by = "group", label_by = "gene"
)


KMPlot

library(survival)

# Basic Kaplan-Meier plot
KMPlot(data = lung, time = "time", status = "status")

# Multiple groups with p-value
KMPlot(
  data = lung,
  time = "time",
  status = "status",
  group_by = "sex",
  show_pval = TRUE
)

# With risk table
KMPlot(
  data = lung,
  time = "time",
  status = "status",
  group_by = "sex",
  show_risk_table = TRUE,
  show_pval = TRUE
)

# With confidence intervals and median lines
KMPlot(
  data = lung,
  time = "time",
  status = "status",
  group_by = "sex",
  show_conf_int = TRUE,
  show_median_line = "hv",
  palette = "Set1"
)

# Publication-ready plot
KMPlot(
  data = lung,
  time = "time",
  status = "status",
  group_by = "sex",
  show_risk_table = TRUE,
  show_pval = TRUE,
  show_conf_int = TRUE,
  show_median_line = "hv",
  palette = "jco",
  title = "Overall Survival by Sex",
  xlab = "Time (days)",
  ylab = "Survival Probability",
  theme_args = list(base_size = 14)
)

Heatmap

set.seed(8525)

matrix_data <- matrix(rnorm(60), nrow = 6, ncol = 10)
rownames(matrix_data) <- paste0("R", 1:6)
colnames(matrix_data) <- paste0("C", 1:10)

if (requireNamespace("cluster", quietly = TRUE)) {
  Heatmap(matrix_data)
}


if (requireNamespace("cluster", quietly = TRUE)) {
  # use a different color palette
  # change the main legend title
  # show row names (legend will be hidden)
  # show column names
  # change the row name annotation name and side
  # change the column name annotation name
  Heatmap(matrix_data,
    palette = "viridis", values_by = "z-score",
    show_row_names = TRUE, show_column_names = TRUE,
    rows_name = "Features", row_names_side = "left",
    columns_name = "Samples"
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  # flip the heatmap
  Heatmap(matrix_data,
    palette = "viridis", values_by = "z-score",
    show_row_names = TRUE, show_column_names = TRUE,
    rows_name = "Features", row_names_side = "left",
    columns_name = "Samples", flip = TRUE
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  # add annotations to the heatmap
  rows_data <- data.frame(
    rows = paste0("R", 1:6),
    group = sample(c("X", "Y", "Z"), 6, replace = TRUE)
  )
  Heatmap(matrix_data,
    rows_data = rows_data,
    row_annotation = list(Group = "group"),
    row_annotation_type = list(Group = "simple"),
    row_annotation_palette = list(Group = "Spectral")
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  Heatmap(matrix_data,
    rows_data = rows_data,
    rows_split_by = "group"
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  # add labels to the heatmap
  Heatmap(matrix_data,
    rows_data = rows_data,
    rows_split_by = "group", cell_type = "label",
    label = function(x) {
      ifelse(
        x > 0, scales::number(x, accuracy = 0.01), NA
      )
    }
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  # add labels based on an external data
  pvalues <- matrix(runif(60, 0, 0.5), nrow = 6, ncol = 10)
  Heatmap(matrix_data,
    rows_data = rows_data,
    rows_split_by = "group", cell_type = "label",
    label = function(x, i, j) {
      pv <- ComplexHeatmap::pindex(pvalues, i, j)
      ifelse(pv < 0.01, "***",
        ifelse(pv < 0.05, "**",
          ifelse(pv < 0.1, "*", NA)
        )
      )
    }
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  # Make the row/column name annotation thicker
  Heatmap(matrix_data,
    # Use the "name." prefix
    column_annotation_params = list(name.columns = list(height = 5)),
    row_annotation_params = list(name.rows = list(width = 5))
  )
}

Long Format Data

# Use long form data
N <- 500
heatmap_long <- data.frame(
  value = rnorm(N),
  c = sample(letters[1:8], N, replace = TRUE),
  r = sample(LETTERS[1:5], N, replace = TRUE),
  p = sample(c("x", "y"), N, replace = TRUE),
  q = sample(c("X", "Y", "Z"), N, replace = TRUE),
  a = as.character(sample(1:5, N, replace = TRUE)),
  p1 = runif(N),
  p2 = runif(N)
)

if (requireNamespace("cluster", quietly = TRUE)) {
  Heatmap(heatmap_long,
    rows_by = "r", columns_by = "c", values_by = "value",
    rows_split_by = "p", columns_split_by = "q", show_column_names = TRUE
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  # split into multiple heatmaps
  Heatmap(heatmap_long,
    values_by = "value", columns_by = "c", rows_by = "r", split_by = "p",
    upper_cutoff = 2, lower_cutoff = -2, legend.position = c("none", "right"),
    design = "AAAAAA#BBBBBBB"
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  # cell_type = "bars" (default is "tile")
  Heatmap(heatmap_long,
    values_by = "value", rows_by = "r", columns_by = "c",
    cell_type = "bars"
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  p <- Heatmap(heatmap_long,
    values_by = "value", rows_by = "r", columns_by = "c",
    cell_type = "dot", dot_size = length, dot_size_name = "data points",
    add_bg = TRUE, add_reticle = TRUE
  )
  p
}


if (requireNamespace("cluster", quietly = TRUE)) {
  Heatmap(heatmap_long,
    values_by = "value", rows_by = "r", columns_by = "c",
    cell_type = "pie", pie_group_by = "q", pie_size = sqrt,
    add_bg = TRUE, add_reticle = TRUE
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  Heatmap(heatmap_long,
    values_by = "value", rows_by = "r", columns_by = "c",
    cell_type = "violin", add_bg = TRUE, add_reticle = TRUE
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  Heatmap(heatmap_long,
    values_by = "value", rows_by = "r", columns_by = "c",
    cell_type = "boxplot", add_bg = TRUE, add_reticle = TRUE
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  Heatmap(heatmap_long,
    values_by = "value", rows_by = "r", columns_by = "c",
    column_annotation = list(r1 = "p", r2 = "q", r3 = "p1"),
    column_annotation_type = list(r1 = "ring", r2 = "bar", r3 = "violin"),
    column_annotation_params = list(
      r1 = list(height = grid::unit(10, "mm"), show_legend = FALSE),
      r3 = list(height = grid::unit(18, "mm"))
    ),
    row_annotation = c("q", "p2", "a"),
    row_annotation_side = "right",
    row_annotation_type = list(q = "pie", p2 = "density", a = "simple"),
    row_annotation_params = list(q = list(width = grid::unit(12, "mm"))),
    show_row_names = TRUE, show_column_names = TRUE
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  Heatmap(heatmap_long,
    values_by = "value", rows_by = "r", columns_by = "c",
    split_by = "p", palette = list(x = "Reds", y = "Blues")
  )
}


if (requireNamespace("cluster", quietly = TRUE)) {
  # implies in_form = "wide-rows"
  Heatmap(heatmap_long, rows_by = c("p1", "p2"), columns_by = "c")
}


if (requireNamespace("cluster", quietly = TRUE)) {
  # implies wide-columns
  Heatmap(heatmap_long, rows_by = "r", columns_by = c("p1", "p2"))
}


ChordPlot

set.seed(8525)
chord_data <- data.frame(
  nodes1 = sample(c("Source1", "Source2", "Source3"), 10, replace = TRUE),
  nodes2 = sample(letters[1:3], 10, replace = TRUE),
  y = sample(1:5, 10, replace = TRUE)
)

# Basic chord plot
ChordPlot(chord_data, from = "nodes1", to = "nodes2")

# With rotated labels and colored by target
ChordPlot(chord_data,
  from = "nodes1", to = "nodes2",
  links_color = "to", labels_rot = TRUE
)

# With values
ChordPlot(chord_data, from = "nodes1", to = "nodes2", y = "y")

# Split by variable
ChordPlot(chord_data, from = "nodes1", to = "nodes2", split_by = "y")

# With custom palettes per split
ChordPlot(chord_data,
  from = "nodes1", to = "nodes2", split_by = "y",
  palette = c("1" = "Reds", "2" = "Blues", "3" = "Greens")
)

# Flip source and target
ChordPlot(chord_data, from = "nodes1", to = "nodes2", flip = TRUE)

VennDiagram

set.seed(8525)
venn_sets <- list(
  A = sort(sample(letters, 8)),
  B = sort(sample(letters, 8)),
  C = sort(sample(letters, 8)),
  D = sort(sample(letters, 8))
)

VennDiagram(venn_sets)
VennDiagram(venn_sets, fill_mode = "set")
VennDiagram(venn_sets, label = "both")
VennDiagram(venn_sets, palette = "material-indigo", alpha = 0.6)

CorPlot

data(iris)

# Basic correlation plot with grouping
CorPlot(iris, x = "Sepal.Length", y = "Sepal.Width", group_by = "Species")


# With custom annotations and positioning
CorPlot(iris,
  x = "Sepal.Length", y = "Sepal.Width",
  group_by = "Species",
  anno_items = c("n", "eq", "r2", "pearson"),
  anno_position = "bottomright"
)


# With highlighting specific points
CorPlot(iris,
  x = "Sepal.Length", y = "Sepal.Width",
  group_by = "Species",
  highlight = 'Species == "setosa"',
  highlight_color = "red",
  highlight_size = 3,
  highlight_stroke = 1.5
)


# With faceting by groups
CorPlot(iris,
  x = "Sepal.Length", y = "Sepal.Width",
  facet_by = "Species",
  facet_scales = "free",
  add_smooth = TRUE,
  smooth_color = "blue"
)


# With splitting and custom palettes
CorPlot(iris,
  x = "Sepal.Length", y = "Sepal.Width",
  split_by = "Species",
  palette = c(setosa = "Set1", versicolor = "Dark2", virginica = "Paired"),
  combine = TRUE
)


Session Info

sessionInfo()
#> R version 4.5.2 (2025-10-31)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.3 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
#>  [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
#>  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
#> [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
#> 
#> time zone: UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] dplyr_1.2.0   ggplot2_4.0.2 ggforge_1.0.1
#> 
#> loaded via a namespace (and not attached):
#>  [1] tidyselect_1.2.1      farver_2.1.2          S7_0.2.1             
#>  [4] fastmap_1.2.0         tweenr_2.0.3          digest_0.6.39        
#>  [7] lifecycle_1.0.5       cluster_2.1.8.1       Cairo_1.7-0          
#> [10] survival_3.8-3        magrittr_2.0.4        compiler_4.5.2       
#> [13] rlang_1.1.7           sass_0.4.10           tools_4.5.2          
#> [16] igraph_2.2.2          yaml_2.3.12           knitr_1.51           
#> [19] ggsignif_0.6.4        labeling_0.4.3        RColorBrewer_1.1-3   
#> [22] abind_1.4-8           withr_3.0.2           purrr_1.2.1          
#> [25] BiocGenerics_0.56.0   desc_1.4.3            stats4_4.5.2         
#> [28] grid_4.5.2            polyclip_1.10-7       ggpubr_0.6.3         
#> [31] colorspace_2.1-2      scales_1.4.0          iterators_1.0.14     
#> [34] MASS_7.3-65           ggridges_0.5.7        dichromat_2.0-0.1    
#> [37] cli_3.6.5             rmarkdown_2.30        crayon_1.5.3         
#> [40] ragg_1.5.0            generics_0.1.4        otel_0.2.0           
#> [43] rjson_0.2.23          cachem_1.1.0          ggforce_0.5.0        
#> [46] stringr_1.6.0         splines_4.5.2         parallel_4.5.2       
#> [49] matrixStats_1.5.0     vctrs_0.7.1           Matrix_1.7-4         
#> [52] jsonlite_2.0.0        carData_3.0-6         car_3.1-5            
#> [55] IRanges_2.44.0        GetoptLong_1.1.0      patchwork_1.3.2      
#> [58] S4Vectors_0.48.0      rstatix_0.7.3         ggrepel_0.9.7        
#> [61] clue_0.3-67           Formula_1.2-5         magick_2.9.1         
#> [64] systemfonts_1.3.1     foreach_1.5.2         ggnewscale_0.5.2     
#> [67] tidyr_1.3.2           jquerylib_0.1.4       glue_1.8.0           
#> [70] pkgdown_2.2.0         codetools_0.2-20      shape_1.4.6.1        
#> [73] stringi_1.8.7         gtable_0.3.6          ComplexHeatmap_2.26.1
#> [76] tibble_3.3.1          pillar_1.11.1         htmltools_0.5.9      
#> [79] circlize_0.4.17       R6_2.6.1              textshaping_1.0.4    
#> [82] doParallel_1.0.17     evaluate_1.0.5        lattice_0.22-7       
#> [85] png_0.1-8             backports_1.5.0       broom_1.0.12         
#> [88] bslib_0.10.0          Rcpp_1.1.1            nlme_3.1-168         
#> [91] mgcv_1.9-3            xfun_0.56             fs_1.6.6             
#> [94] zoo_1.8-15            forcats_1.0.1         pkgconfig_2.0.3      
#> [97] GlobalOptions_0.1.3