knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) # Why purrr? Most of the ggstatsplot functions have grouped_ variants, which are designed to quickly run the same ggstatsplot function across multiple levels of a single grouping variable. Although this function is useful for data exploration, it has two strong weaknesses- • The arguments applied to grouped_ function call are applied uniformly to all levels of the grouping variable when we might want to customize them for different levels of the grouping variable. • Only one grouping variable can be used to repeat the analysis when in reality there can be a combination of grouping variables and the operation needs to be repeated for all resulting combinations. We will see how to overcome this limitation by combining ggstatsplot with the purrr package. Note: • Unlike the typical function call for ggstatsplot functions where arguments can be quoted ("x") or unquoted (x), while using purrr::pmap, we must quote the arguments. • You can use ggplot2 themes from extension packages (like ggthemes). • If you'd like some more background or an introduction to the purrr package, please see this tutorial. # Introduction and methodology For all the examples in this vignette we are going to build lists of things that we will pass along to purrr which will in turn return a list of plots that will be passed to combine_plots. As the name implies combine_plots merges the individual plots into one bigger plot with common labeling and aesthetics. What are these lists that we are building? The lists correspond to the parameters in our ggstatsplot function like ggbetweenstats. If you look at the help file for ?ggbetweenstats for example the very first parameter it wants is the data file we'll be using. We can also pass it different titles of even ggthemes. You can pass: • A single chr string such as xlab = "Continent" or numeric such as nboot = 25 in which case it will be reused/recycled as many times as needed. • A vector of values such as mean.label.size = c(3, 4, 5) in which case it will be coerced to a list and checked for the right class (in this case int) and the right quantity of entries in the vector i.e., mean.label.size = c(3, 4) will fail if we're trying to make three plots. • A list; either named data = year_list or created as you go outlier.label.color = list("#56B4E9", "#009E73", "#F0E442"). Any list will be checked for the right class (in this case chr) and the right quantity of entries in the list. # ggbetweenstats Following our methodology above let's start with ggebtweenstats. We'll use the gapminder dataset. We'll make a 3 item named list called data_list using dplyr::filter and base::split. library(ggstatsplot) # for reproducibility set.seed(123) # let's split the dataframe and create a list by years of interest year_list <- gapminder::gapminder %>% dplyr::filter( .data = ., year == 1967 | year == 1987 | year == 2007, continent != "Oceania" ) %>% base::split(x = ., f = .$year, drop = TRUE) # this created a list with 3 elements, one for each year we want # you can check the structure of the file for yourself # str(year_list[1]) # checking the length of the list and the names of each element length(year_list) names(year_list)

Now that we have the data divided into the three relevant years in a list we'll turn to purrr::pmap to create a list of ggplot objects that we'll make use of stored in plot_list. When you look at the documentation for ?pmap it will accept .l which is a list of lists. The length of .l determines the number of arguments that .f will be called with. List names will be used if present. .f is the function we want to apply. In our case .f = ggstatsplot::ggbetweenstats.

So let's keep building .l.First is data = year_list, the x & y axes are constant in all three plots so we pass the variable name as a string x = "continent". Same with the label we'll use for outliers where needed. For demonstration purposes let's assume we want the outliers on each plot to be a different color. Not actually recommending it just demonstrating what's possible. We build a list on the fly with outlier.label.color = list("#56B4E9", "#009E73", "#F0E442"). The rest of the code shows you a wide variety of possibilities and we won't catalog them here.

plot_list <- purrr::pmap( .l = list( data = year_list, x = "continent", y = "lifeExp", outlier.tagging = TRUE, outlier.label = "country", outlier.label.color = list( "#56B4E9", "#009E73", "#F0E442" ), xlab = "Continent", ylab = "Life expectancy", title = list( "Year: 1967", "Year: 1987", "Year: 2007" ), type = list("r", "bf", "np"), pairwise.comparisons = TRUE, pairwise.display = list("s", "ns", "all"), pairwise.annotation = list("asterisk", "asterisk", "p.value"), p.adjust.method = list("hommel", "bonferroni", "BH"), nboot = 25, conf.level = list(0.99, 0.95, 0.90), mean.label.size = c(3, 4, 5), k = list(1, 2, 3), effsize.type = list( NULL, "partial_omega", "partial_eta" ), plot.type = list("box", "boxviolin", "violin"), mean.ci = list(TRUE, FALSE, FALSE), package = list("nord", "ochRe", "awtools"), palette = list("aurora", "parliament", "ppalette"), ggtheme = list( ggthemes::theme_stata(), ggplot2::theme_classic(), ggthemes::theme_fivethirtyeight() ), ggstatsplot.layer = list(FALSE, FALSE, FALSE), sample.size.label = list(TRUE, FALSE, TRUE), messages = FALSE ), .f = ggstatsplot::ggbetweenstats )

The final step is to pass the plot_list object we just created to the combine_plots function. While each of the 3 plots already has labeling information combine_plots gives us an opportunity to add additional details to the merged plots and specify the layout in rows and columns.

# combining all individual plots from the list into a single plot using combine_plots function ggstatsplot::combine_plots( plotlist = plot_list, title.text = "Changes in life expectancy across continents (1967-2007)", title.color = "red", nrow = 3, ncol = 1 )

# ggscatterstats

For the next example lets use the same methodology on different data and using ggscatterstats to produce scatterplots combined with marginal histograms/boxplots/density plots with statistical details added as a subtitle.

For data we'll use movies_wide which is from IMDB and part of the ggstatsplot package. Since it's a large dataset with some relatively small categories like NC-17 we'll sample only one quarter of the data and completely drop NC-17 using dplyr. Once again we'll feed purrr::pmap a list of lists via .l and for .f we'll specify .f = ggstatsplot::ggscatterstats.

This time we'll put all the code in one block...

# grouped_ variants

Note that although all the above examples were written with the non-grouped variants of functions, the same rule holds true for the grouped_ variants of all the above functions.

For example, if we want to use the grouped_gghistostats across three different datasets, you can use purrr::pmap() function. For the sake of brevity, the plots are not displayed here, but you can run the following code and check the individual grouped_ plots (e.g., plotlist[[1]]).

# create a list of plots plotlist <- purrr::pmap( .l = list( data = list(mtcars, iris, ToothGrowth), x = alist(wt, Sepal.Length, len), results.subtitle = list(FALSE), grouping.var = alist(am, Species, supp), title.text = list("Dataset: mtcars", "Dataset: iris", "Dataset: ToothGrowth"), messages = list(FALSE) ), .f = ggstatsplot::grouped_gghistostats ) # given that we had three different datasets, we expect a list of length 3 # (each of which contains a grouped_ plot) length(plotlist)

# Suggestions

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