Introduction to ggpiestats

The function ggstatsplot::ggpiestats can be used for quick data exploration and/or to prepare publication-ready pie charts to summarize the statistical relationship(s) among one or more categorical variables. We will see examples of how to use this function in this vignette.

To begin with, here are some instances where you would want to use ggpiestats-

  • to check if the proportion of observations matches our hypothesized proportion, this is typically known as a “Goodness of Fit” test

  • to see if the frequency distribution of two categorical variables are independent of each other using the contingency table analysis

  • to check if the proportion of observations at each level of a categorical variable is equal

Note: The following demo uses the pipe operator (%>%), if you are not familiar with this operator, here is a good explanation: http://r4ds.had.co.nz/pipes.html.

ggpiestats only works with data organized in dataframes or tibbles. It will not work with other data structures like base R tables or matrices. It can operate on dataframes that are organized with one row per observation or dataframes that have one column containing counts. This vignette provides examples of both (see examples below).

To help demonstrate how ggpiestats can be used with categorical (also known as nominal) data, a modified version of the original Titanic dataset (from the datasets library) has been provided in the ggstatsplot package with the name Titanic_full. The Titanic Passenger Survival Dataset provides information “on the fate of passengers on the fatal maiden voyage of the ocean liner Titanic, including economic status (class), sex, age, and survival.”

Let’s have a look at the structure of both.

Goodness of Fit with ggpiestats

The simplest use case for ggpiestats is that we want to display information about one categorical or nominal variable. As part of that display or plot, we may also choose to execute a chi-squared goodness of fit test to see whether the proportions (or percentages) in categories of the single variable appear to line up with our hypothesis or model. To start simple and then expand, let’s say that we’d like to display a piechart with the percentages of passengers who did or did not survive. Our initial hypothesis is that it was no different than flipping a coin. People had a 50/50 chance of surviving.

Note: equal proportions per category are the default, e.g. 50/50, but you can specify any hypothesized ratio you like with ratio so if our hypothesis was that 80% died and 20% survived we would add ratio = c(.80,.20) when we entered the code.

Let’s move on to a more complex example statistically and in terms of the features we will use in ggpiestats

Independence (or association) with ggpiestats

Let’s next investigate whether the passenger’s gender was independent of, or associated with, gender. The test is whether the proportion of people who survived was different between the sexes using ggpiestats.

We’ll modify a number of arguments to change the appearance of this plot and showcase the flexibility of ggpiestats. We will:

  1. Change the plot theme to ggplot2::theme_grey()

  2. Change our color palette to category10_d3 from ggsci package

  3. We’ll customize the subtitle by being more precise about which chi squared test this is stat.title = "chi squared test of independence: "

  4. Finally, we’ll make a call to ggplot2 to modify the size of our plot title and to make it right justified

The plot clearly shows that survival rates were very different between males and females. The Pearson’s chi-square test of independence is significant given our large sample size. Additionally, for both females and males, the survival rates were significantly different than 50% as indicated by the *** which is equivalent to a goodness of fit test for each gender.

Grouped analysis with grouped_ggpiestats

What if we want to do the same analysis of gender but also factor in the passenger’s age (Age)? We have information that classifies the passengers as Child or Adult, perhaps that makes a difference to their survival rate? We could write a for loop or use purrr, but ggstatsplot provides a special helper function for such instances: grouped_ggpiestats.

It is a convenient wrapper function around ggstatsplot::combine_plots. It applies ggpiestats across all levels of a specified grouping variable and then combines the list of individual plots into a single plot. Note that the grouping variable can be anything: conditions in a given study, groups in a study sample, different studies, etc.

The resulting pie charts and statistics make the story clear. For adults gender very much matters. Women survived at much higher rates than men. For children gender is not significantly associated with survival and both male and female children have a survival rate that is not significantly different from 50/50.

Grouped analysis with ggpiestats + purrr

Although grouped_ggpiestats provides a quick way to explore the data, it leaves much to be desired. For example, we may want to add different captions, titles, themes, or palettes for each level of the grouping variable, etc. For cases like these, it would be better to use purrr package.

See the associated vignette here: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/purrr_examples.html

Working with data organized by counts

ggpiestats can also work with dataframe containing counts (aka tabled data), i.e., when each row doesn’t correspond to a unique observation. For example, consider the following notional fishing dataframe containing data from two boats (A and B) about the number of different types fish they caught in the months of February and March. In this dataframe, each row corresponds to a unique combination of Boat and Month.

When the data is organized this way, we make a slightly different call to the ggpiestats function: we use the counts argument. If we want to investigate the relationship of type of fish by month (a test of independence), our command would be:

The results support our hypothesis that the type of fish caught is related to the month in which we’re fishing. The chi squared independence test results at the top of the plot. In February we catch significantly more Haddock than we would hypothesize for an equal distribution. Whereas in March our results indicate there’s no strong evidence that the distribution isn’t equal.

Within-subjects designs

For our final example let’s imagine we’re conducting clinical trials for some new imaginary wonder drug. We have 134 subjects entering the trial. Some of them enter healthy (N=96), some of them enter the trial already being sick (N=38). All of them receive our treatment or intervention. Then we check back in a month to see if they are healthy or sick. A classic pre/post experimental design. We’re interested in seeing the change in both groupings. In the case of within-subjects designs, you can set paired = TRUE, which will display results from McNemar test in the subtitle. (Note: If you forget to set paired = TRUE, the results you get will be inaccurate.)

The results bode well for our experimental wonder drug. Of the 96 who started out healthy only 4% were sick after a month. Ideally, we would have hoped for zero but reality is seldom perfect. On the other side of the 38 who started out sick that number has reduced to just 13 or 34% which is a marked improvement.

Suggestions

If you find any bugs or have any suggestions/remarks, please file an issue on GitHub: https://github.com/IndrajeetPatil/ggstatsplot/issues

Session Information

Summarizing session information for reproducibility.

options(width = 200)
devtools::session_info()
#> - Session info ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.1 (2018-07-02)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  ctype    English_United States.1252  
#>  tz       America/New_York            
#>  date     2018-10-21                  
#> 
#> - Packages -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#>  package      * version     date       lib source                                      
#>  assertthat     0.2.0       2017-04-11 [1] CRAN (R 3.5.0)                              
#>  backports      1.1.2       2017-12-13 [1] CRAN (R 3.5.0)                              
#>  base64enc      0.1-3       2015-07-28 [1] CRAN (R 3.5.0)                              
#>  BayesFactor    0.9.12-4.2  2018-05-19 [1] CRAN (R 3.5.0)                              
#>  bayesplot      1.6.0       2018-08-02 [1] CRAN (R 3.5.1)                              
#>  bindr          0.1.1       2018-03-13 [1] CRAN (R 3.5.0)                              
#>  bindrcpp     * 0.2.2       2018-03-29 [1] CRAN (R 3.5.0)                              
#>  boot           1.3-20      2017-08-06 [2] CRAN (R 3.5.1)                              
#>  broom          0.5.0.9001  2018-10-03 [1] Github (tidymodels/broom@140eb58)           
#>  broom.mixed    0.2.2.9000  2018-10-09 [1] Github (bbolker/broom.mixed@fb9163a)        
#>  ca             0.71        2018-10-10 [1] CRAN (R 3.5.1)                              
#>  callr          3.0.0       2018-08-24 [1] CRAN (R 3.5.1)                              
#>  cli            1.0.1       2018-09-25 [1] CRAN (R 3.5.1)                              
#>  cluster        2.0.7-1     2018-04-13 [2] CRAN (R 3.5.1)                              
#>  coda           0.19-2      2018-10-08 [1] CRAN (R 3.5.1)                              
#>  codetools      0.2-15      2016-10-05 [2] CRAN (R 3.5.1)                              
#>  coin           1.2-2       2017-11-28 [1] CRAN (R 3.5.0)                              
#>  colorspace     1.3-2       2016-12-14 [1] CRAN (R 3.5.0)                              
#>  commonmark     1.6         2018-09-30 [1] CRAN (R 3.5.1)                              
#>  cowplot        0.9.99      2018-08-23 [1] Github (wilkelab/cowplot@374c3e9)           
#>  crayon         1.3.4       2018-09-26 [1] Github (r-lib/crayon@3e751fb)               
#>  data.table     1.11.8      2018-09-30 [1] CRAN (R 3.5.1)                              
#>  debugme        1.1.0       2017-10-22 [1] CRAN (R 3.5.0)                              
#>  DEoptimR       1.0-8       2016-11-19 [1] CRAN (R 3.5.0)                              
#>  desc           1.2.0       2018-05-01 [1] CRAN (R 3.5.0)                              
#>  devtools       2.0.0       2018-10-19 [1] CRAN (R 3.5.1)                              
#>  digest         0.6.18      2018-10-10 [1] CRAN (R 3.5.1)                              
#>  dplyr        * 0.7.7       2018-10-16 [1] CRAN (R 3.5.1)                              
#>  effsize        0.7.1       2017-03-21 [1] CRAN (R 3.5.0)                              
#>  emmeans        1.2.4       2018-09-22 [1] CRAN (R 3.5.1)                              
#>  estimability   1.3         2018-02-11 [1] CRAN (R 3.5.0)                              
#>  evaluate       0.12        2018-10-09 [1] CRAN (R 3.5.1)                              
#>  exact2x2       1.6.3       2018-07-27 [1] CRAN (R 3.5.1)                              
#>  exactci        1.3-3       2017-10-02 [1] CRAN (R 3.5.0)                              
#>  fansi          0.4.0       2018-10-05 [1] Github (brodieG/fansi@ab11e9c)              
#>  fit.models     0.5-14      2017-04-06 [1] CRAN (R 3.5.0)                              
#>  forcats        0.3.0       2018-02-19 [1] CRAN (R 3.5.0)                              
#>  foreign        0.8-70      2017-11-28 [2] CRAN (R 3.5.1)                              
#>  fs             1.2.6       2018-08-23 [1] CRAN (R 3.5.1)                              
#>  generics       0.0.1.9000  2018-10-03 [1] Github (r-lib/generics@24ba515)             
#>  ggcorrplot     0.1.2       2018-09-11 [1] CRAN (R 3.5.1)                              
#>  ggExtra        0.8         2018-08-14 [1] Github (daattali/ggExtra@76d1618)           
#>  ggplot2        3.0.0.9000  2018-09-26 [1] Github (tidyverse/ggplot2@e9f7ded)          
#>  ggrepel        0.8.0.9000  2018-09-09 [1] Github (slowkow/ggrepel@91877ca)            
#>  ggridges       0.5.1       2018-10-04 [1] Github (clauswilke/ggridges@1d5131f)        
#>  ggstatsplot  * 0.0.6.9000  2018-10-21 [1] local                                       
#>  glmmTMB        0.2.2.0     2018-07-03 [1] CRAN (R 3.5.1)                              
#>  glue           1.3.0       2018-09-17 [1] Github (tidyverse/glue@4e74901)             
#>  gnm            1.1-0       2018-06-21 [1] CRAN (R 3.5.0)                              
#>  groupedstats   0.0.3.9000  2018-10-12 [1] local                                       
#>  gtable         0.2.0       2016-02-26 [1] CRAN (R 3.5.0)                              
#>  gtools         3.8.1       2018-06-26 [1] CRAN (R 3.5.0)                              
#>  haven          1.1.2       2018-06-27 [1] CRAN (R 3.5.0)                              
#>  hms            0.4.2       2018-03-10 [1] CRAN (R 3.5.0)                              
#>  htmldeps       0.1.1       2018-09-17 [1] Github (rstudio/htmldeps@c1023e0)           
#>  htmltools      0.3.6       2017-04-28 [1] CRAN (R 3.5.0)                              
#>  httpuv         1.4.5       2018-07-19 [1] CRAN (R 3.5.1)                              
#>  jmv            0.9.4       2018-09-29 [1] Github (jamovi/jmv@7dac133)                 
#>  jmvcore        0.9.4       2018-09-17 [1] CRAN (R 3.5.1)                              
#>  knitr          1.20.19     2018-10-10 [1] Github (yihui/knitr@3abb642)                
#>  later          0.7.5       2018-09-18 [1] CRAN (R 3.5.1)                              
#>  lattice        0.20-35     2017-03-25 [2] CRAN (R 3.5.1)                              
#>  lazyeval       0.2.1       2017-10-29 [1] CRAN (R 3.5.0)                              
#>  lme4           1.1-18-1    2018-08-17 [1] CRAN (R 3.5.1)                              
#>  lmtest         0.9-36      2018-04-04 [1] CRAN (R 3.5.0)                              
#>  magrittr       1.5         2014-11-22 [1] CRAN (R 3.5.0)                              
#>  MASS           7.3-50      2018-04-30 [2] CRAN (R 3.5.1)                              
#>  Matrix         1.2-14      2018-04-13 [2] CRAN (R 3.5.1)                              
#>  MatrixModels   0.4-1       2015-08-22 [1] CRAN (R 3.5.0)                              
#>  mc2d           0.1-18      2017-03-06 [1] CRAN (R 3.5.0)                              
#>  memoise        1.1.0       2017-04-21 [1] CRAN (R 3.5.0)                              
#>  mime           0.6         2018-10-05 [1] CRAN (R 3.5.1)                              
#>  miniUI         0.1.1.1     2018-05-18 [1] CRAN (R 3.5.0)                              
#>  minqa          1.2.4       2014-10-09 [1] CRAN (R 3.5.0)                              
#>  mnormt         1.5-5       2016-10-15 [1] CRAN (R 3.5.0)                              
#>  modelr         0.1.2       2018-05-11 [1] CRAN (R 3.5.0)                              
#>  modeltools     0.2-22      2018-07-16 [1] CRAN (R 3.5.1)                              
#>  multcomp       1.4-8       2017-11-08 [1] CRAN (R 3.5.0)                              
#>  munsell        0.5.0       2018-06-12 [1] CRAN (R 3.5.0)                              
#>  mvtnorm        1.0-8       2018-05-31 [1] CRAN (R 3.5.0)                              
#>  nlme           3.1-137     2018-04-07 [2] CRAN (R 3.5.1)                              
#>  nloptr         1.2.1       2018-10-03 [1] CRAN (R 3.5.1)                              
#>  nnet           7.3-12      2016-02-02 [1] CRAN (R 3.5.0)                              
#>  paletteer      0.1.0       2018-07-10 [1] CRAN (R 3.5.1)                              
#>  pbapply        1.3-4       2018-01-10 [1] CRAN (R 3.5.0)                              
#>  pcaPP          1.9-73      2018-01-14 [1] CRAN (R 3.5.0)                              
#>  pillar         1.3.0.9000  2018-10-01 [1] Github (r-lib/pillar@7be5b8a)               
#>  pkgbuild       1.0.2       2018-10-16 [1] CRAN (R 3.5.1)                              
#>  pkgconfig      2.0.2       2018-08-16 [1] CRAN (R 3.5.1)                              
#>  pkgdown        1.1.0.9000  2018-10-21 [1] Github (metrumresearchgroup/pkgdown@3836984)
#>  pkgload        1.0.1       2018-10-11 [1] CRAN (R 3.5.1)                              
#>  plyr           1.8.4       2016-06-08 [1] CRAN (R 3.5.0)                              
#>  prediction     0.3.6       2018-05-22 [1] CRAN (R 3.5.0)                              
#>  prettyunits    1.0.2       2015-07-13 [1] CRAN (R 3.5.0)                              
#>  processx       3.2.0       2018-08-16 [1] CRAN (R 3.5.1)                              
#>  promises       1.0.1       2018-04-13 [1] CRAN (R 3.5.0)                              
#>  ps             1.2.0       2018-10-16 [1] CRAN (R 3.5.1)                              
#>  psych          1.8.9       2018-10-08 [1] local                                       
#>  purrr          0.2.5       2018-05-29 [1] CRAN (R 3.5.0)                              
#>  purrrlyr       0.0.3       2018-05-29 [1] CRAN (R 3.5.0)                              
#>  pwr            1.2-2       2018-03-03 [1] CRAN (R 3.5.0)                              
#>  qvcalc         0.9-1       2017-09-19 [1] CRAN (R 3.5.0)                              
#>  R6             2.3.0       2018-10-04 [1] CRAN (R 3.5.1)                              
#>  Rcpp           0.12.19     2018-10-01 [1] CRAN (R 3.5.1)                              
#>  relimp         1.0-5       2016-03-30 [1] CRAN (R 3.5.0)                              
#>  remotes        2.0.1       2018-10-19 [1] CRAN (R 3.5.1)                              
#>  reshape        0.8.7       2017-08-06 [1] CRAN (R 3.5.0)                              
#>  reshape2       1.4.3       2017-12-11 [1] CRAN (R 3.5.0)                              
#>  rjson          0.2.20      2018-06-08 [1] CRAN (R 3.5.0)                              
#>  rlang          0.2.99.0000 2018-10-10 [1] Github (r-lib/rlang@fb09ff3)                
#>  rmarkdown      1.10.14     2018-10-10 [1] Github (rstudio/rmarkdown@dcc7d37)          
#>  robust         0.4-18      2017-04-27 [1] CRAN (R 3.5.0)                              
#>  robustbase     0.93-3      2018-09-21 [1] CRAN (R 3.5.1)                              
#>  roxygen2       6.1.0.9000  2018-09-13 [1] Github (klutometis/roxygen@cc34200)         
#>  rprojroot      1.3-2       2018-01-03 [1] CRAN (R 3.5.0)                              
#>  rrcov          1.4-4       2018-05-24 [1] CRAN (R 3.5.0)                              
#>  rstudioapi     0.8         2018-10-02 [1] CRAN (R 3.5.1)                              
#>  sandwich       2.5-0       2018-08-17 [1] CRAN (R 3.5.1)                              
#>  scales         1.0.0       2018-08-09 [1] CRAN (R 3.5.1)                              
#>  sessioninfo    1.1.0       2018-09-25 [1] CRAN (R 3.5.1)                              
#>  shiny          1.1.0       2018-05-17 [1] CRAN (R 3.5.0)                              
#>  sjlabelled     1.0.14      2018-09-12 [1] CRAN (R 3.5.1)                              
#>  sjmisc         2.7.5       2018-09-13 [1] CRAN (R 3.5.1)                              
#>  sjstats        0.17.1      2018-10-04 [1] Github (strengejacke/sjstats@9eafc5e)       
#>  skimr          1.0.3       2018-07-06 [1] Github (ropenscilabs/skimr@c67559a)         
#>  snakecase      0.9.2       2018-08-14 [1] CRAN (R 3.5.1)                              
#>  ssanv          1.1         2015-06-23 [1] CRAN (R 3.5.0)                              
#>  stringdist     0.9.5.1     2018-06-08 [1] CRAN (R 3.5.0)                              
#>  stringi        1.2.4       2018-07-20 [1] CRAN (R 3.5.1)                              
#>  stringr        1.3.1       2018-05-10 [1] CRAN (R 3.5.0)                              
#>  survival       2.42-3      2018-04-16 [2] CRAN (R 3.5.1)                              
#>  testthat       2.0.1       2018-10-13 [1] CRAN (R 3.5.1)                              
#>  TH.data        1.0-9       2018-07-10 [1] CRAN (R 3.5.1)                              
#>  tibble         1.4.2       2018-01-22 [1] CRAN (R 3.5.1)                              
#>  tidyr          0.8.1.9000  2018-10-06 [1] Github (tidyverse/tidyr@3b62137)            
#>  tidyselect     0.2.5       2018-10-11 [1] CRAN (R 3.5.1)                              
#>  TMB            1.7.14      2018-06-23 [1] CRAN (R 3.5.0)                              
#>  usethis        1.4.0.9000  2018-09-23 [1] Github (r-lib/usethis@1e3c6a6)              
#>  utf8           1.1.4       2018-05-24 [1] CRAN (R 3.5.0)                              
#>  vcd            1.4-4       2017-12-06 [1] CRAN (R 3.5.0)                              
#>  vcdExtra       0.7-1       2017-09-29 [1] CRAN (R 3.5.0)                              
#>  withr          2.1.2       2018-03-15 [1] CRAN (R 3.5.0)                              
#>  WRS2           0.10-0      2018-06-15 [1] CRAN (R 3.5.0)                              
#>  xfun           0.3         2018-07-06 [1] CRAN (R 3.5.1)                              
#>  xml2           1.2.0       2018-01-24 [1] CRAN (R 3.5.0)                              
#>  xtable         1.8-3       2018-08-29 [1] CRAN (R 3.5.1)                              
#>  yaml           2.2.0       2018-07-25 [1] CRAN (R 3.5.1)                              
#>  zoo            1.8-4       2018-09-19 [1] CRAN (R 3.5.1)                              
#> 
#> [1] C:/Users/inp099/Documents/R/win-library/3.5
#> [2] C:/Program Files/R/R-3.5.1/library