Skip to content

Commit

Permalink
Check with devel statsExpressions (#885)
Browse files Browse the repository at this point in the history
* use devel statsExpressions

* minor cleanup

* fix example

* YAGNI grouped_list helper
  • Loading branch information
IndrajeetPatil authored Sep 24, 2023
1 parent 8cd6875 commit 71c8ab1
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 115 deletions.
2 changes: 1 addition & 1 deletion API
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extract_stats(p, ...)
extract_subtitle(p)
ggbarstats(data, x, y, counts = NULL, type = "parametric", paired = FALSE, results.subtitle = TRUE, label = "percentage", label.args = list(alpha = 1, fill = "white"), sample.size.label.args = list(size = 4), k = 2L, proportion.test = results.subtitle, perc.k = 0L, bf.message = TRUE, ratio = NULL, conf.level = 0.95, sampling.plan = "indepMulti", fixed.margin = "rows", prior.concentration = 1, title = NULL, subtitle = NULL, caption = NULL, legend.title = NULL, xlab = NULL, ylab = NULL, ggtheme = ggstatsplot::theme_ggstatsplot(), package = "RColorBrewer", palette = "Dark2", ggplot.component = NULL, ...)
ggbetweenstats(data, x, y, type = "parametric", pairwise.display = "significant", p.adjust.method = "holm", effsize.type = "unbiased", bf.prior = 0.707, bf.message = TRUE, results.subtitle = TRUE, xlab = NULL, ylab = NULL, caption = NULL, title = NULL, subtitle = NULL, k = 2L, var.equal = FALSE, conf.level = 0.95, nboot = 100L, tr = 0.2, centrality.plotting = TRUE, centrality.type = type, centrality.point.args = list(size = 5, color = "darkred"), centrality.label.args = list(size = 3, nudge_x = 0.4, segment.linetype = 4, min.segment.length = 0), point.args = list(position = ggplot2::position_jitterdodge(dodge.width = 0.6), alpha = 0.4, size = 3, stroke = 0, na.rm = TRUE), boxplot.args = list(width = 0.3, alpha = 0.2, na.rm = TRUE), violin.args = list(width = 0.5, alpha = 0.2, na.rm = TRUE), ggsignif.args = list(textsize = 3, tip_length = 0.01, na.rm = TRUE), ggtheme = ggstatsplot::theme_ggstatsplot(), package = "RColorBrewer", palette = "Dark2", ggplot.component = NULL, ...)
ggcoefstats(x, statistic = NULL, conf.int = TRUE, conf.level = 0.95, k = 2L, exclude.intercept = FALSE, effectsize.type = "eta", meta.analytic.effect = FALSE, meta.type = "parametric", bf.message = TRUE, sort = "none", xlab = NULL, ylab = NULL, title = NULL, subtitle = NULL, caption = NULL, only.significant = FALSE, point.args = list(size = 3, color = "blue", na.rm = TRUE), errorbar.args = list(height = 0, na.rm = TRUE), vline = TRUE, vline.args = list(linewidth = 1, linetype = "dashed"), stats.labels = TRUE, stats.label.color = NULL, stats.label.args = list(size = 3, direction = "y", min.segment.length = 0), package = "RColorBrewer", palette = "Dark2", ggtheme = ggstatsplot::theme_ggstatsplot(), ...)
ggcoefstats(x, statistic = NULL, conf.int = TRUE, conf.level = 0.95, k = 2L, exclude.intercept = FALSE, effectsize.type = "eta", meta.analytic.effect = FALSE, meta.type = "parametric", bf.message = TRUE, sort = "none", xlab = NULL, ylab = NULL, title = NULL, subtitle = NULL, caption = NULL, only.significant = FALSE, point.args = list(size = 3, color = "blue", na.rm = TRUE), errorbar.args = list(height = 0, na.rm = TRUE), vline = TRUE, vline.args = list(linewidth = 1, linetype = "dashed"), stats.labels = TRUE, stats.label.color = NULL, stats.label.args = list(size = 3, direction = "y", min.segment.length = 0, na.rm = TRUE), package = "RColorBrewer", palette = "Dark2", ggtheme = ggstatsplot::theme_ggstatsplot(), ...)
ggcorrmat(data, cor.vars = NULL, cor.vars.names = NULL, matrix.type = "upper", type = "parametric", tr = 0.2, partial = FALSE, k = 2L, sig.level = 0.05, conf.level = 0.95, bf.prior = 0.707, p.adjust.method = "holm", pch = "cross", ggcorrplot.args = list(method = "square", outline.color = "black", pch.cex = 14), package = "RColorBrewer", palette = "Dark2", colors = c("#E69F00", "white", "#009E73"), ggtheme = ggstatsplot::theme_ggstatsplot(), ggplot.component = NULL, title = NULL, subtitle = NULL, caption = NULL, ...)
ggdotplotstats(data, x, y, xlab = NULL, ylab = NULL, title = NULL, subtitle = NULL, caption = NULL, type = "parametric", test.value = 0, bf.prior = 0.707, bf.message = TRUE, effsize.type = "g", conf.level = 0.95, tr = 0.2, k = 2L, results.subtitle = TRUE, point.args = list(color = "black", size = 3, shape = 16), centrality.plotting = TRUE, centrality.type = type, centrality.line.args = list(color = "blue", linewidth = 1, linetype = "dashed"), ggplot.component = NULL, ggtheme = ggstatsplot::theme_ggstatsplot(), ...)
gghistostats(data, x, binwidth = NULL, xlab = NULL, title = NULL, subtitle = NULL, caption = NULL, type = "parametric", test.value = 0, bf.prior = 0.707, bf.message = TRUE, effsize.type = "g", conf.level = 0.95, tr = 0.2, k = 2L, ggtheme = ggstatsplot::theme_ggstatsplot(), results.subtitle = TRUE, bin.args = list(color = "black", fill = "grey50", alpha = 0.7), centrality.plotting = TRUE, centrality.type = type, centrality.line.args = list(color = "blue", linewidth = 1, linetype = "dashed"), normal.curve = FALSE, normal.curve.args = list(linewidth = 2), ggplot.component = NULL, ...)
Expand Down
8 changes: 4 additions & 4 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ message: 'To cite package "ggstatsplot" in publications use:'
type: software
license: GPL-3.0-only
title: 'ggstatsplot: ''ggplot2'' Based Plots with Statistical Details'
version: 0.12.1
version: 0.12.1.9000
doi: 10.21105/joss.03167
abstract: 'Extension of ''ggplot2'', ''ggstatsplot'' creates graphics with details
from statistical tests included in the plots themselves. It provides an easier syntax
Expand Down Expand Up @@ -447,8 +447,8 @@ references:
given-names: Indrajeet
email: [email protected]
orcid: https://orcid.org/0000-0003-1995-6531
version: '>= 1.5.2'
year: '2023'
version: '>= 1.5.2.9000'
- type: software
title: tidyr
abstract: 'tidyr: Tidy Messy Data'
Expand Down Expand Up @@ -511,7 +511,7 @@ references:
given-names: Jeffrey N.
email: [email protected]
year: '2023'
version: '>= 0.9.12-4.4'
version: '>= 0.9.12-4.5'
- type: software
title: gapminder
abstract: 'gapminder: Data from Gapminder'
Expand Down Expand Up @@ -736,7 +736,7 @@ references:
given-names: Vaudor
email: [email protected]
year: '2023'
version: '>= 1.0.6'
version: '>= 1.0.7'
- type: software
title: withr
abstract: 'withr: Run Code ''With'' Temporarily Modified Global State'
Expand Down
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ Imports:
purrr (>= 1.0.2),
rlang,
stats,
statsExpressions (>= 1.5.2),
statsExpressions (>= 1.5.2.9000),
tidyr,
utils
Suggests:
afex,
BayesFactor (>= 0.9.12-4.4),
BayesFactor (>= 0.9.12-4.5),
gapminder,
knitr,
lme4,
Expand All @@ -67,9 +67,11 @@ Suggests:
survival,
testthat (>= 3.1.10),
tibble,
vdiffr (>= 1.0.6),
vdiffr (>= 1.0.7),
withr,
WRS2
Remotes:
IndrajeetPatil/statsExpressions
VignetteBuilder:
knitr
Encoding: UTF-8
Expand Down
8 changes: 4 additions & 4 deletions R/ggbarstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ ggbarstats <- function(data,
)

subtitle_df <- .eval_f(contingency_table, !!!.f.args, type = type)
subtitle <- extract_expression(subtitle_df)
subtitle <- .extract_expression(subtitle_df)

# Bayes Factor caption
if (type != "bayes" && bf.message && isFALSE(paired)) {
caption_df <- .eval_f(contingency_table, !!!.f.args, type = "bayes")
caption <- extract_expression(caption_df)
caption <- .extract_expression(caption_df)
}
}

Expand All @@ -124,13 +124,13 @@ ggbarstats <- function(data,
onesample_df <- onesample_data(data, {{ x }}, {{ y }}, k)

# if no. of factor levels is greater than the default palette color count
.palette_message(package, palette, nlevels(data %>% pull({{ x }})))
.is_palette_sufficient(package, palette, nlevels(data %>% pull({{ x }})))

# plot
plotBar <- ggplot(descriptive_df, aes({{ y }}, perc, fill = {{ x }})) +
geom_bar(stat = "identity", position = "fill", color = "black") +
scale_y_continuous(
labels = function(x) paste0(x * 100L, "%"),
labels = ~ insight::format_percent(., digits = 0L),
breaks = seq(from = 0.0, to = 1.0, by = 0.10),
minor_breaks = seq(from = 0.05, to = 0.95, by = 0.10)
) +
Expand Down
4 changes: 2 additions & 2 deletions R/ggbetweenstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ ggbetweenstats <- function(data,

.f <- .f_switch(test)
subtitle_df <- .eval_f(.f, !!!.f.args, type = type)
subtitle <- extract_expression(subtitle_df)
subtitle <- .extract_expression(subtitle_df)

if (type == "parametric" && bf.message) {
caption_df <- .eval_f(.f, !!!.f.args, type = "bayes")
caption <- extract_expression(caption_df)
caption <- .extract_expression(caption_df)
}
}

Expand Down
2 changes: 1 addition & 1 deletion R/ggbetweenstats_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
ggplot.component = NULL,
...) {
# if no. of factor levels is greater than the default palette color count
.palette_message(package, palette, nlevels(x))
.is_palette_sufficient(package, palette, nlevels(x))

plot +
labs(
Expand Down
27 changes: 15 additions & 12 deletions R/ggcoefstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ ggcoefstats <- function(x,
vline.args = list(linewidth = 1.0, linetype = "dashed"),
stats.labels = TRUE,
stats.label.color = NULL,
stats.label.args = list(size = 3.0, direction = "y", min.segment.length = 0),
stats.label.args = list(
size = 3.0,
direction = "y",
min.segment.length = 0,
na.rm = TRUE
),
package = "RColorBrewer",
palette = "Dark2",
ggtheme = ggstatsplot::theme_ggstatsplot(),
Expand All @@ -164,8 +169,7 @@ ggcoefstats <- function(x,
ci = conf.level,
table_wide = TRUE,
...
) %>%
rename_all(~ gsub("omega2.|eta2.", "", .x))
)

# anova objects need further cleaning
if (all(c("df", "df.error") %in% names(tidy_df))) tidy_df %<>% mutate(effectsize = paste0("partial ", effectsize.type, "-squared"))

Check warning on line 175 in R/ggcoefstats.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggcoefstats.R,line=175,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 135 characters.
Expand All @@ -178,7 +182,7 @@ ggcoefstats <- function(x,
}

# create a new term column if it's not present
if (!"term" %in% names(tidy_df)) tidy_df %<>% mutate(term = paste("term", row_number(), sep = "_"))
if (!"term" %in% names(tidy_df)) tidy_df %<>% mutate(term = paste0("term_", row_number()))

# check for duplicate terms and columns -------------------------

Expand Down Expand Up @@ -225,7 +229,7 @@ ggcoefstats <- function(x,

tidy_df %<>% parameters::sort_parameters(sort = sort, column = "estimate")

# `term` needs to be a factor column; otherwise, ggplot2 will sort the x-axis
# `term` needs to be a factor column; otherwise, ggplot2 will sort the `x`-axis
# labels alphabetically and terms won't appear in the expected order
tidy_df %<>% dplyr::mutate(term = factor(term, tidy_df$term))

Expand All @@ -235,22 +239,22 @@ ggcoefstats <- function(x,

if (!is.null(glance_df) && all(c("AIC", "BIC") %in% names(glance_df))) {
glance_df %<>% mutate(expression = list(parse(text = glue("list(AIC=='{format_value(AIC, 0L)}', BIC=='{format_value(BIC, 0L)}')"))))

Check warning on line 241 in R/ggcoefstats.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggcoefstats.R,line=241,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 136 characters.
caption <- glance_df$expression[[1L]]
caption <- .extract_expression(glance_df)
}

# meta analysis -------------------------

if (meta.analytic.effect) {
meta.type <- stats_type_switch(meta.type)

# results from frequentist random-effects meta-analysis
# frequentist
subtitle_df <- meta_analysis(tidy_df, type = meta.type, k = k)
subtitle <- subtitle_df$expression[[1L]]
subtitle <- .extract_expression(subtitle_df)

# results from Bayesian random-effects meta-analysis (only for parametric)
# Bayesian
if (meta.type == "parametric" && bf.message) {
caption_df <- suppressWarnings(meta_analysis(tidy_df, type = "bayes", k = k))
caption <- caption_df$expression[[1L]]
caption <- .extract_expression(caption_df)
}
}

Expand All @@ -276,7 +280,7 @@ ggcoefstats <- function(x,
# ggrepel labels -------------------------

if (stats.labels) {
if (is.null(stats.label.color) && .palette_message(package, palette, length(tidy_df$term))) {
if (is.null(stats.label.color) && .is_palette_sufficient(package, palette, length(tidy_df$term))) {
stats.label.color <- paletteer::paletteer_d(paste0(package, "::", palette), length(tidy_df$term))
}

Expand All @@ -287,7 +291,6 @@ ggcoefstats <- function(x,
mapping = aes(x = estimate, y = term, label = expression),
parse = TRUE,
color = stats.label.color %||% "black",
na.rm = TRUE,
!!!stats.label.args
)
}
Expand Down
4 changes: 2 additions & 2 deletions R/ggdotplotstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ ggdotplotstats <- function(data,
)

subtitle_df <- .eval_f(one_sample_test, !!!.f.args, type = type)
subtitle <- extract_expression(subtitle_df)
subtitle <- .extract_expression(subtitle_df)

if (type == "parametric" && bf.message) {
caption_df <- .eval_f(one_sample_test, !!!.f.args, type = "bayes")
caption <- extract_expression(caption_df)
caption <- .extract_expression(caption_df)
}
}

Expand Down
6 changes: 3 additions & 3 deletions R/gghistostats.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ gghistostats <- function(data,

# subtitle with statistical results
subtitle_df <- .eval_f(one_sample_test, !!!.f.args, type = type)
subtitle <- extract_expression(subtitle_df)
subtitle <- .extract_expression(subtitle_df)

# BF message
if (type == "parametric" && bf.message) {
caption_df <- .eval_f(one_sample_test, !!!.f.args, type = "bayes")
caption <- extract_expression(caption_df)
caption <- .extract_expression(caption_df)
}
}

Expand All @@ -131,7 +131,7 @@ gghistostats <- function(data,
scale_y_continuous(
sec.axis = sec_axis(
trans = ~ . / nrow(data),
labels = function(x) paste0(x * 100, "%"),
labels = function(x) insight::format_percent(x, digits = 0L),
name = "proportion"
)
) +
Expand Down
6 changes: 3 additions & 3 deletions R/ggpiestats.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ ggpiestats <- function(data,
)

subtitle_df <- .eval_f(contingency_table, !!!.f.args, type = type)
subtitle <- extract_expression(subtitle_df)
subtitle <- .extract_expression(subtitle_df)

# Bayes Factor caption
if (type != "bayes" && bf.message && isFALSE(paired)) {
caption_df <- .eval_f(contingency_table, !!!.f.args, type = "bayes")
caption <- extract_expression(caption_df)
caption <- .extract_expression(caption_df)
}
}

Expand All @@ -156,7 +156,7 @@ ggpiestats <- function(data,
if (test == "two.way") onesample_df <- onesample_data(data, {{ x }}, {{ y }}, k)

# if no. of factor levels is greater than the default palette color count
.palette_message(package, palette, min_length = x_levels)
.is_palette_sufficient(package, palette, min_length = x_levels)

# creating the basic plot
plotPie <- ggplot(descriptive_df, mapping = aes(x = "", y = perc)) +
Expand Down
4 changes: 2 additions & 2 deletions R/ggscatterstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ ggscatterstats <- function(data,
)

subtitle_df <- .eval_f(corr_test, !!!.f.args, type = type)
subtitle <- extract_expression(subtitle_df)
subtitle <- .extract_expression(subtitle_df)

# BF message for null hypothesis support
if (type == "parametric" && bf.message) {
caption_df <- .eval_f(corr_test, !!!.f.args, type = "bayes")
caption <- extract_expression(caption_df)
caption <- .extract_expression(caption_df)
}
}

Expand Down
4 changes: 2 additions & 2 deletions R/ggwithinstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ ggwithinstats <- function(data,
# styler: off
.f <- .f_switch(test)
subtitle_df <- .eval_f(.f, !!!.f.args, type = type)
subtitle <- extract_expression(subtitle_df)
subtitle <- .extract_expression(subtitle_df)
# styler: on

if (type == "parametric" && bf.message) {
# styler: off
caption_df <- .eval_f(.f, !!!.f.args, type = "bayes")
caption <- extract_expression(caption_df)
caption <- .extract_expression(caption_df)
# styler: on
}
}
Expand Down
34 changes: 18 additions & 16 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' @title Split data frame into a list by grouping variable.
#' @title Split data frame into a list by grouping variable
#'
#' @description
#'
Expand All @@ -12,27 +12,27 @@
#' @examplesIf identical(Sys.getenv("NOT_CRAN"), "true")
#' ggstatsplot:::.grouped_list(ggplot2::msleep, grouping.var = vore)
#' @keywords internal
.grouped_list <- function(data, grouping.var = NULL) {
data <- as_tibble(data)

if (quo_is_null(enquo(grouping.var))) {
return(data)
}

data %>%
.grouped_list <- function(data, grouping.var) {
as_tibble(data) %>%
split(f = new_formula(NULL, enquo(grouping.var)), drop = TRUE) %>%
list(data = ., title = names(.))
}


#' @title Message if palette doesn't have enough number of colors.
#' @name .palette_message
#' @description Informs the user about not using the default color palette
#' @title Check if palette has enough number of colors
#'
#' @description
#' Informs the user about not using the default color palette
#' when the number of factor levels is greater than 8, the maximum number of
#' colors allowed by `"Dark2"` palette from the `RColorBrewer` package.
#' colors allowed by `"Dark2"` palette from the `{RColorBrewer}` package.
#'
#' @examples
#' ggstatsplot:::.is_palette_sufficient("RColorBrewer", "Dark2", 6L)
#' ggstatsplot:::.is_palette_sufficient("RColorBrewer", "Dark2", 12L)
#'
#' @autoglobal
#' @noRd
.palette_message <- function(package, palette, min_length) {
#' @keywords internal
.is_palette_sufficient <- function(package, palette, min_length) {
palette_length <- paletteer::palettes_d_names %>%
filter(package == !!package, palette == !!palette) %$%
length[[1L]]
Expand All @@ -49,6 +49,7 @@
return(are_enough_colors_available)
}


#' @noRd
.eval_f <- function(.f, ...) {
tryCatch(
Expand All @@ -57,5 +58,6 @@
)
}


#' @noRd
extract_expression <- function(data) purrr::pluck(data, "expression", 1L, .default = NULL)
.extract_expression <- function(data) purrr::pluck(data, "expression", 1L, .default = NULL)
Loading

0 comments on commit 71c8ab1

Please sign in to comment.