Package | Status | Usage | GitHub | Miscellaneous |
---|---|---|---|---|
βWhat is to be sought in designs for the display of information is the clear portrayal of complexity. Not the complication of the simple; rather β¦ the revelation of the complex.β
- Edward R. Tufte
ggstatsplot
is an
extension of ggplot2
package
for creating graphics with details from statistical tests included in
the information-rich plots themselves. In a typical exploratory data
analysis workflow, data visualization and statistical modeling are two
different phases: visualization informs modeling, and modeling in its
turn can suggest a different visualization method, and so on and so
forth. The central idea of ggstatsplot
is simple: combine these two
phases into one in the form of graphics with statistical details, which
makes data exploration simpler and faster.
Type | Source | Command |
---|---|---|
Release | CRAN | install.packages("ggstatsplot") |
Development | GitHub | remotes::install_github("IndrajeetPatil/ggstatsplot") |
Linux users may encounter some installation problems. In particular, the
ggstatsplot
package depends on the PMCMRplus
package.
ERROR: dependencies βgmpβ, βRmpfrβ are not available for package βPMCMRplusβ
ERROR: dependency βpairwiseComparisonsβ is not available for package βggstatsplotβ
This means that your operating system lacks gmp
and Rmpfr
libraries.
If you use Ubuntu
, you can install these dependencies:
sudo apt-get install libgmp3-dev
sudo apt-get install libmpfr-dev
The following README
file briefly describes the installation
procedure:
https://CRAN.R-project.org/package=PMCMRplus/readme/README.html
If you want to cite this package in a scientific journal or in any other
context, run the following code in your R
console:
citation("ggstatsplot")
Patil, I. (2021). Visualizations with statistical details: The
'ggstatsplot' approach. Journal of Open Source Software, 6(61), 3167,
doi:10.21105/joss.03167
A BibTeX entry for LaTeX users is
@Article{,
doi = {10.21105/joss.03167},
url = {https://doi.org/10.21105/joss.03167},
year = {2021},
publisher = {{The Open Journal}},
volume = {6},
number = {61},
pages = {3167},
author = {Indrajeet Patil},
title = {{Visualizations with statistical details: The {'ggstatsplot'} approach}},
journal = {{Journal of Open Source Software}},
}
There is currently a publication in preparation corresponding to this package and the citation will be updated once itβs published.
To see the detailed documentation for each function in the stable CRAN version of the package, see:
-
Presentation: https://indrajeetpatil.github.io/ggstatsplot_slides/slides/ggstatsplot_presentation.html#1
-
Vignettes: https://indrajeetpatil.github.io/ggstatsplot/articles/
It, therefore, produces a limited kinds of plots for the supported analyses:
In addition to these basic plots, ggstatsplot
also provides
grouped_
versions (see below) that makes it easy to repeat the
same analysis for any grouping variable.
The table below summarizes all the different types of analyses currently supported in this package-
Functions | Description | Parametric | Non-parametric | Robust | Bayesian |
---|---|---|---|---|---|
ggbetweenstats |
Between group/condition comparisons | β | β | β | β |
ggwithinstats |
Within group/condition comparisons | β | β | β | β |
gghistostats , ggdotplotstats |
Distribution of a numeric variable | β | β | β | β |
ggcorrmat |
Correlation matrix | β | β | β | β |
ggscatterstats |
Correlation between two variables | β | β | β | β |
ggpiestats , ggbarstats |
Association between categorical variables | β | β | β | β |
ggpiestats , ggbarstats |
Equal proportions for categorical variable levels | β | β | β | β |
ggcoefstats |
Regression model coefficients | β | β | β | β |
ggcoefstats |
Random-effects meta-analysis | β | β | β | β |
Summary of Bayesian analysis
Analysis | Hypothesis testing | Estimation |
---|---|---|
(one/two-sample) t-test | β | β |
one-way ANOVA | β | β |
correlation | β | β |
(one/two-way) contingency table | β | β |
random-effects meta-analysis | β | β |
For all statistical tests reported in the plots, the default template abides by the APA gold standard for statistical reporting. For example, here are results from Yuenβs test for trimmed means (robust t-test):
Here is a summary table of all the statistical tests currently supported across various functions: https://indrajeetpatil.github.io/statsExpressions/articles/stats_details.html
Here are examples of the main functions currently supported in
ggstatsplot
.
Note: If you are reading this on GitHub
repository, the
documentation below is for the development version of the package.
So you may see some features available here that are not currently
present in the stable version of this package on CRAN. For
documentation relevant for the CRAN
version, see:
https://CRAN.R-project.org/package=ggstatsplot/readme/README.html
This function creates either a violin plot, a box plot, or a mix of two for between-group or between-condition comparisons with results from statistical tests in the subtitle. The simplest function call looks like this-
# for reproducibility
set.seed(123)
library(ggstatsplot)
# plot
ggbetweenstats(
data = iris,
x = Species,
y = Sepal.Length,
title = "Distribution of sepal length across Iris species"
)
Defaults return
β
raw data + distributions
β
descriptive statistics
β
inferential statistics
β
effect size + CIs
β
pairwise
comparisons
β
Bayesian hypothesis-testing
β
Bayesian
estimation
A number of other arguments can be specified to make this plot even more
informative or change some of the default options. Additionally, there
is also a grouped_
variant of this function that makes it easy to
repeat the same operation across a single grouping variable:
# for reproducibility
set.seed(123)
# plot
grouped_ggbetweenstats(
data = dplyr::filter(movies_long, genre %in% c("Action", "Comedy")),
x = mpaa,
y = length,
grouping.var = genre, # grouping variable
outlier.tagging = TRUE, # whether outliers need to be tagged
outlier.label = title, # variable to be used for tagging outliers
outlier.coef = 2,
ggsignif.args = list(textsize = 4, tip_length = 0.01),
p.adjust.method = "bonferroni", # method for adjusting p-values for multiple comparisons
# adding new components to `ggstatsplot` default
ggplot.component = list(ggplot2::scale_y_continuous(sec.axis = ggplot2::dup_axis())),
caption = substitute(paste(italic("Source"), ": IMDb (Internet Movie Database)")),
palette = "default_jama",
package = "ggsci",
plotgrid.args = list(nrow = 1),
annotation.args = list(title = "Differences in movie length by mpaa ratings for different genres")
)
Note here that the function can be used to tag outliers!
graphical element | geom_ used |
argument for further modification |
---|---|---|
raw data | ggplot2::geom_point |
point.args |
box plot | ggplot2::geom_boxplot |
β |
density plot | ggplot2::geom_violin |
violin.args |
centrality measure point | ggplot2::geom_point |
centrality.point.args |
centrality measure label | ggrepel::geom_label_repel |
centrality.label.args |
outlier point | ggplot2::stat_boxplot |
β |
outlier label | ggrepel::geom_label_repel |
outlier.label.args |
pairwise comparisons | ggsignif::geom_ggsignif |
ggsignif.args |
Central tendency measure
Type | Measure | Function used |
---|---|---|
Parametric | mean | parameters::describe_distribution |
Non-parametric | median | parameters::describe_distribution |
Robust | trimmed mean | parameters::describe_distribution |
Bayesian | MAP (maximum a posteriori probability) estimate | parameters::describe_distribution |
Hypothesis testing
Type | No. of groups | Test | Function used |
---|---|---|---|
Parametric | > 2 | Fisherβs or Welchβs one-way ANOVA | stats::oneway.test |
Non-parametric | > 2 | KruskalβWallis one-way ANOVA | stats::kruskal.test |
Robust | > 2 | Heteroscedastic one-way ANOVA for trimmed means | WRS2::t1way |
Bayes Factor | > 2 | Fisherβs ANOVA | BayesFactor::anovaBF |
Parametric | 2 | Studentβs or Welchβs t-test | stats::t.test |
Non-parametric | 2 | MannβWhitney U test | stats::wilcox.test |
Robust | 2 | Yuenβs test for trimmed means | WRS2::yuen |
Bayesian | 2 | Studentβs t-test | BayesFactor::ttestBF |
Effect size estimation
Pairwise comparison tests
Type | Equal variance? | Test | p-value adjustment? | Function used |
---|---|---|---|---|
Parametric | No | Games-Howell test | β | stats::pairwise.t.test |
Parametric | Yes | Studentβs t-test | β | PMCMRplus::gamesHowellTest |
Non-parametric | No | Dunn test | β | PMCMRplus::kwAllPairsDunnTest |
Robust | No | Yuenβs trimmed means test | β | WRS2::lincon |
Bayes Factor | β | Studentβs t-test | β | BayesFactor::ttestBF |
For more, see the ggbetweenstats
vignette:
https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggbetweenstats.html
ggbetweenstats
function has an identical twin function ggwithinstats
for repeated measures designs that behaves in the same fashion with a
few minor tweaks introduced to properly visualize the repeated measures
design. As can be seen from an example below, the only difference
between the plot structure is that now the group means are connected by
paths to highlight the fact that these data are paired with each other.
# for reproducibility and data
set.seed(123)
library(WRS2) # for data
library(afex) # to run anova
# plot
ggwithinstats(
data = WineTasting,
x = Wine,
y = Taste,
title = "Wine tasting",
caption = "Data source: `WRS2` R package",
ggtheme = ggthemes::theme_fivethirtyeight()
)
Defaults return
β
raw data + distributions
β
descriptive statistics
β
inferential statistics
β
effect size + CIs
β
pairwise
comparisons
β
Bayesian hypothesis-testing
β
Bayesian
estimation
The central tendency measure displayed will depend on the statistics:
Type | Measure | Function used |
---|---|---|
Parametric | mean | parameters::describe_distribution |
Non-parametric | median | parameters::describe_distribution |
Robust | trimmed mean | parameters::describe_distribution |
Bayesian | MAP estimate | parameters::describe_distribution |
As with the ggbetweenstats
, this function also has a grouped_
variant that makes repeating the same analysis across a single grouping
variable quicker. We will see an example with only repeated
measurements-
# common setup
set.seed(123)
# plot
grouped_ggwithinstats(
data = dplyr::filter(
bugs_long,
region %in% c("Europe", "North America"),
condition %in% c("LDLF", "LDHF")
),
x = condition,
y = desire,
type = "np", # non-parametric statistics
xlab = "Condition",
ylab = "Desire to kill an artrhopod",
grouping.var = region,
outlier.tagging = TRUE,
outlier.label = education
)
graphical element | geom_ used |
argument for further modification |
---|---|---|
raw data | ggplot2::geom_point |
point.args |
point path | ggplot2::geom_path |
point.path.args |
box plot | ggplot2::geom_boxplot |
β |
density plot | ggplot2::geom_violin |
violin.args |
centrality measure point | ggplot2::geom_point |
centrality.point.args |
centrality measure point path | ggplot2::geom_path |
centrality.path.args |
centrality measure label | ggrepel::geom_label_repel |
centrality.label.args |
outlier point | ggplot2::stat_boxplot |
β |
outlier label | ggrepel::geom_label_repel |
outlier.label.args |
pairwise comparisons | ggsignif::geom_ggsignif |
ggsignif.args |
Central tendency measure
Type | Measure | Function used |
---|---|---|
Parametric | mean | parameters::describe_distribution |
Non-parametric | median | parameters::describe_distribution |
Robust | trimmed mean | parameters::describe_distribution |
Bayesian | MAP (maximum a posteriori probability) estimate | parameters::describe_distribution |
Hypothesis testing
Type | No. of groups | Test | Function used |
---|---|---|---|
Parametric | > 2 | One-way repeated measures ANOVA | afex::aov_ez |
Non-parametric | > 2 | Friedman rank sum test | stats::friedman.test |
Robust | > 2 | Heteroscedastic one-way repeated measures ANOVA for trimmed means | WRS2::rmanova |
Bayes Factor | > 2 | One-way repeated measures ANOVA | BayesFactor::anovaBF |
Parametric | 2 | Studentβs t-test | stats::t.test |
Non-parametric | 2 | Wilcoxon signed-rank test | stats::wilcox.test |
Robust | 2 | Yuenβs test on trimmed means for dependent samples | WRS2::yuend |
Bayesian | 2 | Studentβs t-test | BayesFactor::ttestBF |
Effect size estimation
Pairwise comparison tests
Type | Test | p-value adjustment? | Function used |
---|---|---|---|
Parametric | Studentβs t-test | β | stats::pairwise.t.test |
Non-parametric | Durbin-Conover test | β | PMCMRplus::durbinAllPairsTest |
Robust | Yuenβs trimmed means test | β | WRS2::rmmcp |
Bayesian | Studentβs t-test | β | BayesFactor::ttestBF |
For more, see the ggwithinstats
vignette:
https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggwithinstats.html
To visualize the distribution of a single variable and check if its mean
is significantly different from a specified value with a one-sample
test, gghistostats
can be used.
# for reproducibility
set.seed(123)
# plot
gghistostats(
data = ggplot2::msleep, # dataframe from which variable is to be taken
x = awake, # numeric variable whose distribution is of interest
title = "Amount of time spent awake", # title for the plot
caption = substitute(paste(italic("Source: "), "Mammalian sleep data set")),
test.value = 12, # default value is 0
binwidth = 1, # binwidth value (experiment)
ggtheme = hrbrthemes::theme_ipsum_tw()
)
Defaults return
β
counts + proportion for bins
β
descriptive statistics
β
inferential statistics
β
effect size + CIs
β
Bayesian
hypothesis-testing
β
Bayesian estimation
There is also a grouped_
variant of this function that makes it easy
to repeat the same operation across a single grouping variable:
# for reproducibility
set.seed(123)
# plot
grouped_gghistostats(
data = dplyr::filter(movies_long, genre %in% c("Action", "Comedy")),
x = budget,
test.value = 50,
type = "nonparametric",
xlab = "Movies budget (in million US$)",
grouping.var = genre, # grouping variable
normal.curve = TRUE, # superimpose a normal distribution curve
normal.curve.args = list(color = "red", size = 1),
ggtheme = ggthemes::theme_tufte(),
# modify the defaults from `ggstatsplot` for each plot
ggplot.component = ggplot2::labs(caption = "Source: IMDB.com"),
plotgrid.args = list(nrow = 1),
annotation.args = list(title = "Movies budgets for different genres")
)
graphical element | geom_ used |
argument for further modification |
---|---|---|
histogram bin | ggplot2::stat_bin |
bin.args |
centrality measure line | ggplot2::geom_vline |
centrality.line.args |
normality curve | ggplot2::stat_function |
normal.curve.args |
Central tendency measure
Type | Measure | Function used |
---|---|---|
Parametric | mean | parameters::describe_distribution |
Non-parametric | median | parameters::describe_distribution |
Robust | trimmed mean | parameters::describe_distribution |
Bayesian | MAP (maximum a posteriori probability) estimate | parameters::describe_distribution |
Hypothesis testing
Type | Test | Function used |
---|---|---|
Parametric | One-sample Studentβs t-test | stats::t.test |
Non-parametric | One-sample Wilcoxon test | stats::wilcox.test |
Robust | Bootstrap-t method for one-sample test | WRS2::trimcibt |
Bayesian | One-sample Studentβs t-test | BayesFactor::ttestBF |
Effect size estimation
For more, including information about the variant of this function
grouped_gghistostats
, see the gghistostats
vignette:
https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/gghistostats.html
This function is similar to gghistostats
, but is intended to be used
when the numeric variable also has a label.
# for reproducibility
set.seed(123)
# plot
ggdotplotstats(
data = dplyr::filter(gapminder::gapminder, continent == "Asia"),
y = country,
x = lifeExp,
test.value = 55,
type = "robust",
title = "Distribution of life expectancy in Asian continent",
xlab = "Life expectancy",
caption = substitute(
paste(
italic("Source"),
": Gapminder dataset from https://www.gapminder.org/"
)
)
)
Defaults return
β
descriptives (mean + sample size)
β
inferential statistics
β
effect size + CIs
β
Bayesian hypothesis-testing
β
Bayesian
estimation
As with the rest of the functions in this package, there is also a
grouped_
variant of this function to facilitate looping the same
operation for all levels of a single grouping variable.
# for reproducibility
set.seed(123)
# plot
grouped_ggdotplotstats(
data = dplyr::filter(ggplot2::mpg, cyl %in% c("4", "6")),
x = cty,
y = manufacturer,
type = "bayes", # Bayesian test
xlab = "city miles per gallon",
ylab = "car manufacturer",
grouping.var = cyl, # grouping variable
test.value = 15.5,
point.args = list(color = "red", size = 5, shape = 13),
annotation.args = list(title = "Fuel economy data")
)
graphical element | geom_ used |
argument for further modification |
---|---|---|
raw data | ggplot2::geom_point |
point.args |
centrality measure line | ggplot2::geom_vline |
centrality.line.args |
Central tendency measure
Type | Measure | Function used |
---|---|---|
Parametric | mean | parameters::describe_distribution |
Non-parametric | median | parameters::describe_distribution |
Robust | trimmed mean | parameters::describe_distribution |
Bayesian | MAP (maximum a posteriori probability) estimate | parameters::describe_distribution |
Hypothesis testing
Type | Test | Function used |
---|---|---|
Parametric | One-sample Studentβs t-test | stats::t.test |
Non-parametric | One-sample Wilcoxon test | stats::wilcox.test |
Robust | Bootstrap-t method for one-sample test | WRS2::trimcibt |
Bayesian | One-sample Studentβs t-test | BayesFactor::ttestBF |
Effect size estimation
This function creates a scatterplot with marginal distributions overlaid
on the axes (from ggExtra::ggMarginal
) and results from statistical
tests in the subtitle:
ggscatterstats(
data = ggplot2::msleep,
x = sleep_rem,
y = awake,
xlab = "REM sleep (in hours)",
ylab = "Amount of time spent awake (in hours)",
title = "Understanding mammalian sleep"
)
Defaults return
β
raw data + distributions
β
marginal distributions
β
inferential statistics
β
effect size + CIs
β
Bayesian
hypothesis-testing
β
Bayesian estimation
The available marginal distributions are-
- histograms
- boxplots
- density
- violin
- densigram (density + histogram)
Number of other arguments can be specified to modify this basic plot-
# for reproducibility
set.seed(123)
# plot
ggscatterstats(
data = dplyr::filter(movies_long, genre == "Action"),
x = budget,
y = rating,
type = "robust", # type of test that needs to be run
xlab = "Movie budget (in million/ US$)", # label for x axis
ylab = "IMDB rating", # label for y axis
label.var = title, # variable for labeling data points
label.expression = rating < 5 & budget > 100, # expression that decides which points to label
title = "Movie budget and IMDB rating (action)", # title text for the plot
caption = expression(paste(italic("Note"), ": IMDB stands for Internet Movie DataBase")),
ggtheme = hrbrthemes::theme_ipsum_ps(), # choosing a different theme
# turn off `ggstatsplot` theme layer
marginal.type = "boxplot", # type of marginal distribution to be displayed
xfill = "pink", # color fill for x-axis marginal distribution
yfill = "#009E73" # color fill for y-axis marginal distribution
)
Additionally, there is also a grouped_
variant of this function that
makes it easy to repeat the same operation across a single grouping
variable. Also, note that, as opposed to the other functions, this
function does not return a ggplot
object and any modification you want
to make can be made in advance using ggplot.component
argument
(available for all functions, but especially useful here):
# for reproducibility
set.seed(123)
# plot
grouped_ggscatterstats(
data = dplyr::filter(movies_long, genre %in% c("Action", "Comedy")),
x = rating,
y = length,
grouping.var = genre, # grouping variable
label.var = title,
label.expression = length > 200,
xlab = "IMDB rating",
ggtheme = ggplot2::theme_grey(),
ggplot.component = list(
ggplot2::scale_x_continuous(breaks = seq(2, 9, 1), limits = (c(2, 9)))
),
plotgrid.args = list(nrow = 1),
annotation.args = list(title = "Relationship between movie length and IMDB ratings")
)
graphical element | geom_ used |
argument for further modification |
---|---|---|
raw data | ggplot2::geom_point |
point.args |
labels for raw data | ggrepel::geom_label_repel |
point.label.args |
smooth line | ggplot2::geom_smooth |
smooth.line.args |
marginal distributions | ggExtra::ggMarginal |
β |
Hypothesis testing and Effect size estimation
Type | Test | CI? | Function used |
---|---|---|---|
Parametric | Pearsonβs correlation coefficient | β | correlation::correlation |
Non-parametric | Spearmanβs rank correlation coefficient | β | correlation::correlation |
Robust | Winsorized Pearson correlation coefficient | β | correlation::correlation |
Bayesian | Pearsonβs correlation coefficient | β | correlation::correlation |
For more, see the ggscatterstats
vignette:
https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggscatterstats.html
ggcorrmat
makes a correlalogram (a matrix of correlation coefficients)
with minimal amount of code. Just sticking to the defaults itself
produces publication-ready correlation matrices. But, for the sake of
exploring the available options, letβs change some of the defaults. For
example, multiple aesthetics-related arguments can be modified to change
the appearance of the correlation matrix.
# for reproducibility
set.seed(123)
# as a default this function outputs a correlation matrix plot
ggcorrmat(
data = ggplot2::msleep,
colors = c("#B2182B", "white", "#4D4D4D"),
title = "Correlalogram for mammals sleep dataset",
subtitle = "sleep units: hours; weight units: kilograms"
)
Defaults return
β
effect size + significance
β
careful handling of NA
s
If there are NA
s present in the selected variables, the legend will
display minimum, median, and maximum number of pairs used for
correlation tests.
There is also a grouped_
variant of this function that makes it easy
to repeat the same operation across a single grouping variable:
# for reproducibility
set.seed(123)
# plot
grouped_ggcorrmat(
data = dplyr::filter(movies_long, genre %in% c("Action", "Comedy")),
type = "robust", # correlation method
colors = c("#cbac43", "white", "#550000"),
grouping.var = genre, # grouping variable
matrix.type = "lower" # type of matrix
)
You can also get a dataframe containing all relevant details from the statistical tests:
# setup
set.seed(123)
# dataframe in long format
ggcorrmat(
data = ggplot2::msleep,
type = "bayes",
output = "dataframe"
)
#> # A tibble: 15 x 14
#> parameter1 parameter2 estimate conf.level conf.low conf.high pd
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 sleep_total sleep_rem 0.731 0.95 0.617 0.810 1
#> 2 sleep_total sleep_cycle -0.432 0.95 -0.678 -0.223 0.995
#> 3 sleep_total awake -1.00 0.95 -1.00 -1.00 1
#> 4 sleep_total brainwt -0.339 0.95 -0.523 -0.156 0.996
#> 5 sleep_total bodywt -0.300 0.95 -0.458 -0.142 0.997
#> 6 sleep_rem sleep_cycle -0.306 0.95 -0.535 -0.0555 0.965
#> 7 sleep_rem awake -0.734 0.95 -0.824 -0.638 1
#> 8 sleep_rem brainwt -0.202 0.95 -0.410 0.0130 0.927
#> 9 sleep_rem bodywt -0.315 0.95 -0.481 -0.120 0.994
#> 10 sleep_cycle awake 0.441 0.95 0.226 0.662 0.995
#> 11 sleep_cycle brainwt 0.823 0.95 0.720 0.911 1
#> 12 sleep_cycle bodywt 0.386 0.95 0.145 0.610 0.992
#> 13 awake brainwt 0.341 0.95 0.154 0.524 0.992
#> 14 awake bodywt 0.299 0.95 0.139 0.454 0.998
#> 15 brainwt bodywt 0.926 0.95 0.896 0.957 1
#> rope.percentage prior.distribution prior.location prior.scale bayes.factor
#> <dbl> <chr> <dbl> <dbl> <dbl>
#> 1 0 beta 1.41 1.41 3.00e+ 9
#> 2 0.0173 beta 1.41 1.41 8.85e+ 0
#> 3 0 beta 1.41 1.41 NA
#> 4 0.028 beta 1.41 1.41 7.29e+ 0
#> 5 0.0292 beta 1.41 1.41 9.28e+ 0
#> 6 0.091 beta 1.41 1.41 1.42e+ 0
#> 7 0 beta 1.41 1.41 3.01e+ 9
#> 8 0.212 beta 1.41 1.41 6.54e- 1
#> 9 0.0362 beta 1.41 1.41 4.80e+ 0
#> 10 0.0158 beta 1.41 1.41 8.85e+ 0
#> 11 0 beta 1.41 1.41 3.80e+ 6
#> 12 0.0392 beta 1.41 1.41 3.76e+ 0
#> 13 0.0253 beta 1.41 1.41 7.29e+ 0
#> 14 0.0265 beta 1.41 1.41 9.27e+ 0
#> 15 0 beta 1.41 1.41 1.58e+22
#> method n.obs
#> <chr> <int>
#> 1 Bayesian Pearson correlation 61
#> 2 Bayesian Pearson correlation 32
#> 3 Bayesian Pearson correlation 83
#> 4 Bayesian Pearson correlation 56
#> 5 Bayesian Pearson correlation 83
#> 6 Bayesian Pearson correlation 32
#> 7 Bayesian Pearson correlation 61
#> 8 Bayesian Pearson correlation 48
#> 9 Bayesian Pearson correlation 61
#> 10 Bayesian Pearson correlation 32
#> 11 Bayesian Pearson correlation 30
#> 12 Bayesian Pearson correlation 32
#> 13 Bayesian Pearson correlation 56
#> 14 Bayesian Pearson correlation 83
#> 15 Bayesian Pearson correlation 56
Additionally, partial correlation are also supported:
# setup
set.seed(123)
# dataframe in long format
ggcorrmat(
data = ggplot2::msleep,
type = "bayes",
partial = TRUE,
output = "dataframe"
)
#> # A tibble: 15 x 14
#> parameter1 parameter2 estimate conf.level conf.low conf.high pd
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 sleep_total sleep_rem 0.279 0.95 0.0202 0.550 0.940
#> 2 sleep_total sleep_cycle -0.0181 0.95 -0.306 0.254 0.543
#> 3 sleep_total awake -1 0.95 -1 -1 1
#> 4 sleep_total brainwt -0.0818 0.95 -0.352 0.192 0.678
#> 5 sleep_total bodywt -0.163 0.95 -0.425 0.121 0.818
#> 6 sleep_rem sleep_cycle -0.0666 0.95 -0.335 0.222 0.643
#> 7 sleep_rem awake 0.0505 0.95 -0.212 0.328 0.611
#> 8 sleep_rem brainwt 0.0811 0.95 -0.235 0.326 0.668
#> 9 sleep_rem bodywt -0.0190 0.95 -0.296 0.265 0.544
#> 10 sleep_cycle awake -0.00603 0.95 -0.278 0.279 0.516
#> 11 sleep_cycle brainwt 0.764 0.95 0.637 0.871 1
#> 12 sleep_cycle bodywt -0.0865 0.95 -0.351 0.187 0.691
#> 13 awake brainwt -0.0854 0.95 -0.349 0.205 0.690
#> 14 awake bodywt -0.407 0.95 -0.630 -0.146 0.991
#> 15 brainwt bodywt 0.229 0.95 -0.0341 0.484 0.904
#> rope.percentage prior.distribution prior.location prior.scale bayes.factor
#> <dbl> <chr> <dbl> <dbl> <dbl>
#> 1 0.133 beta 1.41 1.41 1.04
#> 2 0.418 beta 1.41 1.41 0.277
#> 3 0 beta 1.41 1.41 NA
#> 4 0.390 beta 1.41 1.41 0.311
#> 5 0.294 beta 1.41 1.41 0.417
#> 6 0.404 beta 1.41 1.41 0.297
#> 7 0.411 beta 1.41 1.41 0.287
#> 8 0.380 beta 1.41 1.41 0.303
#> 9 0.424 beta 1.41 1.41 0.280
#> 10 0.422 beta 1.41 1.41 0.276
#> 11 0 beta 1.41 1.41 131029.
#> 12 0.393 beta 1.41 1.41 0.309
#> 13 0.390 beta 1.41 1.41 0.310
#> 14 0.033 beta 1.41 1.41 4.82
#> 15 0.206 beta 1.41 1.41 0.637
#> method n.obs
#> <chr> <int>
#> 1 Bayesian Pearson correlation 30
#> 2 Bayesian Pearson correlation 30
#> 3 Bayesian Pearson correlation 30
#> 4 Bayesian Pearson correlation 30
#> 5 Bayesian Pearson correlation 30
#> 6 Bayesian Pearson correlation 30
#> 7 Bayesian Pearson correlation 30
#> 8 Bayesian Pearson correlation 30
#> 9 Bayesian Pearson correlation 30
#> 10 Bayesian Pearson correlation 30
#> 11 Bayesian Pearson correlation 30
#> 12 Bayesian Pearson correlation 30
#> 13 Bayesian Pearson correlation 30
#> 14 Bayesian Pearson correlation 30
#> 15 Bayesian Pearson correlation 30
graphical element | geom_ used |
argument for further modification |
---|---|---|
correlation matrix | ggcorrplot::ggcorrplot |
ggcorrplot.args |
Hypothesis testing and Effect size estimation
Type | Test | CI? | Function used |
---|---|---|---|
Parametric | Pearsonβs correlation coefficient | β | correlation::correlation |
Non-parametric | Spearmanβs rank correlation coefficient | β | correlation::correlation |
Robust | Winsorized Pearson correlation coefficient | β | correlation::correlation |
Bayesian | Pearsonβs correlation coefficient | β | correlation::correlation |
For examples and more information, see the ggcorrmat
vignette:
https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggcorrmat.html
This function creates a pie chart for categorical or nominal variables with results from contingency table analysis (Pearsonβs chi-squared test for between-subjects design and McNemarβs chi-squared test for within-subjects design) included in the subtitle of the plot. If only one categorical variable is entered, results from one-sample proportion test (i.e., a chi-squared goodness of fit test) will be displayed as a subtitle.
To study an interaction between two categorical variables:
# for reproducibility
set.seed(123)
# plot
ggpiestats(
data = mtcars,
x = am,
y = cyl,
package = "wesanderson",
palette = "Royal1",
title = "Dataset: Motor Trend Car Road Tests", # title for the plot
legend.title = "Transmission", # title for the legend
caption = substitute(paste(italic("Source"), ": 1974 Motor Trend US magazine"))
)
Defaults return
β
descriptives (frequency + %s)
β
inferential statistics
β
effect size + CIs
β
Goodness-of-fit tests
β
Bayesian
hypothesis-testing
β
Bayesian estimation
There is also a grouped_
variant of this function that makes it easy
to repeat the same operation across a single grouping variable.
Following example is a case where the theoretical question is about
proportions for different levels of a single nominal variable:
# for reproducibility
set.seed(123)
# plot
grouped_ggpiestats(
data = mtcars,
x = cyl,
grouping.var = am, # grouping variable
label.repel = TRUE, # repel labels (helpful for overlapping labels)
package = "ggsci", # package from which color palette is to be taken
palette = "default_ucscgb" # choosing a different color palette
)
graphical element | geom_ used |
argument for further modification |
---|---|---|
pie slices | ggplot2::geom_col |
β |
descriptive labels | ggplot2::geom_label /ggrepel::geom_label_repel |
label.args |
two-way table
Hypothesis testing
Effect size estimation
one-way table
Hypothesis testing
Type | Test | Function used |
---|---|---|
Parametric/Non-parametric | Goodness of fit test | stats::chisq.test |
Bayesian | Bayesian Goodness of fit test | (custom) |
Effect size estimation
Type | Effect size | CI? | Function used |
---|---|---|---|
Parametric/Non-parametric | Cramerβs | β | bayestestR::describe_posterior |
Bayesian | β | β | β |
For more, see the ggpiestats
vignette:
https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggpiestats.html
In case you are not a fan of pie charts (for very good reasons), you can
alternatively use ggbarstats
function which has a similar syntax.
N.B. The p-values from one-sample proportion test are displayed on top of each bar.
# for reproducibility
set.seed(123)
library(ggplot2)
# plot
ggbarstats(
data = movies_long,
x = mpaa,
y = genre,
title = "MPAA Ratings by Genre",
xlab = "movie genre",
legend.title = "MPAA rating",
ggtheme = hrbrthemes::theme_ipsum_pub(),
ggplot.component = list(ggplot2::scale_x_discrete(guide = ggplot2::guide_axis(n.dodge = 2))),
palette = "Set2"
)
Defaults return
β
descriptives (frequency + %s)
β
inferential statistics
β
effect size + CIs
β
Goodness-of-fit tests
β
Bayesian
hypothesis-testing
β
Bayesian estimation
And, needless to say, there is also a grouped_
variant of this
function-
# setup
set.seed(123)
# plot
grouped_ggbarstats(
data = mtcars,
x = am,
y = cyl,
grouping.var = vs,
package = "wesanderson",
palette = "Darjeeling2",
ggtheme = ggthemes::theme_tufte(base_size = 12)
)
graphical element | geom_ used |
argument for further modification |
---|---|---|
bars | ggplot2::geom_bar |
β |
descriptive labels | ggplot2::geom_label |
label.args |
two-way table
Hypothesis testing
Effect size estimation
one-way table
Hypothesis testing
Type | Test | Function used |
---|---|---|
Parametric/Non-parametric | Goodness of fit test | stats::chisq.test |
Bayesian | Bayesian Goodness of fit test | (custom) |
Effect size estimation
Type | Effect size | CI? | Function used |
---|---|---|---|
Parametric/Non-parametric | Cramerβs | β | bayestestR::describe_posterior |
Bayesian | β | β | β |
The function ggcoefstats
generates dot-and-whisker plots for
regression models saved in a tidy data frame. The tidy dataframes are
prepared using parameters::model_parameters
. Additionally, if
available, the model summary indices are also extracted from
performance::model_performance
.
Although the statistical models displayed in the plot may differ based on the class of models being investigated, there are few aspects of the plot that will be invariant across models:
-
The dot-whisker plot contains a dot representing the estimate and their confidence intervals (
95%
is the default). The estimate can either be effect sizes (for tests that depend on theF
-statistic) or regression coefficients (for tests witht
-, -, andz
-statistic), etc. The function will, by default, display a helpfulx
-axis label that should clear up what estimates are being displayed. The confidence intervals can sometimes be asymmetric if bootstrapping was used. -
The label attached to dot will provide more details from the statistical test carried out and it will typically contain estimate, statistic, and p-value.
-
The caption will contain diagnostic information, if available, about models that can be useful for model selection: The smaller the Akaikeβs Information Criterion (AIC) and the Bayesian Information Criterion (BIC) values, the βbetterβ the model is.
-
The output of this function will be a
ggplot2
object and, thus, it can be further modified (e.g., change themes, etc.) withggplot2
functions.
# for reproducibility
set.seed(123)
# model
mod <- stats::lm(formula = mpg ~ am * cyl, data = mtcars)
# plot
ggcoefstats(mod)
Defaults return
β
inferential statistics
β
estimate + CIs
β
model summary (AIC
and BIC)
This default plot can be further modified to oneβs liking with additional arguments (also, letβs use a different model now):
# for reproducibility
set.seed(123)
# model
mod <- MASS::rlm(formula = mpg ~ am * cyl, data = mtcars)
# plot
ggcoefstats(
x = mod,
point.args = list(color = "red", size = 3, shape = 15),
title = "Car performance predicted by transmission & cylinder count",
subtitle = "Source: 1974 Motor Trend US magazine",
exclude.intercept = TRUE,
ggtheme = hrbrthemes::theme_ipsum_ps()
) + # note the order in which the labels are entered
ggplot2::scale_y_discrete(labels = c("transmission", "cylinders", "interaction")) +
ggplot2::labs(x = "regression coefficient", y = NULL)
Most of the regression models that are supported in the underlying
packages are also supported by ggcoefstats
. For example-
aareg
, afex_aov
, anova
, anova.mlm
, anova
, aov
, aovlist
,
Arima
, bam
, bayesx
, bayesGARCH
, bayesQR
, BBmm
, BBreg
,
bcplm
, betamfx
, betaor
, BFBayesFactor
, BGGM
, bglmerMod
,
bife
, bigglm
, biglm
, blavaan
, bmlm
, blmerMod
, blrm
,
bracl
, brglm
, brglm2
, brmsfit
, brmultinom
, btergm
, cch
,
censReg
, cgam
, cgamm
, cglm
, clm
, clm2
, clmm
, clmm2
,
coeftest
, complmrob
, confusionMatrix
, coxme
, coxph
, coxr
,
coxph.penal
, cpglm
, cpglmm
, crch
, crq
, crr
, DirichReg
,
drc
, eglm
, elm
, emmGrid
, epi.2by2
, ergm
, feis
, felm
,
fitdistr
, fixest
, flexsurvreg
, gam
, Gam
, gamlss
, garch
,
geeglm
, gjrm
, glmc
, glmerMod
, glmmTMB
, gls
, glht
, glm
,
glmm
, glmmadmb
, glmmPQL
, glmRob
, glmrob
, glmx
, gmm
,
HLfit
, hurdle
, ivFixed
, ivprobit
, ivreg
, iv_robust
,
lavaan
, lm
, lm.beta
, lmerMod
, lmerModLmerTest
, lmodel2
,
lmRob
, lmrob
, lm_robust
, logitmfx
, logitor
, logitsf
,
LORgee
, lqm
, lqmm
, lrm
, manova
, maov
, margins
, mcmc
,
mcmc.list
, MCMCglmm
, mclogit
, mice
, mmclogit
, mediate
,
metafor
, merMod
, merModList
, metaplus
, mhurdle
, mixor
,
mjoint
, mle2
, mlm
, multinom
, mvord
, negbin
, negbinmfx
,
negbinirr
, nlmerMod
, nlrq
, nlreg
, nls
, orcutt
, orm
, plm
,
poissonmfx
, poissonirr
, polr
, probitmfx
, ridgelm
,
riskRegression
, rjags
, rlm
, rlmerMod
, robmixglm
, rq
, rqs
,
rqss
, rrvglm
, scam
, selection
, semLm
, semLme
, slm
,
speedglm
, speedlm
, stanfit
, stanreg
, summary.lm
, survreg
,
svyglm
, svy_vglm
, svyolr
, tobit
, truncreg
, varest
, vgam
,
vglm
, wbgee
, wblm
, zeroinfl
, etc.
Although not shown here, this function can also be used to carry out parametric, robust, and Bayesian random-effects meta-analysis.
graphical element | geom_ used |
argument for further modification |
---|---|---|
regression estimate | ggplot2::geom_point |
point.args |
error bars | ggplot2::geom_errorbarh |
errorbar.args |
vertical line | ggplot2::geom_vline |
vline.args |
label with statistical details | ggrepel::geom_label_repel |
stats.label.args |
Hypothesis testing and Effect size estimation
For a more exhaustive account of this function, see the associated vignette- https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggcoefstats.html
ggstatsplot
also offers a convenience function to extract dataframes
with statistical details that are used to create expressions displayed
in ggstatsplot
plots.
set.seed(123)
# a list of tibbles containing statistical analysis summaries
ggbetweenstats(mtcars, cyl, mpg) %>%
extract_stats()
#> $subtitle_data
#> # A tibble: 1 x 13
#> statistic df df.error p.value
#> <dbl> <dbl> <dbl> <dbl>
#> 1 31.6 2 18.0 0.00000127
#> method estimate conf.level
#> <chr> <dbl> <dbl>
#> 1 One-way analysis of means (not assuming equal variances) 0.744 0.95
#> conf.low conf.high effectsize conf.method conf.distribution expression
#> <dbl> <dbl> <chr> <chr> <chr> <list>
#> 1 0.475 0.853 Omega2 ncp F <language>
#>
#> $caption_data
#> # A tibble: 6 x 20
#> term estimate conf.level conf.low conf.high pd rope.percentage
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 mu 20.5 0.95 19.3 21.9 1 0
#> 2 cyl-4 5.90 0.95 4.11 7.52 1 0
#> 3 cyl-6 -0.704 0.95 -2.64 1.06 0.780 0.416
#> 4 cyl-8 -5.18 0.95 -6.76 -3.55 1 0
#> 5 sig2 11.0 0.95 6.24 18.3 1 0
#> 6 g_cyl 2.69 0.95 0.0911 18.7 1 0.0438
#> prior.distribution prior.location prior.scale component bf10
#> <chr> <dbl> <dbl> <chr> <dbl>
#> 1 cauchy 0 0.707 extra 3008850.
#> 2 cauchy 0 0.707 conditional 3008850.
#> 3 cauchy 0 0.707 conditional 3008850.
#> 4 cauchy 0 0.707 conditional 3008850.
#> 5 cauchy 0 0.707 extra 3008850.
#> 6 cauchy 0 0.707 extra 3008850.
#> method log_e_bf10 r2 std.dev r2.conf.level
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 Bayes factors for linear models 14.9 0.714 0.0503 0.95
#> 2 Bayes factors for linear models 14.9 0.714 0.0503 0.95
#> 3 Bayes factors for linear models 14.9 0.714 0.0503 0.95
#> 4 Bayes factors for linear models 14.9 0.714 0.0503 0.95
#> 5 Bayes factors for linear models 14.9 0.714 0.0503 0.95
#> 6 Bayes factors for linear models 14.9 0.714 0.0503 0.95
#> r2.conf.low r2.conf.high expression
#> <dbl> <dbl> <list>
#> 1 0.574 0.788 <language>
#> 2 0.574 0.788 <language>
#> 3 0.574 0.788 <language>
#> 4 0.574 0.788 <language>
#> 5 0.574 0.788 <language>
#> 6 0.574 0.788 <language>
#>
#> $pairwise_comparisons_data
#> # A tibble: 3 x 11
#> group1 group2 statistic p.value alternative method distribution
#> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr>
#> 1 4 6 -6.67 0.00110 two.sided Games-Howell test q
#> 2 4 8 -10.7 0.0000140 two.sided Games-Howell test q
#> 3 6 8 -7.48 0.000257 two.sided Games-Howell test q
#> p.adjustment test.details p.value.adjustment
#> <chr> <chr> <chr>
#> 1 none Games-Howell test Holm
#> 2 none Games-Howell test Holm
#> 3 none Games-Howell test Holm
#> label
#> <chr>
#> 1 list(~italic(p)[Holm-corrected]==0.001)
#> 2 list(~italic(p)[Holm-corrected]==1.4e-05)
#> 3 list(~italic(p)[Holm-corrected]==2.57e-04)
#>
#> $descriptive_data
#> NULL
#>
#> $one_sample_data
#> NULL
Note that all of this analysis is carried out by statsExpressions
package: https://indrajeetpatil.github.io/statsExpressions/
Sometimes you may not like the default plots produced by ggstatsplot
.
In such cases, you can use other custom plots (from ggplot2
or
other plotting packages) and still use ggstatsplot
functions to
display results from relevant statistical test.
For example, in the following chunk, we will create plot (ridgeplot)
using ggridges
package and use ggstatsplot
function for extracting
results.
# loading the needed libraries
set.seed(123)
library(ggridges)
library(ggplot2)
library(ggstatsplot)
# using `ggstatsplot` to get call with statistical results
stats_results <-
ggbetweenstats(
data = morley,
x = Expt,
y = Speed,
output = "subtitle"
)
# using `ggridges` to create plot
ggplot(morley, aes(x = Speed, y = as.factor(Expt), fill = as.factor(Expt))) +
geom_density_ridges(
jittered_points = TRUE,
quantile_lines = TRUE,
scale = 0.9,
alpha = 0.7,
vline_size = 1,
vline_color = "red",
point_size = 0.4,
point_alpha = 1,
position = position_raincloud(adjust_vlines = TRUE)
) + # adding annotations
labs(
title = "Michelson-Morley experiments",
subtitle = stats_results,
x = "Speed of light",
y = "Experiment number"
) + # remove the legend
theme(legend.position = "none")
-
No need to use scores of packages for statistical analysis (e.g., one to get stats, one to get effect sizes, another to get Bayes Factors, and yet another to get pairwise comparisons, etc.).
-
Minimal amount of code needed for all functions (typically only
data
,x
, andy
), which minimizes chances of error and makes for tidy scripts. -
Conveniently toggle between statistical approaches.
-
Truly makes your figures worth a thousand words.
-
No need to copy-paste results to the text editor (MS-Word, e.g.).
-
Disembodied figures stand on their own and are easy to evaluate for the reader.
-
More breathing room for theoretical discussion and other text.
-
No need to worry about updating figures and statistical details separately.
All functions produce publication-ready plots that require very few arguments if one finds the aesthetic and statistical defaults satisfying make the syntax much less cognitively demanding and easy to remember.
This package isβ¦
β an alternative to learning ggplot2
β
(The better you know
ggplot2
, the more you can modify the defaults to your liking.)
β meant to be used in talks/presentations
β
(Default plots can be
too complicated for effectively communicating results in
time-constrained presentation settings, e.g. conference talks.)
β the only game in town
β
(GUI software alternatives:
JASP and jamovi).
To make the maintenance and development of ggstatsplot
, the package
internally relies on the following packages that manage different
aspects of statistical analyses:
The statsExpressions
package forms the statistical backend that
processes data and creates expressions containing results from
statistical tests.
For more exhaustive documentation for this package, see: https://indrajeetpatil.github.io/statsExpressions/
The pairwiseComparisons
package forms the pairwise comparison backend
for creating results that are used to display post hoc multiple
comparisons displayed in ggbetweenstats
and ggwithinstats
functions.
For more exhaustive documentation for this package, see: https://indrajeetpatil.github.io/pairwiseComparisons/
In case you use the GUI software jamovi
,
you can install a module called
jjstatsplot
, which is a
wrapper around ggstatsplot
.
I would like to thank all the contributors to ggstatsplot
who pointed
out bugs or requested features I hadnβt considered. I would especially
like to thank other package developers (especially Daniel LΓΌdecke,
Dominique Makowski, Mattan S. Ben-Shachar, Patrick Mair, Salvatore
Mangiafico, etc.) who have patiently and diligently answered my
relentless number of questions and added feature requests I wanted. I
also want to thank Chuck Powell for his initial contributions to the
package.
The hexsticker was generously designed by Sarah Otterstetter (Max Planck
Institute for Human Development, Berlin). This package has also
benefited from the larger rstats
community on Twitter and
StackOverflow
.
Thanks are also due to my postdoc advisers (Mina Cikara and Fiery Cushman at Harvard University; Iyad Rahwan at Max Planck Institute for Human Development) who patiently supported me spending hundreds (?) of hours working on this package rather than what I was paid to do. π
Iβm happy to receive bug reports, suggestions, questions, and (most of
all) contributions to fix problems and add features. I personally prefer
using the GitHub
issues system over trying to reach out to me in other
ways (personal e-mail, Twitter, etc.). Pull Requests for contributions
are encouraged.
Here are some simple ways in which you can contribute (in the increasing order of commitment):
- Read and correct any inconsistencies in the documentation
- Raise issues about bugs or wanted features
- Review code
- Add new functionality (in the form of new plotting functions or helpers for preparing subtitles)
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.