Skip to content

Commit fbe4923

Browse files
committed
v0.2.0, added more positions for letters, etc
Update NEWS.md
1 parent 768dc97 commit fbe4923

File tree

15 files changed

+146
-70
lines changed

15 files changed

+146
-70
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: ggtukey
22
Title: Compact Letter Displays for 'ggplot2'
3-
Version: 0.1.0
3+
Version: 0.2.0
44
Authors@R:
55
person("Ethan", "Bass", , "ethanbass@gmail.com", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0002-6175-6739"))
@@ -10,7 +10,7 @@ Description: Provides a simple interface to visualize paired comparisons in 'ggp
1010
License: GPL (>= 3)
1111
Encoding: UTF-8
1212
Roxygen: list(markdown = TRUE)
13-
RoxygenNote: 7.2.1
13+
RoxygenNote: 7.2.3
1414
Requires: R >= 4.1.0
1515
Imports:
1616
dplyr,

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import(multcompView)
1313
import(rlang)
1414
importFrom(dplyr,left_join)
1515
importFrom(magrittr,"%>%")
16+
importFrom(rlang,as_name)
1617
importFrom(stats,IQR)
1718
importFrom(stats,TukeyHSD)
1819
importFrom(stats,aov)
1920
importFrom(stats,as.formula)
2021
importFrom(stats,median)
2122
importFrom(stats,quantile)
2223
importFrom(stats,reorder)
24+
importFrom(stats,sd)
2325
importFrom(tidyr,drop_na)

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# ggtukey 0.2.0
2+
3+
* Added additional options to `where` parameter for placement of letters.
4+
* Allowed numeric arguments to `type` argument in `geom_tukey`.
5+
* Added citation file.
6+
* Changed `vjust` default to `-0.2`.
7+
18
# ggtukey 0.1.0
29

310
* Added a `NEWS.md` file to track changes to the package.

R/boxplot_letters.R

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
#' test (with an interaction term between \code{x} and \code{group}). Defaults
2020
#' to \code{two-way}. This argument only applies if the Tukey test is selected,
2121
#' since there is no two-way Kruskal-Wallis test.
22-
#' @param where Where to place the letters. Either above the box (\code{box}),
23-
#' above the upper whisker (\code{whisker}).
22+
#' @param where Where to put the letters. Either above the box (\code{box}) or
23+
#' upper whisker (\code{whisker}) of a boxplot; at the \code{mean} or
24+
#' \code{median}; or at the top of the error bars calculated from the standard
25+
#' error (\code{se}), standard deviation \code{sd}, or 95% confidence intervals
26+
#' returned by \code{\link[Hmisc]{smean.cl.normal}}, or \code{\link[Hmisc]{smean.cl.boot}}.
2427
#' @param raw Whether to plot raw data and (if so), how. The current options are
2528
#' \code{none}, \code{\link[ggplot2]{geom_point}}, \code{\link[ggplot2]{geom_dotplot}}, or
2629
#' \code{\link[ggplot2]{geom_jitter}}.
@@ -41,7 +44,8 @@
4144
#' @importFrom stats TukeyHSD aov as.formula median quantile reorder
4245
#' @import rlang
4346
#' @author Ethan Bass
44-
#' @note Adapted from https://www.mathiasecology.com/code/add-tukeys-significant-letters-to-ggplots
47+
#' @note Adapted from a helpful \href{https://www.mathiasecology.com/code/add-tukeys-significant-letters-to-ggplots}{blog post}
48+
#' by \href{https://github.com/justinmathias}{Justin Mathias}.
4549
#' @return Returns the specified plot as a \code{ggplot} object.
4650
#' @examples
4751
#' set.seed(1)
@@ -53,14 +57,17 @@
5357
#' @export
5458

5559
boxplot_letters <- function(data, x, y, fill, group, test = c("tukey", "kruskalmc"),
56-
type=c("two-way", "one-way"), where = c("box","whisker"),
60+
type=c("two-way", "one-way"),
61+
where = c("box","whisker", "mean", "median",
62+
"se", "sd", "cl_normal", "cl_boot"),
5763
raw = c('none', 'points', 'dots', 'jitter'),
58-
pt_col = "slategray", ..., hjust=0, vjust=0,
64+
pt_col = "slategray", ..., hjust = 0, vjust = -0.2,
5965
lab_size = 4, na.rm = TRUE, threshold = 0.05){
6066
test <- match.arg(test, c("tukey", "kruskalmc"))
6167
raw <- match.arg(raw, c('none', 'points', 'dots', 'jitter'))
6268
type <- match.arg(type, c("two-way","one-way"))
63-
where <- match.arg(where, c("box","whisker"))
69+
where <- match.arg(where, c("box","whisker", "mean", "median",
70+
"se", "sd", "cl_normal", "cl_boot"))
6471
x.s <- deparse(substitute(x))
6572
y.s <- deparse(substitute(y))
6673

R/geom_tukey.R

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
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 where Where to put the letters. Either above the box (\code{box}),
9-
#' above the upper whisker (\code{whisker}), or at the \code{mean} or
10-
#' \code{median}.
11-
#' @param hjust Horizontal adjustment of the label. Argument to \code{\link[ggplot2]{geom_text}}.
12-
#' @param vjust Vertical adjustment of the label. Argument to \code{\link[ggplot2]{geom_text}}.
8+
#' @param where Where to put the letters. Either above the box (\code{box}) or
9+
#' upper whisker (\code{whisker}) of a boxplot; at the \code{mean} or
10+
#' \code{median}; or at the top of the error bars calculated from the standard
11+
#' error (\code{se}), standard deviation \code{sd}, or 95% confidence intervals
12+
#' returned by \code{\link[Hmisc]{smean.cl.normal}}, or \code{\link[Hmisc]{smean.cl.boot}}.
13+
#' @param hjust Horizontal adjustment of the label. (Argument to \code{\link[ggplot2]{geom_text}}).
14+
#' @param vjust Vertical adjustment of the label. (Argument to \code{\link[ggplot2]{geom_text}}).
1315
#' @param size Label size. Argument to \code{\link[ggplot2]{geom_text}}.
1416
#' @param na.rm Logical. Whether to remove observations with NAs for the provided
1517
#' factors (i.e. \code{x} and \code{group}) before plotting. Defaults to TRUE.
@@ -26,10 +28,11 @@
2628
#' * Graves S, Piepho H, Dorai-Raj LSwhfS (2019). multcompView: Visualizations
2729
#' of Paired Comparisons. R package version 0.1-8. \url{https://CRAN.R-project.org/package=multcompView}
2830
#'
29-
#' @note Thank you to Hiroaki Yutani (\url{https://yutani.rbind.io/post/2017-11-07-ggplot-add/})
30-
#' and Simon P Couch (\url{https://www.simonpcouch.com/blog/ggplot-pipe-plus/})
31-
#' for a couple of very helpful blog posts describing the \code{\link[ggplot2]{ggplot_add}}
32-
#' syntax.
31+
#' @note Thank you to \href{https://github.com/yutannihilation}{Hiroaki Yutani}
32+
#' and \href{https://github.com/simonpcouch}{Simon P. Couch} for a couple of
33+
#' very helpful blog posts (\href{https://yutani.rbind.io/post/2017-11-07-ggplot-add/}{1},
34+
#' \href{https://www.simonpcouch.com/blog/ggplot-pipe-plus/}{2}) describing the
35+
#' \code{\link[ggplot2]{ggplot_add}} syntax.
3336
#' @examples
3437
#' library(ggplot2)
3538
#' set.seed(1)
@@ -42,13 +45,18 @@
4245

4346
geom_tukey <- function(test = c("tukey","kruskalmc"), threshold = 0.05,
4447
type=c("two-way", "one-way"),
45-
where = c("box","whisker", "mean", "median"),
46-
hjust=0, vjust=0, size = 4, na.rm = TRUE){
48+
where = c("box","whisker", "mean", "median", "se", "sd",
49+
"cl_normal", "cl_boot"),
50+
hjust = 0, vjust = -0.2, size = 4, na.rm = TRUE){
4751
# store inputs in classed output that can
4852
# be passed to a `ggplot_add` method
4953
test <- match.arg(test, c("tukey", "kruskalmc"))
54+
if (is.numeric(type)){
55+
type <- switch(type, "1"="one-way", "2"="two-way")
56+
}
5057
type <- match.arg(type, c("two-way", "one-way"))
51-
where <- match.arg(where, c("box","whisker", "mean", "median"))
58+
where <- match.arg(where, c("box", "whisker", "mean", "median",
59+
"se", "sd","cl_normal","cl_boot"))
5260
if (test == "kruskalmc"){
5361
type <- "one-way"
5462
}
@@ -71,8 +79,8 @@ geom_tukey <- function(test = c("tukey","kruskalmc"), threshold = 0.05,
7179
#' @noRd
7280
geom_tukey_ <- function(p, test = c("tukey","kruskalmc"), threshold = 0.05,
7381
type=c("two-way", "one-way"),
74-
where = c("box","whisker", "mean","median"),
75-
hjust=0, vjust=0, size = 4, na.rm = TRUE) {
82+
where = c("box","whisker", "mean","median", "se", "sd","cl_normal","cl_boot"),
83+
hjust = 0, vjust = 0, size = 4, na.rm = TRUE) {
7684
data <- p$data
7785
if (na.rm){
7886
data <- drop_na(data, !!p$mapping$x, !!p$facet$params$facets[[1]])
@@ -85,7 +93,7 @@ geom_tukey_ <- function(p, test = c("tukey","kruskalmc"), threshold = 0.05,
8593
data <- get_tukey_letters(data = data,
8694
x = c(p$mapping$x, p$facet$params$facets[[1]]),
8795
y= p$mapping$y, test = test,
88-
where = where,type = type,
96+
where = where, type = type,
8997
threshold = threshold)
9098
} else if (type == "one-way"){
9199
data <- get_tukey_letters(data = data,

R/utils.R

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22
#' Do Tukey Test and calculate letter placement
33
#' @importFrom stats TukeyHSD aov as.formula median quantile reorder
44
#' @importFrom dplyr left_join
5+
#' @importFrom rlang as_name
56
#' @param data A data.frame in long format
67
#' @param x Independent variable or vector of variables to plot on x axis
78
#' @param y Response variable to plot on y axis
89
#' @param test Which test to run for pairwise comparisons. Either \code{tukey}
910
#' (\code{\link[stats]{TukeyHSD}}) or \code{\link[pgirmess]{kruskalmc}}. Defaults
1011
#' to \code{tukey}.
11-
#' @param where Where to put the letters. Either above the box (\code{box}) or
12-
#' above the upper whisker (\code{whisker}).
12+
#' @param where Where to put the letters. Either above the box (\code{box}),
13+
#' above the upper whisker (\code{whisker}), or at the \code{mean} or
14+
#' \code{median}.
1315
#' @param threshold Statistical threshold for significance. Defaults to 0.05.
1416
#' @noRd
1517

16-
get_tukey_letters <- function(data, x, y, group=NULL, test = c("tukey", "kruskalmc"),
18+
get_tukey_letters <- function(data, x, y, group = NULL, test = c("tukey", "kruskalmc"),
1719
type = c("two-way", "one-way"),
18-
where=c("box", "whisker", "mean", "median"),
20+
where = c("box", "whisker", "mean",
21+
"median", "se", "sd", "cl_normal","cl_boot"),
1922
threshold = 0.05){
2023
test <- match.arg(test, c("tukey", "kruskalmc"))
21-
where <- match.arg(where, c("box", "whisker", "mean","median"))
24+
where <- match.arg(where, c("box", "whisker", "mean","median", "se", "sd",
25+
"cl_normal", "cl_boot"))
2226
type <- match.arg(type, c("two-way", "one-way"))
2327
if (inherits(x, "quosure") & is.null(group)){
2428
letters.df <- place_tukey_letters(data, as_name(x), as_name(y), test = test,
@@ -69,7 +73,11 @@ place_tukey_letters <- function(data, x, y, test = c("tukey", "kruskalmc"),
6973
"box" = get_quantile,
7074
"whisker" = get_whisker,
7175
"mean" = mean,
72-
"median" = median)
76+
"median" = median,
77+
"se" = get_sem,
78+
"sd" = get_sd,
79+
"cl_normal" = get_cl_normal,
80+
"cl_boot" = get_cl_boot)
7381
placement <- data %>% # Create a dataframe to assign the letter position.
7482
group_by(.data[[xlab]]) %>%
7583
summarise("Placement.Value" = placement_fnc(.data[[y]]))
@@ -80,6 +88,28 @@ place_tukey_letters <- function(data, x, y, test = c("tukey", "kruskalmc"),
8088
letters.df
8189
}
8290

91+
#' @importFrom stats sd
92+
#' @noRd
93+
get_sem <- function(x){
94+
mean(x) + sd(x)/sqrt(length(x))
95+
}
96+
97+
#' @importFrom stats sd
98+
#' @noRd
99+
get_sd <- function(x){
100+
mean(x) + sd(x)
101+
}
102+
103+
#' @noRd
104+
get_cl_normal <- function(x){
105+
Hmisc::smean.cl.normal(x)[[3]]
106+
}
107+
108+
#' @noRd
109+
get_cl_boot <- function(x){
110+
Hmisc::smean.cl.boot(x)[[3]]
111+
}
112+
83113
#' Check whether color specifications exists.
84114
#'
85115
#' @export

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ For more examples and details on different options for customization, please con
5454

5555
You can cite `ggtukey` as follows:
5656

57-
Bass, E. (2022). ggtukey: Compact Letter Displays for 'ggplot2' (v0.1.0). (https://ethanbass.github.io/ggtukey/).
57+
Bass, E. (2022). ggtukey: Compact Letter Displays for 'ggplot2' (v0.2.0). (https://ethanbass.github.io/ggtukey/).
5858

5959
# Further Reading
6060

inst/CITATION

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
citHeader("To cite ggtukey in publications, please use:")
2+
3+
citEntry(
4+
entry = "manual",
5+
title = "ggtukey: Compact Letter Displays for 'ggplot2'",
6+
author = "Ethan Bass",
7+
year = "2023",
8+
note = "version 0.2.0",
9+
url = "https://ethanbass.github.io/ggtukey/",
10+
doi = "",
11+
textVersion = paste("Bass, E. (2023).",
12+
"ggtukey: Compact Letter Displays for 'ggplot2' (version 0.2.0)."
13+
)
14+
)

man/boxplot_letters.Rd

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geom_tukey.Rd

Lines changed: 14 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)