plot_with_custom_legend: Add custom legend with brackets to a ggplot

View source: R/custom_legend_with_brackets.R

plot_with_custom_legendR Documentation

Add custom legend with brackets to a ggplot

Description

Remove ggplot2's legend and add a custom legend with brackets

Usage

plot_with_custom_legend(
  plot,
  labels,
  colors,
  comparisons = NULL,
  legend_x = 0.05,
  legend_y = 0.95,
  legend_width = 0.25,
  title = NULL
)

Arguments

plot

A ggplot object

labels

Character vector of group names (in order)

colors

Character vector of colors matching the groups

comparisons

Data frame with columns: group1, group2, label

legend_x

X position (0-1)

legend_y

Y position (0-1)

legend_width

Width of legend

title

Legend title

Value

A ggplot object (classes "gg" and "ggplot") with the vbracket legend incorporated. The plot's default legend is typically suppressed and a custom vbracket legend showing statistical comparison brackets is added.

Examples


library(ggplot2)
data <- data.frame(x = 1:10, y = 1:10, group = rep(c("A", "B"), each = 5))
p <- ggplot(data, aes(x, y, color = group)) + geom_line()
comps <- add_bracket_comparisons(groups1 = "A", groups2 = "B", labels = "*")
plot_with_custom_legend(p, c("A", "B"), c("red", "blue"), comps)


vbracket documentation built on Jan. 7, 2026, 9:06 a.m.