55# ' @param type If a grouping variable is provided, determines whether to run
66# ' separate tests for each facet (\code{one-way}) or one (\code{two-way}) test with
77# ' an interaction term between \code{x} and \code{group}. Defaults to \code{two-way}.
8+ # ' @param threshold Statistical threshold for significance. Defaults to 0.05.
89# ' @param where Where to put the letters. Either above the box (\code{box}) or
910# ' upper whisker (\code{whisker}) of a boxplot; at the \code{mean} or
1011# ' \code{median}; or at the top of the error bars calculated from the standard
1112# ' error (\code{se}), standard deviation \code{sd}, or 95% confidence intervals
1213# ' returned by \code{\link[Hmisc]{smean.cl.normal}}, or \code{\link[Hmisc]{smean.cl.boot}}.
1314# ' @param hjust Horizontal adjustment of the label. (Argument to \code{\link[ggplot2]{geom_text}}).
1415# ' @param vjust Vertical adjustment of the label. (Argument to \code{\link[ggplot2]{geom_text}}).
16+ # ' @param geom Which geom to use to plot letters. Options are \code{text} and \code{label}.
1517# ' @param size Label size. Argument to \code{\link[ggplot2]{geom_text}}.
18+ # ' @param color Label color.
19+ # ' @param fill Label fill (only applies if \code{geom == "label"}).
20+ # ' @param alpha Label transparency. Defaults to 1.
1621# ' @param na.rm Logical. Whether to remove observations with NAs for the provided
1722# ' factors (i.e. \code{x} and \code{group}) before plotting. Defaults to TRUE.
18- # ' @param threshold Statistical threshold for significance. Defaults to 0.05.
1923# ' @author Ethan Bass
2024# ' @references
2125# ' * Piepho, Hans-Peter. An Algorithm for a Letter-Based Representation of
4347# ' data |> ggplot(aes(x=Size, y=Value)) + geom_boxplot() + facet_wrap(~Category) + geom_tukey()
4448# ' @export
4549
46- geom_tukey <- function (test = c(" tukey" ," kruskalmc" ), threshold = 0.05 ,
47- type = c(" two-way" , " one-way" ),
50+ geom_tukey <- function (test = c(" tukey" ," kruskalmc" ),
51+ type = c(" two-way" , " one-way" ), threshold = 0.05 ,
4852 where = c(" box" ," whisker" , " mean" , " median" , " se" , " sd" ,
4953 " cl_normal" , " cl_boot" ),
50- hjust = 0 , vjust = - 0.2 , size = 4 , na.rm = TRUE ){
54+ hjust = 0 , vjust = - 0.2 , geom = " text" ,
55+ size = 4 , color = " black" , fill = " white" ,
56+ alpha = 1 , na.rm = TRUE ){
5157 # store inputs in classed output that can
5258 # be passed to a `ggplot_add` method
5359 test <- match.arg(test , c(" tukey" , " kruskalmc" ))
@@ -71,7 +77,11 @@ geom_tukey <- function(test = c("tukey","kruskalmc"), threshold = 0.05,
7177 vjust = vjust ,
7278 size = size ,
7379 na.rm = na.rm ,
74- threshold = threshold
80+ threshold = threshold ,
81+ color = color ,
82+ geom = geom ,
83+ alpha = alpha ,
84+ fill = fill
7585 )
7686}
7787
@@ -80,7 +90,8 @@ geom_tukey <- function(test = c("tukey","kruskalmc"), threshold = 0.05,
8090geom_tukey_ <- function (p , test = c(" tukey" ," kruskalmc" ), threshold = 0.05 ,
8191 type = c(" two-way" , " one-way" ),
8292 where = c(" box" ," whisker" , " mean" ," median" , " se" , " sd" ," cl_normal" ," cl_boot" ),
83- hjust = 0 , vjust = 0 , size = 4 , na.rm = TRUE ) {
93+ hjust = 0 , vjust = 0 , size = 4 , geom = " text" ,
94+ color = " black" , fill = " white" , alpha = 1 , na.rm = TRUE ) {
8495 data <- p $ data
8596 if (na.rm ){
8697 data <- drop_na(data , !! p $ mapping $ x , !! p $ facet $ params $ facets [[1 ]])
@@ -104,9 +115,18 @@ geom_tukey_ <- function(p, test = c("tukey","kruskalmc"), threshold = 0.05,
104115 threshold = threshold )
105116 }
106117 }
107- geom_text(data = data ,
108- aes(y = .data $ Placement.Value , label = .data $ Letter ), hjust = hjust ,
109- vjust = vjust , size = size )
118+ if (geom == " text" ){
119+ geom_text(data = data ,
120+ aes(y = .data $ Placement.Value , label = .data $ Letter ), hjust = hjust ,
121+ vjust = vjust , size = size , color = color , alpha = alpha )
122+ } else if (geom == " label" ){
123+ geom_label(data = data ,
124+ aes(y = .data $ Placement.Value , label = .data $ Letter ), hjust = hjust ,
125+ vjust = vjust , size = size , color = color , alpha = alpha ,
126+ label.size = 0 , fill = fill )
127+ }
128+
129+
110130}
111131
112132# ' @name ggplot_add.geom_tukey
0 commit comments