Learn R Programming

⚠️There's a newer version (0.13.1) of this package.Take me there.

{ggstatsplot}: {ggplot2} Based Plots with Statistical Details

StatusUsageMiscellaneous

Raison d’être

“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.

Installation

TypeSourceCommand
ReleaseCRANinstall.packages("ggstatsplot")
DevelopmentGitHubremotes::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’

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

Citation

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.

Documentation and Examples

To see the detailed documentation for each function in the stable CRAN version of the package, see:

Summary of available plots

It, therefore, produces a limited kinds of plots for the supported analyses:

FunctionPlotDescriptionLifecycle
ggbetweenstatsviolin plotsfor comparisons between groups/conditions
ggwithinstatsviolin plotsfor comparisons within groups/conditions
gghistostatshistogramsfor distribution about numeric variable
ggdotplotstatsdot plots/chartsfor distribution about labeled numeric variable
ggscatterstatsscatterplotsfor correlation between two variables
ggcorrmatcorrelation matricesfor correlations between multiple variables
ggpiestatspie chartsfor categorical data
ggbarstatsbar chartsfor categorical data
ggcoefstatsdot-and-whisker plotsfor regression models and meta-analysis

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.

Summary of types of statistical analyses

The table below summarizes all the different types of analyses currently supported in this package-

FunctionsDescriptionParametricNon-parametricRobustBayesian
ggbetweenstatsBetween group/condition comparisons
ggwithinstatsWithin group/condition comparisons
gghistostats, ggdotplotstatsDistribution of a numeric variable
ggcorrmatCorrelation matrix
ggscatterstatsCorrelation between two variables
ggpiestats, ggbarstatsAssociation between categorical variables
ggpiestats, ggbarstatsEqual proportions for categorical variable levels
ggcoefstatsRegression model coefficients
ggcoefstatsRandom-effects meta-analysis

Summary of Bayesian analysis

AnalysisHypothesis testingEstimation
(one/two-sample) t-test
one-way ANOVA
correlation
(one/two-way) contingency table
random-effects meta-analysis

Statistical reporting

For all statistical tests reported in the plots, the default template abides by the gold standard for statistical reporting. For example, here are results from Yuen’s test for trimmed means (robust t-test):

Summary of statistical tests and effect sizes

Statistical analysis is carried out by {statsExpressions} package, and thus a summary table of all the statistical tests currently supported across various functions can be found in article for that package: https://indrajeetpatil.github.io/statsExpressions/articles/stats_details.html

Primary functions

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

ggbetweenstats

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          = "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!

Summary of graphics
graphical elementgeom_ usedargument for further modification
raw dataggplot2::geom_pointpoint.args
box plotggplot2::geom_boxplot
density plotggplot2::geom_violinviolin.args
centrality measure pointggplot2::geom_pointcentrality.point.args
centrality measure labelggrepel::geom_label_repelcentrality.label.args
outlier pointggplot2::stat_boxplot
outlier labelggrepel::geom_label_repeloutlier.label.args
pairwise comparisonsggsignif::geom_signifggsignif.args
Summary of tests

Central tendency measure

TypeMeasureFunction used
Parametricmeanparameters::describe_distribution
Non-parametricmedianparameters::describe_distribution
Robusttrimmed meanparameters::describe_distribution
BayesianMAP (maximum a posteriori probability) estimateparameters::describe_distribution

Hypothesis testing

TypeNo. of groupsTestFunction used
Parametric> 2Fisher’s or Welch’s one-way ANOVAstats::oneway.test
Non-parametric> 2Kruskal–Wallis one-way ANOVAstats::kruskal.test
Robust> 2Heteroscedastic one-way ANOVA for trimmed meansWRS2::t1way
Bayes Factor> 2Fisher’s ANOVABayesFactor::anovaBF
Parametric2Student’s or Welch’s t-teststats::t.test
Non-parametric2Mann–Whitney U teststats::wilcox.test
Robust2Yuen’s test for trimmed meansWRS2::yuen
Bayesian2Student’s t-testBayesFactor::ttestBF

Effect size estimation

TypeNo. of groupsEffect sizeCI?Function used
Parametric> 2, effectsize::omega_squared, effectsize::eta_squared
Non-parametric> 2effectsize::rank_epsilon_squared
Robust> 2 (Explanatory measure of effect size)WRS2::t1way
Bayes Factor> 2performance::r2_bayes
Parametric2Cohen’s d, Hedge’s geffectsize::cohens_d, effectsize::hedges_g
Non-parametric2r (rank-biserial correlation)effectsize::rank_biserial
Robust2 (Explanatory measure of effect size)WRS2::yuen.effect.ci
Bayesian2bayestestR::describe_posterior

Pairwise comparison tests

TypeEqual variance?Testp-value adjustment?Function used
ParametricNoGames-Howell teststats::pairwise.t.test
ParametricYesStudent’s t-testPMCMRplus::gamesHowellTest
Non-parametricNoDunn testPMCMRplus::kwAllPairsDunnTest
RobustNoYuen’s trimmed means testWRS2::lincon
Bayes FactorStudent’s t-testBayesFactor::ttestBF

For more, see the ggbetweenstats vignette: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggbetweenstats.html

ggwithinstats

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:

TypeMeasureFunction used
Parametricmeanparameters::describe_distribution
Non-parametricmedianparameters::describe_distribution
Robusttrimmed meanparameters::describe_distribution
BayesianMAP estimateparameters::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
)
Summary of graphics
graphical elementgeom_ usedargument for further modification
raw dataggplot2::geom_pointpoint.args
point pathggplot2::geom_pathpoint.path.args
box plotggplot2::geom_boxplotboxplot.args
density plotggplot2::geom_violinviolin.args
centrality measure pointggplot2::geom_pointcentrality.point.args
centrality measure point pathggplot2::geom_pathcentrality.path.args
centrality measure labelggrepel::geom_label_repelcentrality.label.args
outlier pointggplot2::stat_boxplot
outlier labelggrepel::geom_label_repeloutlier.label.args
pairwise comparisonsggsignif::geom_signifggsignif.args
Summary of tests

Central tendency measure

TypeMeasureFunction used
Parametricmeanparameters::describe_distribution
Non-parametricmedianparameters::describe_distribution
Robusttrimmed meanparameters::describe_distribution
BayesianMAP (maximum a posteriori probability) estimateparameters::describe_distribution

Hypothesis testing

TypeNo. of groupsTestFunction used
Parametric> 2One-way repeated measures ANOVAafex::aov_ez
Non-parametric> 2Friedman rank sum teststats::friedman.test
Robust> 2Heteroscedastic one-way repeated measures ANOVA for trimmed meansWRS2::rmanova
Bayes Factor> 2One-way repeated measures ANOVABayesFactor::anovaBF
Parametric2Student’s t-teststats::t.test
Non-parametric2Wilcoxon signed-rank teststats::wilcox.test
Robust2Yuen’s test on trimmed means for dependent samplesWRS2::yuend
Bayesian2Student’s t-testBayesFactor::ttestBF

Effect size estimation

TypeNo. of groupsEffect sizeCI?Function used
Parametric> 2, effectsize::omega_squared, effectsize::eta_squared
Non-parametric> 2 (Kendall’s coefficient of concordance)effectsize::kendalls_w
Robust> 2 (Algina-Keselman-Penfield robust standardized difference average)WRS2::wmcpAKP
Bayes Factor> 2performance::r2_bayes
Parametric2Cohen’s d, Hedge’s geffectsize::cohens_d, effectsize::hedges_g
Non-parametric2r (rank-biserial correlation)effectsize::rank_biserial
Robust2 (Algina-Keselman-Penfield robust standardized difference)WRS2::wmcpAKP
Bayesian2bayestestR::describe_posterior

Pairwise comparison tests

TypeTestp-value adjustment?Function used
ParametricStudent’s t-teststats::pairwise.t.test
Non-parametricDurbin-Conover testPMCMRplus::durbinAllPairsTest
RobustYuen’s trimmed means testWRS2::rmmcp
BayesianStudent’s t-testBayesFactor::ttestBF

For more, see the ggwithinstats vignette: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggwithinstats.html

gghistostats

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    = "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")
)
Summary of graphics
graphical elementgeom_ usedargument for further modification
histogram binggplot2::stat_binbin.args
centrality measure lineggplot2::geom_vlinecentrality.line.args
normality curveggplot2::stat_functionnormal.curve.args
Summary of tests

Central tendency measure

TypeMeasureFunction used
Parametricmeanparameters::describe_distribution
Non-parametricmedianparameters::describe_distribution
Robusttrimmed meanparameters::describe_distribution
BayesianMAP (maximum a posteriori probability) estimateparameters::describe_distribution

Hypothesis testing

TypeTestFunction used
ParametricOne-sample Student’s t-teststats::t.test
Non-parametricOne-sample Wilcoxon teststats::wilcox.test
RobustBootstrap-t method for one-sample testWRS2::trimcibt
BayesianOne-sample Student’s t-testBayesFactor::ttestBF

Effect size estimation

TypeEffect sizeCI?Function used
ParametricCohen’s d, Hedge’s geffectsize::cohens_d, effectsize::hedges_g
Non-parametricr (rank-biserial correlation)effectsize::rank_biserial
Robusttrimmed meanWRS2::trimcibt
Bayes FactorbayestestR::describe_posterior

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

ggdotplotstats

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    = "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")
)
Summary of graphics
graphical elementgeom_ usedargument for further modification
raw dataggplot2::geom_pointpoint.args
centrality measure lineggplot2::geom_vlinecentrality.line.args
Summary of tests

Central tendency measure

TypeMeasureFunction used
Parametricmeanparameters::describe_distribution
Non-parametricmedianparameters::describe_distribution
Robusttrimmed meanparameters::describe_distribution
BayesianMAP (maximum a posteriori probability) estimateparameters::describe_distribution

Hypothesis testing

TypeTestFunction used
ParametricOne-sample Student’s t-teststats::t.test
Non-parametricOne-sample Wilcoxon teststats::wilcox.test
RobustBootstrap-t method for one-sample testWRS2::trimcibt
BayesianOne-sample Student’s t-testBayesFactor::ttestBF

Effect size estimation

TypeEffect sizeCI?Function used
ParametricCohen’s d, Hedge’s geffectsize::cohens_d, effectsize::hedges_g
Non-parametricr (rank-biserial correlation)effectsize::rank_biserial
Robusttrimmed meanWRS2::trimcibt
Bayes FactorbayestestR::describe_posterior

ggscatterstats

This function creates a scatterplot with marginal distributions overlaid on the axes 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

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_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")
)
Summary of graphics
graphical elementgeom_ usedargument for further modification
raw dataggplot2::geom_pointpoint.args
labels for raw dataggrepel::geom_label_repelpoint.label.args
smooth lineggplot2::geom_smoothsmooth.line.args
marginal histogramsggside::geom_xsidehistogram, ggside::geom_ysidehistogramxsidehistogram.args, ysidehistogram.args
Summary of tests

Hypothesis testing and Effect size estimation

TypeTestCI?Function used
ParametricPearson’s correlation coefficientcorrelation::correlation
Non-parametricSpearman’s rank correlation coefficientcorrelation::correlation
RobustWinsorized Pearson correlation coefficientcorrelation::correlation
BayesianPearson’s correlation coefficientcorrelation::correlation

For more, see the ggscatterstats vignette: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggscatterstats.html

ggcorrmat

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 NAs

If there are NAs 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)

## tidy data as output
ggcorrmat(
  data   = dplyr::select(ggplot2::msleep, dplyr::matches("sleep|awake")),
  type   = "bayes",
  output = "dataframe"
)
##> # A tibble: 6 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.606    0.838  1    
##> 2 sleep_total sleep_cycle   -0.432       0.95   -0.681   -0.131  0.995
##> 3 sleep_total awake         -1.00        0.95   -1.00    -1.00   1    
##> 4 sleep_rem   sleep_cycle   -0.304       0.95   -0.576    0.0241 0.963
##> 5 sleep_rem   awake         -0.733       0.95   -0.832   -0.599  1    
##> 6 sleep_cycle awake          0.439       0.95    0.151    0.672  0.998
##>   rope.percentage prior.distribution prior.location prior.scale  bayes.factor
##>             <dbl> <chr>                       <dbl>       <dbl>         <dbl>
##> 1          0      beta                         1.41        1.41 3000790806.  
##> 2          0.0173 beta                         1.41        1.41          8.85
##> 3          0      beta                         1.41        1.41         NA   
##> 4          0.100  beta                         1.41        1.41          1.42
##> 5          0      beta                         1.41        1.41 3005546544.  
##> 6          0.015  beta                         1.41        1.41          8.85
##>   method                       n.obs
##>   <chr>                        <int>
##> 1 Bayesian Pearson correlation    61
##> 2 Bayesian Pearson correlation    32
##> 3 Bayesian Pearson correlation    83
##> 4 Bayesian Pearson correlation    32
##> 5 Bayesian Pearson correlation    61
##> 6 Bayesian Pearson correlation    32

Additionally, partial correlation are also supported:

## setup
set.seed(123)

## tidy data as output
ggcorrmat(
  data    = dplyr::select(ggplot2::msleep, dplyr::matches("sleep|awake")),
  type    = "bayes",
  partial = TRUE,
  output  = "dataframe"
)
##> # A tibble: 6 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.0938        0.95   -0.249     0.411 0.704
##> 2 sleep_total sleep_cycle  0.00194       0.95   -0.333     0.328 0.505
##> 3 sleep_total awake       -1             0.95   -1        -1     1    
##> 4 sleep_rem   sleep_cycle -0.0274        0.95   -0.357     0.300 0.558
##> 5 sleep_rem   awake        0.0889        0.95   -0.231     0.433 0.698
##> 6 sleep_cycle awake        0.00381       0.95   -0.335     0.324 0.508
##>   rope.percentage prior.distribution prior.location prior.scale bayes.factor
##>             <dbl> <chr>                       <dbl>       <dbl>        <dbl>
##> 1           0.378 beta                         1.41        1.41        0.311
##> 2           0.434 beta                         1.41        1.41        0.267
##> 3           0     beta                         1.41        1.41       NA    
##> 4           0.429 beta                         1.41        1.41        0.272
##> 5           0.386 beta                         1.41        1.41        0.309
##> 6           0.444 beta                         1.41        1.41        0.267
##>   method                       n.obs
##>   <chr>                        <int>
##> 1 Bayesian Pearson correlation    32
##> 2 Bayesian Pearson correlation    32
##> 3 Bayesian Pearson correlation    32
##> 4 Bayesian Pearson correlation    32
##> 5 Bayesian Pearson correlation    32
##> 6 Bayesian Pearson correlation    32
Summary of graphics
graphical elementgeom_ usedargument for further modification
correlation matrixggcorrplot::ggcorrplotggcorrplot.args
Summary of tests

Hypothesis testing and Effect size estimation

TypeTestCI?Function used
ParametricPearson’s correlation coefficientcorrelation::correlation
Non-parametricSpearman’s rank correlation coefficientcorrelation::correlation
RobustWinsorized Pearson correlation coefficientcorrelation::correlation
BayesianPearson’s correlation coefficientcorrelation::correlation

For examples and more information, see the ggcorrmat vignette: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggcorrmat.html

ggpiestats

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      = "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
)
Summary of graphics
graphical elementgeom_ usedargument for further modification
pie slicesggplot2::geom_col
descriptive labelsggplot2::geom_label/ggrepel::geom_label_repellabel.args
Summary of tests

two-way table

Hypothesis testing

TypeDesignTestFunction used
Parametric/Non-parametricUnpairedPearson’s teststats::chisq.test
BayesianUnpairedBayesian Pearson’s testBayesFactor::contingencyTableBF
Parametric/Non-parametricPairedMcNemar’s teststats::mcnemar.test
BayesianPaired

Effect size estimation

TypeDesignEffect sizeCI?Function used
Parametric/Non-parametricUnpairedCramer’s effectsize::cramers_v
BayesianUnpairedCramer’s effectsize::cramers_v
Parametric/Non-parametricPairedCohen’s effectsize::cohens_g
BayesianPaired

one-way table

Hypothesis testing

TypeTestFunction used
Parametric/Non-parametricGoodness of fit teststats::chisq.test
BayesianBayesian Goodness of fit test(custom)

Effect size estimation

TypeEffect sizeCI?Function used
Parametric/Non-parametricPearson’s effectsize::pearsons_c
Bayesian

For more, see the ggpiestats vignette: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggpiestats.html

ggbarstats

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)
)
Summary of graphics
graphical elementgeom_ usedargument for further modification
barsggplot2::geom_bar
descriptive labelsggplot2::geom_labellabel.args
Summary of tests

two-way table

Hypothesis testing

TypeDesignTestFunction used
Parametric/Non-parametricUnpairedPearson’s teststats::chisq.test
BayesianUnpairedBayesian Pearson’s testBayesFactor::contingencyTableBF
Parametric/Non-parametricPairedMcNemar’s teststats::mcnemar.test
BayesianPaired

Effect size estimation

TypeDesignEffect sizeCI?Function used
Parametric/Non-parametricUnpairedCramer’s effectsize::cramers_v
BayesianUnpairedCramer’s effectsize::cramers_v
Parametric/Non-parametricPairedCohen’s effectsize::cohens_g
BayesianPaired

one-way table

Hypothesis testing

TypeTestFunction used
Parametric/Non-parametricGoodness of fit teststats::chisq.test
BayesianBayesian Goodness of fit test(custom)

Effect size estimation

TypeEffect sizeCI?Function used
Parametric/Non-parametricPearson’s effectsize::pearsons_c
Bayesian

ggcoefstats

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 the F-statistic) or regression coefficients (for tests with t-, -, and z-statistic), etc. The function will, by default, display a helpful x-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.) with {ggplot2} functions.

## for reproducibility
set.seed(123)

## model
mod <- stats::lm(formula = mpg ~ am * cyl, data = mtcars)

## plot
ggcoefstats(mod, ggtheme = hrbrthemes::theme_ipsum_ps())

Defaults return

✅ inferential statistics ✅ estimate + CIs ✅ model summary (AIC and BIC)

Supported models

Most of the regression models that are supported in the underlying packages are also supported by ggcoefstats.

insight::supported_models()
##>   [1] "aareg"             "afex_aov"          "AKP"              
##>   [4] "Anova.mlm"         "aov"               "aovlist"          
##>   [7] "Arima"             "averaging"         "bamlss"           
##>  [10] "bamlss.frame"      "bayesQR"           "bayesx"           
##>  [13] "BBmm"              "BBreg"             "bcplm"            
##>  [16] "betamfx"           "betaor"            "betareg"          
##>  [19] "BFBayesFactor"     "bfsl"              "BGGM"             
##>  [22] "bife"              "bifeAPEs"          "bigglm"           
##>  [25] "biglm"             "blavaan"           "blrm"             
##>  [28] "bracl"             "brglm"             "brmsfit"          
##>  [31] "brmultinom"        "btergm"            "censReg"          
##>  [34] "cgam"              "cgamm"             "cglm"             
##>  [37] "clm"               "clm2"              "clmm"             
##>  [40] "clmm2"             "clogit"            "coeftest"         
##>  [43] "complmrob"         "confusionMatrix"   "coxme"            
##>  [46] "coxph"             "coxph.penal"       "coxr"             
##>  [49] "cpglm"             "cpglmm"            "crch"             
##>  [52] "crq"               "crqs"              "crr"              
##>  [55] "dep.effect"        "DirichletRegModel" "drc"              
##>  [58] "eglm"              "elm"               "epi.2by2"         
##>  [61] "ergm"              "feglm"             "feis"             
##>  [64] "felm"              "fitdistr"          "fixest"           
##>  [67] "flexsurvreg"       "gam"               "Gam"              
##>  [70] "gamlss"            "gamm"              "gamm4"            
##>  [73] "garch"             "gbm"               "gee"              
##>  [76] "geeglm"            "glht"              "glimML"           
##>  [79] "glm"               "Glm"               "glmm"             
##>  [82] "glmmadmb"          "glmmPQL"           "glmmTMB"          
##>  [85] "glmrob"            "glmRob"            "glmx"             
##>  [88] "gls"               "gmnl"              "HLfit"            
##>  [91] "htest"             "hurdle"            "iv_robust"        
##>  [94] "ivFixed"           "ivprobit"          "ivreg"            
##>  [97] "lavaan"            "lm"                "lm_robust"        
##> [100] "lme"               "lmerMod"           "lmerModLmerTest"  
##> [103] "lmodel2"           "lmrob"             "lmRob"            
##> [106] "logistf"           "logitmfx"          "logitor"          
##> [109] "LORgee"            "lqm"               "lqmm"             
##> [112] "lrm"               "manova"            "MANOVA"           
##> [115] "margins"           "maxLik"            "mclogit"          
##> [118] "mcmc"  

Copy Link

Version

Install

install.packages('ggstatsplot')

Monthly Downloads

9,145

Version

0.9.1

License

GPL-3 | file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Indrajeet Patil

Last Published

May 10th, 2025

Functions in ggstatsplot (0.9.1)

ggdotplotstats

Dot plot/chart for labeled numeric data.
ggpiestats

Pie charts with statistical tests
grouped_gghistostats

Grouped histograms for distribution of a numeric variable
grouped_ggdotplotstats

Grouped histograms for distribution of a labeled numeric variable
grouped_ggbarstats

Grouped bar charts with statistical tests
grouped_ggbetweenstats

Violin plots for group or condition comparisons in between-subjects designs repeated across all levels of a grouping variable.
gghistostats

Histogram for distribution of a numeric variable
grouped_ggcorrmat

Visualization of a correlalogram (or correlation matrix) for all levels of a grouping variable
ggwithinstats

Box/Violin plots for within-subjects (or repeated measures) comparisons
ggscatterstats

Scatterplot with marginal distributions and statistical results
ggstatsplot-package

ggstatsplot: 'ggplot2' Based Plots with Statistical Details
theme_ggstatsplot

Default theme used in {ggstatsplot}
grouped_ggscatterstats

Scatterplot with marginal distributions for all levels of a grouping variable
grouped_ggwithinstats

Violin plots for group or condition comparisons in within-subjects designs repeated across all levels of a grouping variable.
grouped_ggpiestats

Grouped pie charts with statistical tests
grouped_list

Split dataframe into a list by grouping variable.
pairwise_comparisons

Multiple pairwise comparison tests with tidy data
reexports

Objects exported from other packages
iris_long

Edgar Anderson's Iris Data in long format.
movies_long

Movie information and user ratings from IMDB.com (long format).
ggbetweenstats

Box/Violin plots for between-subjects comparisons
ggcoefstats

Dot-and-whisker plots for regression analyses
extract_stats

Extracting dataframes with statistical details from {ggstatsplot}
ggbarstats

Bar (column) charts with statistical tests
Titanic_full

Titanic dataset.
bugs_long

Tidy version of the "Bugs" dataset.
bugs_wide

Wide-format version of the "Bugs" dataset.
combine_plots

Combining and arranging multiple plots in a grid
ggcorrmat

Visualization of a correlation matrix