Skip to content

Commit 68d299f

Browse files
Make sure extract_stats() and cousins work out of the box with grouped plots (#955)
1 parent ff5457b commit 68d299f

File tree

7 files changed

+90
-21
lines changed

7 files changed

+90
-21
lines changed

API

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
combine_plots(plotlist, plotgrid.args = list(), annotation.args = list(), guides = "collect", ...)
66
extract_caption(p)
7-
extract_stats(p, ...)
7+
extract_stats(p)
88
extract_subtitle(p)
99
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), digits = 2L, proportion.test = results.subtitle, digits.perc = 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, ...)
1010
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, digits = 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, ...)

NEWS.md

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ N.B. All statistical analysis in `{ggstatsplot}` is carried out in
55
read the `NEWS` for that package:
66
<https://indrajeetpatil.github.io/statsExpressions/news/index.html>
77

8+
## MAJOR CHANGES
9+
10+
- `extract_stats()` returns a list of class `ggstatsplot_stats` which
11+
contains all the statistical summaries and expressions for a given plot.
12+
13+
- `extract_stats()`, `extract_subtitle()`, `extract_caption()` now works
14+
out of the box for the grouped plots as well.
15+
816
## BUG FIXES
917

1018
- `ggpiestats()` and `ggbarstats()` now respect `ratio()` argument for

R/extract-stats.R

+32-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#' The exact details included will depend on the function.
2222
#'
2323
#' @param p A plot from `{ggstatsplot}` package
24-
#' @param ... Ignored
2524
#'
2625
#' @autoglobal
2726
#'
@@ -46,27 +45,45 @@
4645
#'
4746
#' extract_stats(p1)
4847
#'
49-
#' extract_stats(p2[[1L]])
50-
#' extract_stats(p2[[2L]])
48+
#' extract_stats(p2)
5149
#' @export
52-
extract_stats <- function(p, ...) {
50+
extract_stats <- function(p) {
51+
if (inherits(p, "patchwork")) purrr::map(.extract_plots(p), .extract_stats) else .extract_stats(p)
52+
}
53+
54+
.extract_plots <- function(p) purrr::map(seq_along(p), ~ purrr::pluck(p, .x))
55+
56+
.pluck_plot_env <- function(p, data) purrr::pluck(p, "plot_env", data)
57+
58+
.extract_stats <- function(p) {
5359
# styler: off
54-
list(
55-
subtitle_data = tryCatch(p$plot_env$subtitle_df, error = function(e) NULL),
56-
caption_data = tryCatch(p$plot_env$caption_df, error = function(e) NULL),
57-
pairwise_comparisons_data = tryCatch(p$plot_env$mpc_df, error = function(e) NULL),
58-
descriptive_data = tryCatch(p$plot_env$descriptive_df, error = function(e) NULL),
59-
one_sample_data = tryCatch(p$plot_env$onesample_df, error = function(e) NULL),
60-
tidy_data = tryCatch(p$plot_env$tidy_df, error = function(e) NULL),
61-
glance_data = tryCatch(p$plot_env$glance_df, error = function(e) NULL)
62-
)
60+
structure(list(
61+
subtitle_data = .pluck_plot_env(p, "subtitle_df"),
62+
caption_data = .pluck_plot_env(p, "caption_df"),
63+
pairwise_comparisons_data = .pluck_plot_env(p, "mpc_df"),
64+
descriptive_data = .pluck_plot_env(p, "descriptive_df"),
65+
one_sample_data = .pluck_plot_env(p, "onesample_df"),
66+
tidy_data = .pluck_plot_env(p, "tidy_df"),
67+
glance_data = .pluck_plot_env(p, "glance_df")
68+
), class = c("ggstatsplot_stats", "list"))
6369
# styler: on
6470
}
6571

72+
73+
74+
# function factory to extract particular kind of stats data
75+
.extract_stats_data <- function(data_component) {
76+
function(p) {
77+
dat <- extract_stats(p)
78+
.pluck_expression <- function(x) purrr::pluck(x, data_component, "expression", 1L)
79+
if (inherits(dat, "ggstatsplot_stats")) .pluck_expression(dat) else purrr::map(dat, .pluck_expression)
80+
}
81+
}
82+
6683
#' @rdname extract_stats
6784
#' @export
68-
extract_subtitle <- function(p) purrr::pluck(extract_stats(p), "subtitle_data", "expression", 1L)
85+
extract_subtitle <- .extract_stats_data("subtitle_data")
6986

7087
#' @rdname extract_stats
7188
#' @export
72-
extract_caption <- function(p) purrr::pluck(extract_stats(p), "caption_data", "expression", 1L)
89+
extract_caption <- .extract_stats_data("caption_data")

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,9 @@ extract_stats(p)
769769
#>
770770
#> $glance_data
771771
#> NULL
772+
#>
773+
#> attr(,"class")
774+
#> [1] "ggstatsplot_stats" "list"
772775
```
773776

774777
Note that all of this analysis is carried out by `{statsExpressions}`

man/extract_stats.Rd

+2-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/extract-stats.md

+24
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,27 @@
128128
NULL
129129
130130

131+
# checking if extract_stats works for grouped plots
132+
133+
Code
134+
p8 <- grouped_ggpiestats(mtcars, x = cyl, grouping.var = am)
135+
extracted_data <- extract_stats(p8)
136+
summary(extracted_data)
137+
Output
138+
Length Class Mode
139+
[1,] 7 ggstatsplot_stats list
140+
[2,] 7 ggstatsplot_stats list
141+
Code
142+
extract_subtitle(p8)
143+
Output
144+
[[1]]
145+
list(chi["gof"]^2 * "(" * 2 * ")" == "7.68", italic(p) == "0.02",
146+
widehat(italic("C"))["Pearson"] == "0.54", CI["95%"] ~ "[" *
147+
"0.07", "0.73" * "]", italic("n")["obs"] == "19")
148+
149+
[[2]]
150+
list(chi["gof"]^2 * "(" * 2 * ")" == "4.77", italic(p) == "0.09",
151+
widehat(italic("C"))["Pearson"] == "0.52", CI["95%"] ~ "[" *
152+
"0.00", "0.74" * "]", italic("n")["obs"] == "13")
153+
154+

tests/testthat/test-extract-stats.R

+20
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,23 @@ test_that(
7575
})
7676
}
7777
)
78+
79+
80+
test_that(
81+
"checking if extract_stats works for grouped plots",
82+
{
83+
expect_snapshot({
84+
p8 <- grouped_ggpiestats(mtcars, x = cyl, grouping.var = am)
85+
extracted_data <- extract_stats(p8)
86+
summary(extracted_data)
87+
extract_subtitle(p8)
88+
})
89+
}
90+
)
91+
92+
test_that(
93+
"checking if extract_stats produces NULL on supported objects",
94+
{
95+
expect_length(purrr::compact(extract_stats(iris)), 0L)
96+
}
97+
)

0 commit comments

Comments
 (0)