View source: R/custom_legend_with_brackets.R
| plot_with_custom_legend | R Documentation |
Remove ggplot2's legend and add a custom legend with brackets
plot_with_custom_legend(
plot,
labels,
colors,
comparisons = NULL,
legend_x = 0.05,
legend_y = 0.95,
legend_width = 0.25,
title = NULL
)
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 |
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.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.