### Install ggstatsplot (Development) Source: https://indrajeetpatil.github.io/ggstatsplot/index.html Use this command to install the development version of the ggstatsplot package. ```R pak::pak("IndrajeetPatil/ggstatsplot") ``` -------------------------------- ### Preparing Data for ggdotplotstats Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/purrr_examples.html Initial setup for ggdotplotstats by loading libraries and splitting the gapminder dataset. ```R library(ggthemes) library(hrbrthemes) ## let's split the data frame and create a list by continent ## let's leave out Oceania because it has just two data points continent_list <- gapminder::gapminder %>% dplyr::filter(continent != "Oceania") %>% split(f = .$continent, drop = TRUE) ``` -------------------------------- ### Inspecting Titanic_full dataset Source: https://indrajeetpatil.github.io/ggstatsplot/reference/Titanic_full.html Examples for checking dimensions, viewing the first few rows, and inspecting the structure of the dataset. ```R dim(Titanic_full) #> [1] 2201 5 head(Titanic_full) #> # A tibble: 6 × 5 #> id Class Sex Age Survived #> #> 1 1 3rd Male Child No #> 2 2 3rd Male Child No #> 3 3 3rd Male Child No #> 4 4 3rd Male Child No #> 5 5 3rd Male Child No #> 6 6 3rd Male Child No dplyr::glimpse(Titanic_full) #> Rows: 2,201 #> Columns: 5 #> $ id 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18… #> $ Class 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3… #> $ Sex Male, Male, Male, Male, Male, Male, Male, Male, Male, Male, M… #> $ Age Child, Child, Child, Child, Child, Child, Child, Child, Child… #> $ Survived No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, N… ``` -------------------------------- ### Install Ubuntu Dependencies for PMCMRplus Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/faq.html Install `gmp` and `Rmpfr` libraries on Ubuntu to resolve installation issues with the PMCMRplus package, a dependency of ggstatsplot. ```bash sudo apt-get install libgmp3-dev ``` ```bash sudo apt-get install libmpfr-dev ``` -------------------------------- ### Inspecting the bugs_long dataset Source: https://indrajeetpatil.github.io/ggstatsplot/reference/bugs_long.html Demonstrates how to check the dimensions, view the first few rows, and get a summary of the data structure. ```R dim(bugs_long) #> [1] 372 6 head(bugs_long) #> # A tibble: 6 × 6 #> subject gender region education condition desire #> #> 1 1 Female North America some LDLF 6 #> 2 2 Female North America advance LDLF 10 #> 3 3 Female Europe college LDLF 5 #> 4 4 Female North America college LDLF 6 #> 5 5 Female North America some LDLF 3 #> 6 6 Female Europe some LDLF 2 dplyr::glimpse(bugs_long) #> Rows: 372 #> Columns: 6 #> $ subject 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1… #> $ gender Female, Female, Female, Female, Female, Female, Female, Fema… #> $ region North America, North America, Europe, North America, North A… #> $ education some, advance, college, college, some, some, some, high, hig… #> $ condition "LDLF", "LDLF", "LDLF", "LDLF", "LDLF", "LDLF", "LDLF", "LDL… #> $ desire 6.0, 10.0, 5.0, 6.0, 3.0, 2.0, 10.0, 10.0, 9.5, 8.5, 0.0, 9.… ``` -------------------------------- ### Basic gghistostats for Reporting Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/gghistostats.html A simple example of `gghistostats` to demonstrate how statistical results can be integrated into reports. This function visualizes data and provides statistical summaries. ```R gghistostats(trees, Height, test.value = 75) ``` -------------------------------- ### Install ggstatsplot (Release) Source: https://indrajeetpatil.github.io/ggstatsplot/index.html Use this command to install the stable release version of the ggstatsplot package from CRAN. ```R install.packages("ggstatsplot") ``` -------------------------------- ### Generate and analyze pie charts with ggpiestats Source: https://indrajeetpatil.github.io/ggstatsplot/reference/ggpiestats.html Demonstrates creating a one-sample goodness of fit proportion test plot and extracting statistical details using extract_stats, followed by an association test example. ```R # for reproducibility set.seed(123) # one sample goodness of fit proportion test p <- ggpiestats(mtcars, vs) # looking at the plot p # extracting details from statistical tests extract_stats(p) #> $subtitle_data #> # A tibble: 1 × 13 #> statistic df p.value method effectsize #> #> 1 0.5 1 0.480 Chi-squared test for given probabilities Pearson's C #> estimate conf.level conf.low conf.high conf.method conf.distribution n.obs #> #> 1 0.124 0.95 0 0.426 ncp chisq 32 #> expression #> #> 1 #> #> $caption_data #> # A tibble: 1 × 4 #> bf10 prior.scale method expression #> #> 1 0.180 1 Bayesian one-way contingency table analysis #> #> $pairwise_comparisons_data #> NULL #> #> $descriptive_data #> # A tibble: 2 × 4 #> vs counts perc .label #> #> 1 1 14 43.8 44% #> 2 0 18 56.2 56% #> #> $one_sample_data #> NULL #> #> $tidy_data #> NULL #> #> $glance_data #> NULL #> #> attr(,"class") #> [1] "ggstatsplot_stats" "list" # association test (or contingency table analysis) ggpiestats(mtcars, vs, cyl) ``` -------------------------------- ### Create Pie Chart with Statistical Summary Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/purrr_examples.html Utilize ggpiestats to generate pie charts with statistical analysis. This example splits data by passenger class and applies the function to each subset, customizing labels, palettes, and themes. ```R class_list <- Titanic_full %>% split(f = .$Class, drop = TRUE) ``` ```R length(class_list) names(class_list) ``` ```R plot_list <- purrr::pmap( .l = list( data = class_list, x = "Survived", y = "Sex", label = list("both", "count", "percentage", "both"), title = list( "Passenger class: 1st", "Passenger class: 2nd", "Passenger class: 3rd", "Passenger class: Crew" ), caption = list( "Total: 319, Died: 120, Survived: 199, % Survived: 62%", "Total: 272, Died: 155, Survived: 117, % Survived: 43%", "Total: 709, Died: 537, Survived: 172, % Survived: 25%", "Data not available for crew passengers" ), package = list("RColorBrewer", "ghibli", "palettetown", "yarrr"), palette = list("Accent", "MarnieMedium1", "pikachu", "nemo"), ggtheme = list( ggplot2::theme_grey(), ggplot2::theme_bw(), ggthemes::theme_tufte(), ggthemes::theme_economist() ), proportion.test = list(TRUE, FALSE, TRUE, FALSE), type = list("p", "p", "bf", "p") ), .f = ggpiestats ) ``` ```R combine_plots( plotlist = plot_list, annotation.args = list(title = "Survival in Titanic disaster by gender for all passenger classes"), plotgrid.args = list(ncol = 1), guides = "keep" ) ``` -------------------------------- ### Create a ggdotplotstats plot Source: https://indrajeetpatil.github.io/ggstatsplot/reference/ggdotplotstats.html Example of creating a dot plot with statistical details using ggdotplotstats. Requires ggplot2 and ggstatsplot packages. ```r # for reproducibility set.seed(123) # creating a plot p <- ggdotplotstats( data = ggplot2::mpg, x = cty, y = manufacturer, title = "Fuel economy data", xlab = "city miles per gallon" ) # looking at the plot p ``` -------------------------------- ### Visualize Cox proportional hazards model with ggcoefstats Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggcoefstats.html Visualize coefficients from a Cox proportional hazards regression model using ggcoefstats. Requires the survival package. This example demonstrates fitting a stratified model. ```R library(survival) # create the simplest-test data set test1 <- list( time = c(4, 3, 1, 1, 2, 2, 3), status = c(1, 1, 1, 0, 1, 1, 0), x = c(0, 2, 1, 1, 1, 0, 0), sex = c(0, 0, 0, 0, 1, 1, 1) ) # fit a stratified model mod_coxph <- survival::coxph( formula = Surv(time, status) ~ x + strata(sex), data = test1 ) ggcoefstats( x = mod_coxph, title = "Cox proportional hazards regression model" ) ``` -------------------------------- ### Visualize Custom Data Frame with ggcoefstats Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggcoefstats.html This example shows how to visualize a custom data frame using ggcoefstats. The data frame must include 'term' and 'estimate' columns, and optionally 'conf.low', 'conf.high', and 'p.value'. Specify the statistic type if needed. ```R # let's create a data frame df_full <- tibble::tribble( ~term, ~statistic, ~estimate, ~std.error, ~p.value, ~df.error, "study1", 0.158, 0.0665, 0.778, 0.875, 5L, "study2", 1.33, 0.542, 0.280, 0.191, 10L, "study3", 1.24, 0.045, 0.030, 0.001, 12L, "study4", 0.156, 0.500, 0.708, 0.885, 8L, "study5", 0.33, 0.032, 0.280, 0.101, 2L, "study6", 1.04, 0.085, 0.030, 0.001, 3L ) ggcoefstats( x = df_full, meta.analytic.effect = TRUE, statistic = "t", package = "LaCroixColoR", palette = "paired" ) ``` -------------------------------- ### Customized Correlation Matrix Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggcorrmat.html Creates a detailed correlation matrix with custom variable names, correlation types, labels, themes, and color palettes. This example showcases extensive customization options for visual appeal and information display. ```R ggcorrmat( data = gapminder_2007, ## data from which variable is to be taken cor.vars = lifeExp:gdpPercap, ## specifying correlation matrix variables cor.vars.names = c( "Life Expectancy", "population", "GDP (per capita)" ), type = "np", ## which correlation coefficient is to be computed lab.col = "red", ## label color ggtheme = ggplot2::theme_light(), ## selected ggplot2 theme ## turn off default ggestatsplot theme overlay matrix.type = "lower", ## correlation matrix structure colors = NULL, ## turning off manual specification of colors palette = "category10_d3", ## choosing a color palette package = "ggsci", ## package to which color palette belongs title = "Gapminder correlation matrix", ## custom title subtitle = "Source: Gapminder Foundation" ## custom subtitle ) ``` -------------------------------- ### Extracting Statistical Analysis Summaries with ggstatsplot Source: https://indrajeetpatil.github.io/ggstatsplot/index.html This example shows the output of the `extract_stats` function, which returns a list of tibbles containing detailed statistical summaries for various analyses, such as ANOVA, pairwise comparisons, and Bayesian factors. These summaries are generated by the `statsExpressions` package. ```R extract_stats(p) #> $subtitle_data #> # A tibble: 1 × 14 #> statistic df df.error p.value #> #> 1 31.6 2 18.0 0.00000127 #> method effectsize estimate #> #> 1 One-way analysis of means (not assuming equal variances) Omega2 0.744 #> conf.level conf.low conf.high conf.method conf.distribution n.obs expression #> #> 1 0.95 0.531 1 ncp F 32 #> #> $caption_data #> # A tibble: 6 × 17 #> term pd prior.distribution prior.location prior.scale bf10 #> #> 1 mu 1 cauchy 0 0.707 3008850. #> 2 cyl-4 1 cauchy 0 0.707 3008850. #> 3 cyl-6 0.780 cauchy 0 0.707 3008850. #> 4 cyl-8 1 cauchy 0 0.707 3008850. #> 5 sig2 1 cauchy 0 0.707 3008850. #> 6 g_cyl 1 cauchy 0 0.707 3008850. #> method log_e_bf10 effectsize estimate std.dev #> #> 1 Bayes factors for linear models 14.9 Bayesian R-squared 0.714 0.0503 #> 2 Bayes factors for linear models 14.9 Bayesian R-squared 0.714 0.0503 #> 3 Bayes factors for linear models 14.9 Bayesian R-squared 0.714 0.0503 #> 4 Bayes factors for linear models 14.9 Bayesian R-squared 0.714 0.0503 #> 5 Bayes factors for linear models 14.9 Bayesian R-squared 0.714 0.0503 #> 6 Bayes factors for linear models 14.9 Bayesian R-squared 0.714 0.0503 #> conf.level conf.low conf.high conf.method n.obs expression #> #> 1 0.95 0.574 0.788 HDI 32 #> 2 0.95 0.574 0.788 HDI 32 #> 3 0.95 0.574 0.788 HDI 32 #> 4 0.95 0.574 0.788 HDI 32 #> 5 0.95 0.574 0.788 HDI 32 #> 6 0.95 0.574 0.788 HDI 32 #> #> $pairwise_comparisons_data #> # A tibble: 3 × 9 #> group1 group2 statistic p.value alternative distribution p.adjust.method #> #> 1 4 6 -6.67 0.00110 two.sided q Holm #> 2 4 8 -10.7 0.0000140 two.sided q Holm #> 3 6 8 -7.48 0.000257 two.sided q Holm #> test expression #> #> 1 Games-Howell #> 2 Games-Howell #> 3 Games-Howell #> #> $descriptive_data #> NULL #> #> $one_sample_data #> NULL #> #> $tidy_data #> NULL #> #> $glance_data #> NULL #> #> attr(,"class") #> [1] "ggstatsplot_stats" "list" ``` -------------------------------- ### Get dimensions of iris_long Source: https://indrajeetpatil.github.io/ggstatsplot/reference/iris_long.html Returns the dimensions (number of rows and columns) of the iris_long dataset. ```R dim(iris_long) #> [1] 600 6 ``` -------------------------------- ### Load ggplot2 library Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/principles.html Initializes the ggplot2 library for plotting. ```R library(ggplot2) ``` -------------------------------- ### Get Citation for ggstatsplot Source: https://indrajeetpatil.github.io/ggstatsplot/index.html Run this R code to obtain the citation information for the ggstatsplot package, useful for academic publications. ```R citation("ggstatsplot") ``` -------------------------------- ### Load Libraries and Inspect Data Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/gghistostats.html Loads necessary libraries (psych, dplyr) and inspects the structure of the sat.act dataset using glimpse. ```R library(psych) library(dplyr) ## looking at the structure of the data using glimpse dplyr::glimpse(psych::sat.act) #> Rows: 700 #> Columns: 6 #> $ gender 2, 2, 2, 1, 1, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 2, 2, 2, 1, 2, … #> $ education 3, 3, 3, 4, 2, 5, 5, 3, 4, 5, 3, 4, 4, 4, 3, 4, 3, 4, 4, 4, … #> $ age 19, 23, 20, 27, 33, 26, 30, 19, 23, 40, 23, 34, 32, 41, 20, … #> $ ACT 24, 35, 21, 26, 31, 28, 36, 22, 22, 35, 32, 29, 21, 35, 27, … #> $ SATV 500, 600, 480, 550, 600, 640, 610, 520, 400, 730, 760, 710, … #> $ SATQ 500, 500, 470, 520, 550, 640, 500, 560, 600, 800, 710, 600, … ``` -------------------------------- ### Basic ggbarstats Plot Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggbarstats.html A simple example of `ggbarstats` used with the `mtcars` dataset to visualize the relationship between transmission type and number of cylinders. ```r ggbarstats(mtcars, am, cyl) ``` -------------------------------- ### Visualize Bayesian Models Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggcoefstats.html Shows how to visualize various Bayesian models from the BayesFactor package using ggcoefstats. ```R library(BayesFactor) # one sample t-test mod1 <- ttestBF(mtcars$wt, mu = 3) # independent t-test mod2 <- ttestBF(formula = wt ~ am, data = mtcars) # paired t-test mod3 <- ttestBF(x = sleep$extra[1:10], y = sleep$extra[11:20], paired = TRUE) # correlation mod4 <- correlationBF(y = iris$Sepal.Length, x = iris$Sepal.Width) # contingency tabs (not supported) data("raceDolls") mod5 <- contingencyTableBF( raceDolls, sampleType = "indepMulti", fixedMargin = "cols" ) # anova data("puzzles") mod6 <- anovaBF( formula = RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", whichModels = "top", progress = FALSE ) # regression-1 mod7 <- regressionBF(rating ~ ., data = attitude, progress = FALSE) # meta-analysis t <- c(-0.15, 2.39, 2.42, 2.43, -0.15, 2.39, 2.42, 2.43) N <- c(100, 150, 97, 99, 99, 97, 100, 150) mod8 <- meta.ttestBF(t, N, rscale = 1, nullInterval = c(0, Inf)) # proportion test mod9 <- proportionBF(y = 15, N = 25, p = 0.5) # list of plots combine_plots( plotlist = list( ggcoefstats(mod1, title = "one sample t-test"), ggcoefstats(mod2, title = "independent t-test"), ggcoefstats(mod3, title = "paired t-test"), ggcoefstats(mod4, title = "correlation"), ggcoefstats(mod5, title = "contingency table", effectsize.type = "cramers_v"), ggcoefstats(mod6, title = "anova"), ggcoefstats(mod7, title = "regression-1"), ggcoefstats(mod8, title = "meta-analysis"), ggcoefstats(mod9, title = "proportion test") ), annotation.args = list(title = "Example from `{BayesFactor}` package") ) ``` -------------------------------- ### Generate ggbetweenstats plot Source: https://indrajeetpatil.github.io/ggstatsplot/reference/ggbetweenstats.html Generates a plot using the ggbetweenstats function with specified data, grouping, and outcome variables. Includes reproducibility setup. ```R # for reproducibility set.seed(123) p <- ggbetweenstats(mtcars, am, mpg) p ``` -------------------------------- ### Comparative statistical tests with ggwithinstats Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggwithinstats.html Shows how to generate multiple plots using different statistical test types (parametric, nonparametric, robust, and Bayesian) for a subset of data. ```R ## selecting subset of the data df_disgust <- dplyr::filter(bugs_long, condition %in% c("LDHF", "HDHF")) ## parametric t-test p1 <- ggwithinstats( data = df_disgust, x = condition, y = desire, type = "p", effsize.type = "d", conf.level = 0.99, title = "Parametric test", package = "ggsci", palette = "nrc_npg" ) ## Mann-Whitney U test (nonparametric test) p2 <- ggwithinstats( data = df_disgust, x = condition, y = desire, xlab = "Condition", ylab = "Desire to kill bugs", type = "np", conf.level = 0.99, title = "Non-parametric Test", package = "ggsci", palette = "uniform_startrek" ) ## robust t-test p3 <- ggwithinstats( data = df_disgust, x = condition, y = desire, xlab = "Condition", ylab = "Desire to kill bugs", type = "r", conf.level = 0.99, title = "Robust Test", package = "wesanderson", palette = "Royal2" ) ## Bayes Factor for parametric t-test p4 <- ggwithinstats( data = df_disgust, x = condition, y = desire, xlab = "Condition", ylab = "Desire to kill bugs", type = "bayes", title = "Bayesian Test", package = "ggsci", palette = "nrc_npg" ) ``` -------------------------------- ### Inspect Titanic Dataset Structure Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggpiestats.html Use dplyr::glimpse to inspect the structure of the original Titanic dataset and the modified Titanic_full dataset used in examples. ```r # looking at the original data in tabular format dplyr::glimpse(Titanic) #> 'table' num [1:4, 1:2, 1:2, 1:2] 0 0 35 0 0 0 17 0 118 154 ... #> - attr(*, "dimnames")=List of 4 #> ..$ Class : chr [1:4] "1st" "2nd" "3rd" "Crew" #> ..$ Sex : chr [1:2] "Male" "Female" #> ..$ Age : chr [1:2] "Child" "Adult" #> ..$ Survived: chr [1:2] "No" "Yes" ``` ```r # looking at the dataset as a tibble or data frame dplyr::glimpse(Titanic_full) #> Rows: 2,201 #> Columns: 5 #> $ id 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18… #> $ Class 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3rd, 3… #> $ Sex Male, Male, Male, Male, Male, Male, Male, Male, Male, Male, M… #> $ Age Child, Child, Child, Child, Child, Child, Child, Child, Child… #> $ Survived No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, N… ``` -------------------------------- ### Create statistical plots with ggbetweenstats Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggbetweenstats.html Generates various statistical test visualizations including parametric, non-parametric, robust, and Bayesian tests. ```R df_year <- dplyr::filter(gapminder::gapminder, year == 2007 | year == 1957) p1 <- ggbetweenstats( data = df_year, x = year, y = lifeExp, xlab = "Year", ylab = "Life expectancy", # to remove violin plot violin.args = list(width = 0), type = "p", conf.level = 0.99, title = "Parametric test", package = "ggsci", palette = "nrc_npg" ) p2 <- ggbetweenstats( data = df_year, x = year, y = lifeExp, xlab = "Year", ylab = "Life expectancy", # to remove box plot boxplot.args = list(width = 0), type = "np", conf.level = 0.99, title = "Non-parametric Test", package = "ggsci", palette = "uniform_startrek" ) p3 <- ggbetweenstats( data = df_year, x = year, y = lifeExp, xlab = "Year", ylab = "Life expectancy", type = "r", conf.level = 0.99, title = "Robust Test", tr = 0.005, package = "wesanderson", palette = "Royal2", digits = 3 ) ## Bayes Factor for parametric t-test and boxviolin plot p4 <- ggbetweenstats( data = df_year, x = year, y = lifeExp, xlab = "Year", ylab = "Life expectancy", type = "bayes", violin.args = list(width = 0), boxplot.args = list(width = 0), point.args = list(alpha = 0), title = "Bayesian Test", package = "ggsci", palette = "nrc_npg" ) ``` -------------------------------- ### ggbetweenstats Configuration Arguments Source: https://indrajeetpatil.github.io/ggstatsplot/reference/grouped_ggbetweenstats.html Detailed breakdown of arguments used to configure statistical plots in ggbetweenstats. ```APIDOC ## Arguments for ggbetweenstats ### Description Configuration arguments for customizing the behavior and appearance of statistical plots generated by ggstatsplot. ### Parameters - **data** (data frame) - Required - A data frame or tibble containing the variables. Must be ungrouped. - **xlab** (string) - Optional - Label for x-axis variable. - **ylab** (string) - Optional - Label for y-axis variable. - **p.adjust.method** (string) - Optional - Method for p-value adjustment (e.g., "holm", "bonferroni", "fdr"). - **pairwise.display** (string) - Optional - Which pairwise comparisons to display ("significant", "non-significant", "all", "none"). - **bf.message** (logical) - Optional - Whether to display Bayes Factor for parametric tests. - **results.subtitle** (logical) - Optional - Whether to display statistical test results as a subtitle. - **centrality.plotting** (logical) - Optional - Whether to display central tendency measure. - **centrality.type** (string) - Optional - Type of centrality parameter ("parametric", "nonparametric", "robust", "bayes"). - **point.args** (list) - Optional - Aesthetic arguments for ggplot2::geom_point(). - **boxplot.args** (list) - Optional - Aesthetic arguments for ggplot2::geom_boxplot(). - **violin.args** (list) - Optional - Aesthetic arguments for ggplot2::geom_violin(). - **ggtheme** (theme) - Optional - A ggplot2 theme to apply to the plot. ``` -------------------------------- ### Create a statistical visualization with ggbetweenstats Source: https://indrajeetpatil.github.io/ggstatsplot/articles/ggstatsplot.html Generates a plot comparing groups with embedded descriptive and inferential statistical details. ```R ggbetweenstats(iris, Species, Sepal.Length) ``` -------------------------------- ### Turn off Scientific Notation in ggwithinstats Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/faq.html Control the display of numerical precision in `ggwithinstats` by setting the `digits` argument. This example shows how to display 4 decimal places. ```r set.seed(123) library(ggstatsplot) library(WRS2) ggwithinstats( WineTasting, Wine, Taste, paired = TRUE ) ``` ```r ggwithinstats( WineTasting, Wine, Taste, paired = TRUE, digits = 4L ) ``` -------------------------------- ### Create Within-Subjects Design Plot Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggbarstats.html Use `ggbarstats` with `paired = TRUE` for within-subjects designs. Ensure `paired` is set to TRUE to get accurate results from the McNemar test. ```r clinical_trial <- tibble::tribble( ~SickBefore, ~SickAfter, ~Counts, "No", "Yes", 4, "Yes", "No", 25, "Yes", "Yes", 13, "No", "No", 92 ) ggbarstats( data = clinical_trial, x = SickAfter, y = SickBefore, counts = Counts, paired = TRUE, label = "both", title = "Results from imaginary clinical trial", package = "ggsci", palette = "default_ucscgb" ) ``` -------------------------------- ### Customizing Plots with ggplot.component and annotation.args Source: https://indrajeetpatil.github.io/ggstatsplot/reference/grouped_ggbetweenstats.html Shows how to modify individual plots using the `ggplot.component` argument for y-axis scaling and `annotation.args` for plot titles. This example uses a filtered movies_long dataset. ```R # modifying individual plots using `ggplot.component` argument grouped_ggbetweenstats( data = filter( movies_long, genre %in% c("Action", "Comedy"), mpaa %in% c("R", "PG") ), x = genre, y = rating, grouping.var = mpaa, ggplot.component = scale_y_continuous( breaks = seq(1, 9, 1), limits = (c(1, 9)) ), annotation.args = list(title = "Ratings by genre for different MPAA ratings") ) #> Scale for y is already present. #> Adding another scale for y, which will replace the existing scale. #> Scale for y is already present. #> Adding another scale for y, which will replace the existing scale. ``` -------------------------------- ### Visualize Regression Models with List Outputs Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggcoefstats.html Demonstrates extracting specific components from a list-based model object (like gamm4) to visualize coefficients. ```R library(gamm4) # data dat <- gamSim(1, n = 400, scale = 2) # now add 20 level random effect `fac'... dat$fac <- fac <- as.factor(sample(1:20, 400, replace = TRUE)) dat$y <- dat$y + model.matrix(~ fac - 1) %*% rnorm(20) * .5 # model object br <- gamm4::gamm4( formula = y ~ s(x0) + x1 + s(x2), data = dat, random = ~ (1 | fac) ) # looking at the classes of the objects contained in the list purrr::map(br, class) combine_plots( plotlist = list( # first object plot (only parametric terms are shown) ggcoefstats( x = br$gam, title = "generalized additive model (parametric terms)", digits = 3 ), # second object plot ggcoefstats( x = br$mer, title = "linear mixed-effects model", digits = 3 ) ), plotgrid.args = list(nrow = 1) ) ``` -------------------------------- ### Describing Distribution with datawizard Source: https://indrajeetpatil.github.io/ggstatsplot/reference/ggbetweenstats.html Use datawizard::describe_distribution() for parametric, non-parametric, robust, and Bayesian distribution descriptions. ```R datawizard::describe_distribution() ``` -------------------------------- ### Create a correlation matrix with ggcorrmat Source: https://indrajeetpatil.github.io/ggstatsplot/index.html Produces a publication-ready correlation matrix with customizable aesthetics. ```R 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" ) ``` -------------------------------- ### Advanced grouped_ggbetweenstats usage Source: https://indrajeetpatil.github.io/ggstatsplot/index.html Employ grouped_ggbetweenstats to repeat operations across a single grouping variable. This example demonstrates customization of significance arguments, p-value adjustment, palette, package, plot grid, and annotation. ```R set.seed(123) grouped_ggbetweenstats( data = dplyr::filter(movies_long, genre %in% c("Action", "Comedy")), x = mpaa, y = length, grouping.var = genre, 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") ) ``` -------------------------------- ### Combine Plots for Comparison in R Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/principles.html This R code snippet combines two plots (superposition and juxtaposition) for comparison. It filters the data first and then uses `combine_plots` to arrange them. Ensure `ggplot2` and `ggstatsplot` are installed and loaded. ```r df <- dplyr::filter(movies_long, genre %in% c("Comedy", "Drama")) combine_plots( plotlist = list( # superposition ggplot(data = df, mapping = aes(x = length, y = rating, color = genre)) + geom_jitter(size = 3, alpha = 0.5) + geom_smooth(method = "lm") + labs(title = "superposition (recommended in Cleveland's paradigm)") + theme_ggstatsplot(), # juxtaposition grouped_ggscatterstats( data = df, x = length, y = rating, grouping.var = genre, marginal = FALSE, annotation.args = list(title = "juxtaposition (`{ggstatsplot}` implementation in `grouped_` functions)") ) ), ## combine for comparison annotation.args = list(title = "Two ways to compare different aspects of data"), plotgrid.args = list(nrow = 2L) ) ``` -------------------------------- ### Creating and Combining ggcorrmat Plots Source: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/purrr_examples.html Demonstrates splitting a dataframe and applying ggcorrmat across subsets, followed by combining the results. ```R ## splitting the data frame by cut and creating a list ## let's leave out "fair" cut ## also, to make this fast, let's only use 5% of the sample cut_list <- ggplot2::diamonds %>% dplyr::sample_frac(size = 0.05) %>% dplyr::filter(cut != "Fair") %>% split(f = .$cut, drop = TRUE) ## checking the length and names of each element length(cut_list) names(cut_list) ## running function on every element of this list note that if you want the same ## value for a given argument across all elements of the list, you need to ## specify it just once plot_list <- purrr::pmap( .l = list( data = cut_list, cor.vars = list(c("carat", "depth", "table", "price")), type = list("pearson", "np", "robust", "bf"), partial = list(TRUE, FALSE, TRUE, FALSE), title = list("Cut: Good", "Cut: Very Good", "Cut: Premium", "Cut: Ideal"), p.adjust.method = list("hommel", "fdr", "BY", "hochberg"), lab.size = 3.5, colors = list( c("#56B4E9", "white", "#999999"), c("#CC79A7", "white", "#F0E442"), c("#56B4E9", "white", "#D55E00"), c("#999999", "white", "#0072B2") ), ggtheme = list( ggplot2::theme_linedraw(), ggplot2::theme_classic(), ggthemes::theme_fivethirtyeight(), ggthemes::theme_tufte() ) ), .f = ggcorrmat ) ## combining all individual plots from the list into a single plot using ## `combine_plots` function combine_plots( plotlist = plot_list, guides = "keep", annotation.args = list( title = "Relationship between diamond attributes and price across cut", caption = "Dataset: Diamonds from ggplot2 package" ), plotgrid.args = list(nrow = 2L) ) ``` -------------------------------- ### Grouped Scatter Plot with Labeling and Secondary Axis Source: https://indrajeetpatil.github.io/ggstatsplot/reference/grouped_ggscatterstats.html This example demonstrates using grouped_ggscatterstats with labeling features and modifying the plot by adding a secondary y-axis. It uses the 'mpg' dataset and filters for cylinders not equal to 5. ```R grouped_ggscatterstats( data = filter(ggplot2::mpg, cyl != 5), x = displ, y = hwy, grouping.var = cyl, type = "robust", label.var = manufacturer, label.expression = hwy > 25 & displ > 2.5, ggplot.component = scale_y_continuous(sec.axis = dup_axis()) ) ``` -------------------------------- ### Create Grouped Scatter Plot with Statistics Source: https://indrajeetpatil.github.io/ggstatsplot/reference/grouped_ggscatterstats.html Use grouped_ggscatterstats to generate scatter plots with statistical summaries for different groups. This example filters data for 'Comedy' and 'Drama' genres and adds rug plots to the x-axis. ```R set.seed(123) library(dplyr, warn.conflicts = FALSE) library(ggplot2) grouped_ggscatterstats( data = filter(movies_long, genre == "Comedy" | genre == "Drama"), x = length, y = rating, type = "robust", grouping.var = genre, ggplot.component = list(geom_rug(sides = "b")) ) ```