| Title: | Compact Letter Displays for Multiple Comparisons in 'ggplot2' |
|---|---|
| Description: | Provides a simple interface to visualize paired comparisons in 'ggplot2' by adding compact letter displays (i.e. Tukey letters). |
| Authors: | Ethan Bass [aut, cre] (ORCID: <https://orcid.org/0000-0002-6175-6739>) |
| Maintainer: | Ethan Bass <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.5.0 |
| Built: | 2026-05-29 09:14:58 UTC |
| Source: | https://github.com/ethanbass/ggtukey |
Performs pairwise comparisons using TukeyHSD and produces
boxplots with compact letter display showing significance pairwise differences.
Letters are produced by multcompLetters. Plots are
produced by ggplot2. Raw data can also be overlaid in
various ways according to the value of raw.
boxplot_letters( data, x, y, fill, group, test = c("tukey", "kruskalmc", "dunn"), type = c("two-way", "one-way"), where = c("box", "whisker", "mean", "median", "se", "sd", "cl_normal", "cl_boot"), raw = c("none", "points", "dots", "jitter"), pt_col = "slategray", hjust = 0, vjust = -0.2, lab_size = 4, na.rm = TRUE, threshold = 0.05, reversed = FALSE, ... )boxplot_letters( data, x, y, fill, group, test = c("tukey", "kruskalmc", "dunn"), type = c("two-way", "one-way"), where = c("box", "whisker", "mean", "median", "se", "sd", "cl_normal", "cl_boot"), raw = c("none", "points", "dots", "jitter"), pt_col = "slategray", hjust = 0, vjust = -0.2, lab_size = 4, na.rm = TRUE, threshold = 0.05, reversed = FALSE, ... )
data |
A data.frame in "long" format. |
x |
variable to plot on x axis. |
y |
variable to plot on y axis. |
fill |
column or color to fill boxplots |
group |
A grouping variable (to allow faceting). |
test |
Which test to run for pairwise comparisons. Either |
type |
If a grouping variable is provided, determines whether to run
separate tests for each facet ( |
where |
Where to put the letters. Either above the box ( |
raw |
Whether to plot raw data and (if so), how. The current options are
|
pt_col |
Color of points, if raw data is plotted.
|
hjust |
Horizontal adjustment of label. Argument to |
vjust |
Vertical adjustment of label. Argument to |
lab_size |
Label size. Argument to |
na.rm |
Logical. Whether to remove observations with NAs for the provided
factors (i.e. |
threshold |
Statistical threshold for significance. Defaults to 0.05. |
reversed |
Logical. Argument to
|
... |
Additional arguments to |
Allows group variable for faceting
Returns the specified plot as a ggplot object.
Adapted from a helpful blog post by Justin Mathias.
Ethan Bass
set.seed(1) data <- data.frame("Category" = c(rep("Low", 10), rep("Medium", 10), rep("High", 10)), "Value" = c(rnorm(10, 5), rnorm(10, 5.5), rnorm(10, 10)), "Size" = c("Big","Small")) boxplot_letters(data, Category, Value) boxplot_letters(data, x=Category, y=Value, group=Size)set.seed(1) data <- data.frame("Category" = c(rep("Low", 10), rep("Medium", 10), rep("High", 10)), "Value" = c(rnorm(10, 5), rnorm(10, 5.5), rnorm(10, 10)), "Size" = c("Big","Small")) boxplot_letters(data, Category, Value) boxplot_letters(data, x=Category, y=Value, group=Size)
Create Compact Letter Display Layer Groups with at least one letter in common are not significantly different.
geom_tukey( test = c("tukey", "kruskalmc", "dunn"), type = c("two-way", "one-way"), threshold = 0.05, where = c("box", "whisker", "mean", "median", "se", "sd", "cl_normal", "cl_boot"), hjust = 0, vjust = -0.2, geom = "text", size = 4, color = "black", fill = "white", alpha = 1, na.rm = TRUE, reversed = FALSE )geom_tukey( test = c("tukey", "kruskalmc", "dunn"), type = c("two-way", "one-way"), threshold = 0.05, where = c("box", "whisker", "mean", "median", "se", "sd", "cl_normal", "cl_boot"), hjust = 0, vjust = -0.2, geom = "text", size = 4, color = "black", fill = "white", alpha = 1, na.rm = TRUE, reversed = FALSE )
test |
Which test to run for pairwise comparisons. Either |
type |
If a grouping variable is provided, determines whether to run
separate tests for each facet ( |
threshold |
Statistical threshold for significance. Defaults to 0.05. |
where |
Where to put the letters. Either above the box ( |
hjust |
Horizontal adjustment of the label. (Argument to
|
vjust |
Vertical adjustment of the label. (Argument to
|
geom |
Which geom to use to plot letters. Options are |
size |
Label size. Argument to |
color |
Label color. |
fill |
Label fill (only applies if |
alpha |
Label transparency. Defaults to 1. |
na.rm |
Logical. Whether to remove observations with NAs for the provided
factors (i.e. |
reversed |
Logical. Argument to
|
Thank you to Hiroaki Yutani
and Simon P. Couch for a couple of
very helpful blog posts (1,
2) describing the
ggplot_add syntax.
Ethan Bass
Piepho, Hans-Peter. An Algorithm for a Letter-Based Representation of All-Pairwise Comparisons. Journal of Computational and Graphical Statistics 13, no. 2 (June 1, 2004): 456–66. doi:10.1198/1061860043515.
Piepho, Hans-Peter. “Letters in Mean Comparisons: What They Do and Don’t Mean.” Agronomy Journal 110, no. 2 (2018): 431–34. doi:10.2134/agronj2017.10.0580
Graves S, Piepho H, Dorai-Raj LSwhfS (2019). multcompView: Visualizations of Paired Comparisons. R package version 0.1-8. https://CRAN.R-project.org/package=multcompView
library(ggplot2) set.seed(1) data <- data.frame("Category" = c(rep("Low", 10), rep("Medium", 10), rep("High", 10)), "Value" = c(rnorm(10, 5), rnorm(10, 5.5), rnorm(10, 10)), "Size" = c("Big","Small")) data |> ggplot(aes(x=Category, y=Value)) + geom_boxplot() + facet_wrap(~Size) + geom_tukey() data |> ggplot(aes(x=Size, y=Value)) + geom_boxplot() + facet_wrap(~Category) + geom_tukey()library(ggplot2) set.seed(1) data <- data.frame("Category" = c(rep("Low", 10), rep("Medium", 10), rep("High", 10)), "Value" = c(rnorm(10, 5), rnorm(10, 5.5), rnorm(10, 10)), "Size" = c("Big","Small")) data |> ggplot(aes(x=Category, y=Value)) + geom_boxplot() + facet_wrap(~Size) + geom_tukey() data |> ggplot(aes(x=Size, y=Value)) + geom_boxplot() + facet_wrap(~Category) + geom_tukey()
Function to check whether all specified colors are actual colors.
is.color(x, return.colors = FALSE)is.color(x, return.colors = FALSE)
x |
Vector of any of the three kinds of R color specifications,
i.e., either a color name (as listed by
|
return.colors |
Logical: logical values (FALSE, default) or returning colors (TRUE) |
Logical value (or colors)
Adapted from plotfunctions packaage https://cran.r-project.org/web/packages/plotfunctions/index.html
Jacolien van Rij
# correct color definitions: is.color(c('#FF0000FF', '#00FF00FF', '#0000FFFF')) is.color(c('red', 'steelblue', 'green3')) is.color(c(1,7,28)) # mixtures are possible too: is.color(c('#FF0000FF', 'red', 1, '#FF0000', rgb(.1,0,0))) # return colors: # note that 28 is converted to 4... is.color(c(1,7,28), return.colors=TRUE) is.color(c('#FF0000CC', 'red', 1, '#FF0000'), return.colors=TRUE) # 4 incorrect colors, 1 correct: test <- c('#FH0000', 3, '#FF00991', 'lavendel', '#AABBCCFFF') is.color(test) is.color(test, return.colors=TRUE)# correct color definitions: is.color(c('#FF0000FF', '#00FF00FF', '#0000FFFF')) is.color(c('red', 'steelblue', 'green3')) is.color(c(1,7,28)) # mixtures are possible too: is.color(c('#FF0000FF', 'red', 1, '#FF0000', rgb(.1,0,0))) # return colors: # note that 28 is converted to 4... is.color(c(1,7,28), return.colors=TRUE) is.color(c('#FF0000CC', 'red', 1, '#FF0000'), return.colors=TRUE) # 4 incorrect colors, 1 correct: test <- c('#FH0000', 3, '#FF00991', 'lavendel', '#AABBCCFFF') is.color(test) is.color(test, return.colors=TRUE)