/
README.Rmd
1100 lines (847 loc) · 42.6 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo=FALSE, warning=FALSE, message=FALSE}
## show me all columns
options(
tibble.width = Inf,
pillar.bold = TRUE,
pillar.neg = TRUE,
pillar.subtle_num = TRUE,
pillar.min_chars = Inf
)
knitr::opts_chunk$set(
collapse = TRUE,
dpi = 300, ## change to 300 once on CRAN
warning = FALSE,
message = FALSE,
out.width = "100%",
comment = "#>",
fig.path = "man/figures/README-"
)
library(ggstatsplot)
```
## `{ggstatsplot}`: `{ggplot2}` Based Plots with Statistical Details
Status | Usage| Miscellaneous
----------------- | ----------------- | -----------------
[![R build status](https://github.com/IndrajeetPatil/ggstatsplot/workflows/R-CMD-check/badge.svg)](https://github.com/IndrajeetPatil/ggstatsplot) | [![Total downloads badge](https://cranlogs.r-pkg.org/badges/grand-total/ggstatsplot?color=blue)](https://CRAN.R-project.org/package=ggstatsplot) | [![Codecov](https://codecov.io/gh/IndrajeetPatil/ggstatsplot/branch/master/graph/badge.svg)](https://app.codecov.io/gh/IndrajeetPatil/ggstatsplot?branch=master)
[![lints](https://github.com/IndrajeetPatil/ggstatsplot/workflows/lint/badge.svg)](https://github.com/IndrajeetPatil/ggstatsplot) | [![Daily downloads badge](https://cranlogs.r-pkg.org/badges/last-day/ggstatsplot?color=blue)](https://CRAN.R-project.org/package=ggstatsplot) | [![status](https://tinyverse.netlify.com/badge/ggstatsplot)](https://CRAN.R-project.org/package=ggstatsplot)
[![pkgdown](https://github.com/IndrajeetPatil/ggstatsplot/workflows/pkgdown/badge.svg)](https://github.com/IndrajeetPatil/ggstatsplot/actions) | [![DOI](https://joss.theoj.org/papers/10.21105/joss.03167/status.svg)](https://doi.org/10.21105/joss.03167) | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
## Raison d'être <img src="man/figures/logo.png" align="right" width="360" />
> "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}`](https://indrajeetpatil.github.io/ggstatsplot/) is an extension
of [`{ggplot2}`](https://github.com/tidyverse/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
Type | Source | Command
---|---|---
Release | [![CRAN Status](https://www.r-pkg.org/badges/version/ggstatsplot)](https://cran.r-project.org/package=ggstatsplot) | `install.packages("ggstatsplot")`
Development | [![Project Status](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/##active) | `remotes::install_github("IndrajeetPatil/ggstatsplot")`
## 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:
```{r citation, comment=""}
citation("ggstatsplot")
```
## Acknowledgments
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, Brenton Wiernik, Patrick Mair, Salvatore Mangiafico, etc.) who
have patiently and diligently answered my relentless questions and supported
feature requests in their projects. 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, LinkedIn, 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. 😁
## Documentation and Examples
To see the detailed documentation for each function in the stable **CRAN**
version of the package, see:
- [Publication](https://joss.theoj.org/papers/10.21105/joss.03167)
- [Vignettes](https://indrajeetpatil.github.io/ggstatsplot/articles/)
- [Presentation](https://indrajeetpatil.github.io/ggstatsplot_slides/slides/ggstatsplot_presentation.html#1)
## Summary of available plots
It, therefore, produces a limited kinds of plots for the supported analyses:
Function | Plot | Description | Lifecycle
------- | ---------- | ------------------------- | ----
`ggbetweenstats` | **violin plots** | for comparisons *between* groups/conditions | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
`ggwithinstats` | **violin plots** | for comparisons *within* groups/conditions | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
`gghistostats` | **histograms** | for distribution about numeric variable | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
`ggdotplotstats` | **dot plots/charts** | for distribution about labeled numeric variable | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
`ggscatterstats` | **scatterplots** | for correlation between two variables | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
`ggcorrmat` | **correlation matrices** | for correlations between multiple variables | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
`ggpiestats` | **pie charts** | for categorical data | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
`ggbarstats` | **bar charts** | for categorical data | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
`ggcoefstats` | **dot-and-whisker plots** | for regression models and meta-analysis | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
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-
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 | ✅ | ✅
## 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):
<img src="man/figures/stats_reporting_format.png" align="center" />
## 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
### `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-
```{r ggbetweenstats1}
set.seed(123)
ggbetweenstats(
data = iris,
x = Species,
y = Sepal.Length,
title = "Distribution of sepal length across Iris species"
)
```
**Defaults** return<br>
✅ raw data + distributions <br>
✅ descriptive statistics <br>
✅ inferential statistics <br>
✅ effect size + CIs <br>
✅ pairwise comparisons <br>
✅ Bayesian hypothesis-testing <br>
✅ Bayesian estimation <br>
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:
```{r ggbetweenstats2, fig.height=8, fig.width=12}
set.seed(123)
grouped_ggbetweenstats(
data = dplyr::filter(movies_long, genre %in% c("Action", "Comedy")),
x = mpaa,
y = length,
grouping.var = genre,
outlier.tagging = TRUE,
outlier.label = title,
outlier.coef = 2,
ggsignif.args = list(textsize = 4, tip_length = 0.01),
p.adjust.method = "bonferroni",
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 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_signif` | `ggsignif.args`
##### Summary of tests
**Central tendency measure**
Type | Measure | Function used
----------- | --------- | ------------------
Parametric | mean | `datawizard::describe_distribution`
Non-parametric | median | `datawizard::describe_distribution`
Robust | trimmed mean | `datawizard::describe_distribution`
Bayesian | MAP (maximum *a posteriori* probability) estimate | `datawizard::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**
Type | No. of groups | Effect size | CI? | Function used
----------- | --- | ------------------------- | --- | -----
Parametric | > 2 | $\eta_{p}^2$, $\omega_{p}^2$ | ✅ | `effectsize::omega_squared`, `effectsize::eta_squared`
Non-parametric | > 2 | $\epsilon_{ordinal}^2$ | ✅ | `effectsize::rank_epsilon_squared`
Robust | > 2 | $\xi$ (Explanatory measure of effect size) | ✅ | `WRS2::t1way`
Bayes Factor | > 2 | $R_{posterior}^2$ | ✅ | `performance::r2_bayes`
Parametric | 2 | Cohen's *d*, Hedge's *g* | ✅ | `effectsize::cohens_d`, `effectsize::hedges_g`
Non-parametric | 2 | *r* (rank-biserial correlation) | ✅ | `effectsize::rank_biserial`
Robust | 2 | $\xi$ (Explanatory measure of effect size) | ✅ | `WRS2::yuen.effect.ci`
Bayesian | 2 | $\delta_{posterior}$ | ✅ | `bayestestR::describe_posterior`
**Pairwise comparison tests**
Type | Equal variance? | Test | *p*-value adjustment? | Function used
----------- | --- | ------------------------- | --- | -----
Parametric | No | Games-Howell test | ✅ | `PMCMRplus::gamesHowellTest`
Parametric | Yes | Student's *t*-test | ✅ | `stats::pairwise.t.test`
Non-parametric | No | Dunn test | ✅ | `PMCMRplus::kwAllPairsDunnTest`
Robust | No | Yuen's trimmed means test | ✅ | `WRS2::lincon`
Bayesian | `NA` | Student's *t*-test | `NA` | `BayesFactor::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.
```{r ggwithinstats1, fig.width=8, fig.height=6}
set.seed(123)
library(WRS2) ## for data
library(afex) ## to run anova
ggwithinstats(
data = WineTasting,
x = Wine,
y = Taste,
title = "Wine tasting",
ggtheme = hrbrthemes::theme_ipsum_es()
)
```
**Defaults** return<br>
✅ raw data + distributions <br>
✅ descriptive statistics <br>
✅ inferential statistics <br>
✅ effect size + CIs <br>
✅ pairwise comparisons <br>
✅ Bayesian hypothesis-testing <br>
✅ Bayesian estimation <br>
The central tendency measure displayed will depend on the statistics:
Type | Measure | Function used
----------- | --------- | ------------------
Parametric | mean | `datawizard::describe_distribution`
Non-parametric | median | `datawizard::describe_distribution`
Robust | trimmed mean | `datawizard::describe_distribution`
Bayesian | MAP estimate | `datawizard::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-
```{r ggwithinstats2, fig.height=6, fig.width=14}
set.seed(123)
grouped_ggwithinstats(
data = dplyr::filter(bugs_long, region %in% c("Europe", "North America"), condition %in% c("LDLF", "LDHF")),
x = condition,
y = desire,
type = "np",
xlab = "Condition",
ylab = "Desire to kill an artrhopod",
grouping.var = region,
outlier.tagging = TRUE,
outlier.label = education
)
```
##### Summary of graphics
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` | `boxplot.args`
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_signif` | `ggsignif.args`
##### Summary of tests
**Central tendency measure**
Type | Measure | Function used
----------- | --------- | ------------------
Parametric | mean | `datawizard::describe_distribution`
Non-parametric | median | `datawizard::describe_distribution`
Robust | trimmed mean | `datawizard::describe_distribution`
Bayesian | MAP (maximum *a posteriori* probability) estimate | `datawizard::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**
Type | No. of groups | Effect size | CI? | Function used
----------- | --- | ------------------------- | --- | -----
Parametric | > 2 | $\eta_{p}^2$, $\omega_{p}^2$ | ✅ | `effectsize::omega_squared`, `effectsize::eta_squared`
Non-parametric | > 2 | $W_{Kendall}$ (Kendall's coefficient of concordance) | ✅ | `effectsize::kendalls_w`
Robust | > 2 | $\delta_{R-avg}^{AKP}$ (Algina-Keselman-Penfield robust standardized difference average) | ✅ | `WRS2::wmcpAKP`
Bayes Factor | > 2 | $R_{Bayesian}^2$ | ✅ | `performance::r2_bayes`
Parametric | 2 | Cohen's *d*, Hedge's *g* | ✅ | `effectsize::cohens_d`, `effectsize::hedges_g`
Non-parametric | 2 | *r* (rank-biserial correlation) | ✅ | `effectsize::rank_biserial`
Robust | 2 | $\delta_{R}^{AKP}$ (Algina-Keselman-Penfield robust standardized difference) | ✅ | `WRS2::wmcpAKP`
Bayesian | 2 | $\delta_{posterior}$ | ✅ | `bayestestR::describe_posterior`
**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>
### `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.
```{r gghistostats1, fig.width=8}
set.seed(123)
gghistostats(
data = ggplot2::msleep,
x = awake,
title = "Amount of time spent awake",
test.value = 12,
binwidth = 1,
ggtheme = hrbrthemes::theme_ipsum_tw()
)
```
**Defaults** return<br>
✅ counts + proportion for bins<br>
✅ descriptive statistics <br>
✅ inferential statistics <br>
✅ effect size + CIs <br>
✅ Bayesian hypothesis-testing <br>
✅ Bayesian estimation <br>
There is also a `grouped_` variant of this function that makes it
easy to repeat the same operation across a **single** grouping variable:
```{r gghistostats2, fig.height=6, fig.width=12}
set.seed(123)
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,
normal.curve = TRUE,
normal.curve.args = list(color = "red", size = 1),
ggtheme = ggthemes::theme_tufte(),
## modify the defaults from `{ggstatsplot}` for each plot
plotgrid.args = list(nrow = 1),
annotation.args = list(title = "Movies budgets for different genres")
)
```
##### Summary of graphics
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`
##### Summary of tests
**Central tendency measure**
Type | Measure | Function used
----------- | --------- | ------------------
Parametric | mean | `datawizard::describe_distribution`
Non-parametric | median | `datawizard::describe_distribution`
Robust | trimmed mean | `datawizard::describe_distribution`
Bayesian | MAP (maximum *a posteriori* probability) estimate | `datawizard::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**
Type | Effect size | CI? | Function used
------------ | ----------------------- | --- | -----
Parametric | Cohen's *d*, Hedge's *g* | ✅ | `effectsize::cohens_d`, `effectsize::hedges_g`
Non-parametric | *r* (rank-biserial correlation) | ✅ | `effectsize::rank_biserial`
Robust | trimmed mean | ✅ | `WRS2::trimcibt`
Bayes Factor | $\delta_{posterior}$ | ✅ | `bayestestR::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.
```{r ggdotplotstats1, fig.height=10, fig.width=8}
set.seed(123)
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"
)
```
**Defaults** return<br>
✅ descriptives (mean + sample size) <br>
✅ inferential statistics <br>
✅ effect size + CIs <br>
✅ Bayesian hypothesis-testing <br>
✅ Bayesian estimation <br>
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.
```{r ggdotplotstats2, fig.height=6, fig.width=12}
set.seed(123)
grouped_ggdotplotstats(
data = dplyr::filter(ggplot2::mpg, cyl %in% c("4", "6")),
x = cty,
y = manufacturer,
type = "bayes",
xlab = "city miles per gallon",
ylab = "car manufacturer",
grouping.var = cyl,
test.value = 15.5,
point.args = list(color = "red", size = 5, shape = 13),
annotation.args = list(title = "Fuel economy data")
)
```
##### Summary of graphics
graphical element | `geom_` used | argument for further modification
--------- | ------- | --------------------------
raw data | `ggplot2::geom_point` | `point.args`
centrality measure line | `ggplot2::geom_vline` | `centrality.line.args`
##### Summary of tests
**Central tendency measure**
Type | Measure | Function used
----------- | --------- | ------------------
Parametric | mean | `datawizard::describe_distribution`
Non-parametric | median | `datawizard::describe_distribution`
Robust | trimmed mean | `datawizard::describe_distribution`
Bayesian | MAP (maximum *a posteriori* probability) estimate | `datawizard::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**
Type | Effect size | CI? | Function used
------------ | ----------------------- | --- | -----
Parametric | Cohen's *d*, Hedge's *g* | ✅ | `effectsize::cohens_d`, `effectsize::hedges_g`
Non-parametric | *r* (rank-biserial correlation) | ✅ | `effectsize::rank_biserial`
Robust | trimmed mean | ✅ | `WRS2::trimcibt`
Bayes Factor | $\delta_{posterior}$ | ✅ | `bayestestR::describe_posterior`
### `ggscatterstats`
This function creates a scatterplot with marginal distributions overlaid on the
axes and results from statistical tests in the subtitle:
```{r ggscatterstats1, fig.height=6}
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<br>
✅ raw data + distributions <br>
✅ marginal distributions <br>
✅ inferential statistics <br>
✅ effect size + CIs <br>
✅ Bayesian hypothesis-testing <br>
✅ Bayesian estimation <br>
There is also a `grouped_` variant of this function that makes it
easy to repeat the same operation across a **single** grouping variable.
```{r ggscatterstats2, fig.height=8, fig.width=14}
set.seed(123)
grouped_ggscatterstats(
data = dplyr::filter(movies_long, genre %in% c("Action", "Comedy")),
x = rating,
y = length,
grouping.var = genre,
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 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 histograms | `ggside::geom_xsidehistogram`, `ggside::geom_ysidehistogram` | `xsidehistogram.args`, `ysidehistogram.args`
##### Summary of tests
**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`
`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.
```{r ggcorrmat1}
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<br>
✅ effect size + significance<br>
✅ 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:
```{r ggcorrmat2, fig.height=6, fig.width=10}
set.seed(123)
grouped_ggcorrmat(
data = dplyr::filter(movies_long, genre %in% c("Action", "Comedy")),
type = "robust",
colors = c("#cbac43", "white", "#550000"),
grouping.var = genre,
matrix.type = "lower"
)
```
##### Summary of graphics
graphical element | `geom_` used | argument for further modification
--------- | ------- | --------------------------
correlation matrix | `ggcorrplot::ggcorrplot` | `ggcorrplot.args`
##### Summary of tests
**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>
### `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:
```{r ggpiestats1, fig.height=4, fig.width=8}
set.seed(123)
ggpiestats(
data = mtcars,
x = am,
y = cyl,
package = "wesanderson",
palette = "Royal1",
title = "Dataset: Motor Trend Car Road Tests",
legend.title = "Transmission"
)
```
**Defaults** return<br>
✅ descriptives (frequency + %s) <br>
✅ inferential statistics <br>
✅ effect size + CIs <br>
✅ Goodness-of-fit tests <br>
✅ Bayesian hypothesis-testing <br>
✅ Bayesian estimation <br>
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:
```{r ggpiestats2, fig.height=6, fig.width=10}
set.seed(123)
grouped_ggpiestats(
data = mtcars,
x = cyl,
grouping.var = am,
label.repel = TRUE,
package = "ggsci",
palette = "default_ucscgb"
)
```
##### Summary of graphics
graphical element | `geom_` used | argument for further modification
--------- | ------- | --------------------------
pie slices | `ggplot2::geom_col` | ❌
descriptive labels | `ggplot2::geom_label`/`ggrepel::geom_label_repel` | `label.args`
##### Summary of tests
**two-way table**
**Hypothesis testing**
Type | Design | Test | Function used
----------- | ----- | ------------------------- | -----
Parametric/Non-parametric | Unpaired | Pearson's $\chi^2$ test | `stats::chisq.test`
Bayesian | Unpaired | Bayesian Pearson's $\chi^2$ test | `BayesFactor::contingencyTableBF`
Parametric/Non-parametric | Paired | McNemar's $\chi^2$ test | `stats::mcnemar.test`
Bayesian | Paired | ❌ | ❌
**Effect size estimation**
Type | Design | Effect size | CI? | Function used
----------- | ----- | ------------------------- | --- | -----
Parametric/Non-parametric | Unpaired | Cramer's $V$ | ✅ | `effectsize::cramers_v`
Bayesian | Unpaired | Cramer's $V$ | ✅ | `effectsize::cramers_v`
Parametric/Non-parametric | Paired | Cohen's $g$ | ✅ | `effectsize::cohens_g`
Bayesian | Paired | ❌ | ❌ | ❌
**one-way table**
**Hypothesis testing**
Type | Test | Function used
----------- | ------------------------- | -----
Parametric/Non-parametric | Goodness of fit $\chi^2$ test | `stats::chisq.test`
Bayesian | Bayesian Goodness of fit $\chi^2$ test | (custom)
**Effect size estimation**
Type | Effect size | CI? | Function used
----------- | ------------------------- | --- | -----
Parametric/Non-parametric | Pearson's $C$ | ✅ | `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.
```{r ggbarstats1, fig.height=8, fig.width=10}
set.seed(123)
library(ggplot2)
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<br>
✅ descriptives (frequency + %s) <br>
✅ inferential statistics <br>
✅ effect size + CIs <br>
✅ Goodness-of-fit tests <br>
✅ Bayesian hypothesis-testing <br>
✅ Bayesian estimation <br>
And, needless to say, there is also a `grouped_` variant of this function-
```{r ggbarstats2, fig.height=6, fig.width=12}
## setup
set.seed(123)
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 element | `geom_` used | argument for further modification
--------- | ------- | --------------------------
bars | `ggplot2::geom_bar` | ❌
descriptive labels | `ggplot2::geom_label` | `label.args`
##### Summary of tests
**two-way table**
**Hypothesis testing**
Type | Design | Test | Function used
----------- | ----- | ------------------------- | -----
Parametric/Non-parametric | Unpaired | Pearson's $\chi^2$ test | `stats::chisq.test`
Bayesian | Unpaired | Bayesian Pearson's $\chi^2$ test | `BayesFactor::contingencyTableBF`
Parametric/Non-parametric | Paired | McNemar's $\chi^2$ test | `stats::mcnemar.test`
Bayesian | Paired | ❌ | ❌
**Effect size estimation**
Type | Design | Effect size | CI? | Function used
----------- | ----- | ------------------------- | --- | -----
Parametric/Non-parametric | Unpaired | Cramer's $V$ | ✅ | `effectsize::cramers_v`
Bayesian | Unpaired | Cramer's $V$ | ✅ | `effectsize::cramers_v`
Parametric/Non-parametric | Paired | Cohen's $g$ | ✅ | `effectsize::cohens_g`
Bayesian | Paired | ❌ | ❌ | ❌
**one-way table**
**Hypothesis testing**
Type | Test | Function used
----------- | ------------------------- | -----
Parametric/Non-parametric | Goodness of fit $\chi^2$ test | `stats::chisq.test`
Bayesian | Bayesian Goodness of fit $\chi^2$ test | (custom)
**Effect size estimation**
Type | Effect size | CI? | Function used
----------- | ------------------------- | --- | -----
Parametric/Non-parametric | Pearson's $C$ | ✅ | `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`-, $\chi^{2}$-, 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.e
- 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) with `{ggplot2}` functions.
```{r ggcoefstats1, fig.height=5, fig.width=6}
set.seed(123)
## model
mod <- stats::lm(formula = mpg ~ am * cyl, data = mtcars)
ggcoefstats(mod, ggtheme = hrbrthemes::theme_ipsum_ps())
```
**Defaults** return<br>
✅ inferential statistics <br>
✅ estimate + CIs <br>
✅ model summary (AIC and BIC) <br>
##### Supported models
Most of the regression models that are supported in the underlying packages are
also supported by `ggcoefstats`.
```{r supported}
insight::supported_models()
```
Although not shown here, this function can also be used to carry out parametric,
robust, and Bayesian random-effects meta-analysis.
##### Summary of graphics
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`
##### Summary of meta-analysis tests
**Hypothesis testing** and **Effect size estimation**
Type | Test | Effect size | CI? | Function used
----------- | -------------------- | -------- | --- | -----
Parametric | Meta-analysis via random-effects models | $\beta$ | ✅ | `metafor::metafor`
Robust | Meta-analysis via robust random-effects models | $\beta$ | ✅ | `metaplus::metaplus`
Bayes | Meta-analysis via Bayesian random-effects models | $\beta$ | ✅ | `metaBMA::meta_random`
For a more exhaustive account of this function, see the associated vignette-
<https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggcoefstats.html>
### Extracting dataframes with statistical details
`{ggstatsplot}` also offers a convenience function to extract dataframes with
statistical details that are used to create expressions displayed in
`{ggstatsplot}` plots.
```{r extract_stats}