R/grouped_ggbarstats.R
grouped_ggbarstats.Rd
Helper function for ggstatsplot::ggbarstats
to apply this
function across multiple levels of a given factor and combining the
resulting plots using ggstatsplot::combine_plots
.
grouped_ggbarstats( data, main, condition, counts = NULL, grouping.var, title.prefix = NULL, output = "plot", x = NULL, y = NULL, ..., plotgrid.args = list(), title.text = NULL, title.args = list(size = 16, fontface = "bold"), caption.text = NULL, caption.args = list(size = 10), sub.text = NULL, sub.args = list(size = 12) )
data | A dataframe (or a tibble) from which variables specified are to be taken. A matrix or tables will not be accepted. |
---|---|
main | The variable to use as the rows in the contingency table. |
condition | The variable to use as the columns in the contingency
table. Default is |
counts | A string naming a variable in data containing counts, or |
grouping.var | A single grouping variable (can be entered either as a
bare name |
title.prefix | Character string specifying the prefix text for the fixed
plot title (name of each factor level) (Default: |
output | Character that describes what is to be returned: can be
|
x | The variable to use as the rows in the contingency table. |
y | The variable to use as the columns in the contingency
table. Default is |
... | Arguments passed on to
|
plotgrid.args | A list of additional arguments to |
title.text | String or plotmath expression to be drawn as title for the combined plot. |
title.args | A list of additional arguments
provided to |
caption.text | String or plotmath expression to be drawn as the caption for the combined plot. |
caption.args | A list of additional arguments
provided to |
sub.text | The label with which the combined plot should be annotated. Can be a plotmath expression. |
sub.args | A list of additional arguments
provided to |
Unlike a number of statistical softwares, ggstatsplot
doesn't
provide the option for Yates' correction for the Pearson's chi-squared
statistic. This is due to compelling amount of Monte-Carlo simulation
research which suggests that the Yates' correction is overly conservative,
even in small sample sizes. As such it is recommended that it should not
ever be applied in practice (Camilli & Hopkins, 1978, 1979; Feinberg, 1980;
Larntz, 1978; Thompson, 1988).
For more about how the effect size measures and their confidence intervals
are computed, see ?rcompanion::cohenG
, ?rcompanion::cramerV
, and
?rcompanion::cramerVFit
.
# \donttest{ # for reproducibility set.seed(123) # let's create a smaller dataframe diamonds_short <- ggplot2::diamonds %>% dplyr::filter(.data = ., cut %in% c("Very Good", "Ideal")) %>% dplyr::filter(.data = ., clarity %in% c("SI1", "SI2", "VS1", "VS2")) %>% dplyr::sample_frac(tbl = ., size = 0.05) # plot ggstatsplot::grouped_ggbarstats( data = diamonds_short, x = color, y = clarity, grouping.var = cut, title.prefix = "Quality", bar.label = "both", plotgrid.args = list(nrow = 2) )#> Warning: Chi-squared approximation may be incorrect#> Note: 95% CI for effect size estimate was computed with 100 bootstrap samples. #>#>#> # A tibble: 4 x 16 #> clarity counts perc N D E F G H I #> <ord> <int> <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> #> 1 VS1 80 16.8 (n = 80) 10.00% 18.75% 15.00% 20.00% 8.75% 17.50% #> 2 VS2 133 27.9 (n = 133) 13.53% 16.54% 18.80% 21.05% 15.04% 9.77% #> 3 SI1 146 30.6 (n = 146) 13.01% 16.44% 21.23% 17.81% 10.96% 15.07% #> 4 SI2 118 24.7 (n = 118) 20.34% 12.71% 16.95% 16.95% 17.80% 11.86% #> J statistic p.value parameter method #> <chr> <dbl> <dbl> <dbl> <chr> #> 1 10.00% 7.33 0.292 6 Chi-squared test for given probabilities #> 2 5.26% 16.2 0.0127 6 Chi-squared test for given probabilities #> 3 5.48% 16.0 0.0140 6 Chi-squared test for given probabilities #> 4 3.39% 15.7 0.0154 6 Chi-squared test for given probabilities #> significance #> <chr> #> 1 ns #> 2 * #> 3 * #> 4 * #> Note: 95% CI for effect size estimate was computed with 100 bootstrap samples. #>#>#> # A tibble: 4 x 16 #> clarity counts perc N D E F G H I J #> <ord> <int> <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> #> 1 VS1 185 23.6 (n = 185) 11.35% 15.14% 16.76% 25.41% 13.51% 9.19% 8.65% #> 2 VS2 261 33.2 (n = 261) 19.16% 19.54% 16.09% 17.24% 12.26% 10.34% 5.36% #> 3 SI1 216 27.5 (n = 216) 16.67% 16.67% 13.89% 19.91% 17.13% 9.72% 6.02% #> 4 SI2 123 15.7 (n = 123) 16.26% 13.82% 17.07% 18.70% 16.26% 13.82% 4.07% #> statistic p.value parameter method #> <dbl> <dbl> <dbl> <chr> #> 1 25.6 0.000268 6 Chi-squared test for given probabilities #> 2 29.7 0.0000448 6 Chi-squared test for given probabilities #> 3 21.2 0.00167 6 Chi-squared test for given probabilities #> 4 12.0 0.0609 6 Chi-squared test for given probabilities #> significance #> <chr> #> 1 *** #> 2 *** #> 3 ** #> 4 ns# }