# combine_plots

## Combining multiple plots: Example #2 with plyr

Another popular package for handling big datasets is plyr, which allows us to repeatedly apply a common function on smaller pieces and then combine the results into a larger whole.

In this example we'll start with the gapminder dataset. We're interested in the linear relationship between Gross Domestic Product (per capita) and life expectancy in the year 2007, for all the continents except Oceania. We'll use dplyr to filter to the right rows then use plyr to repeat the ggscatterstats function across each of the 4 continents remaining. The result is of that is a list of plots called plots. We then feed plots to the combine_plots function to merge them into one plot. We will call attention to the countries which have very low life expectancy (< 45 years) by labeling those countries when they occur.

library(plyr) library(gapminder) # for reproducibility set.seed(123) # let's have a look at the structure of the data dplyr::glimpse(x = gapminder::gapminder) # creating a list of plots plots <- plyr::dlply( .data = dplyr::filter(gapminder::gapminder, year == 2007, continent != "Oceania"), .variables = .(continent), .fun = function(data) ggstatsplot::ggscatterstats( data = data, x = gdpPercap, y = lifeExp, xfill = "#0072B2", yfill = "#009E73", label.var = "country", label.expression = "lifeExp < 45", title = glue::glue("Continent: {data\$continent}"), marginal = FALSE ) + ggplot2::scale_x_continuous(labels = scales::comma) ) # combining individual plots ggstatsplot::combine_plots( plotlist = plots, title.text = "Relationship between GDP (per capita) and life expectancy", nrow = 2 )

The combine_plots function can also be useful for adding additional textual information that can not be added by making a single call to a ggstatsplot function via the title, subtitle, or caption options. For this example let's assume we want to assess the relationship between a movie's rating and its budget from the Internet Movie Database using polynomial regression.
ggcoefstats will do most of the work, including the title, subtitle, and caption. But we want to add at the bottom an annotation to show the formula we are using for our regression. combine_plots allows us to add sub.text = to accomplish that task as shown in the resulting plot.
library(ggstatsplot) ggstatsplot::combine_plots( # preparing the plot with ggstatsplot function # let's use only 20% of data to speed up the process ggstatsplot::ggcoefstats( x = stats::lm( formula = rating ~ stats::poly(budget, degree = 3), data = dplyr::sample_frac(tbl = ggstatsplot::movies_long, size = 0.2), na.action = na.omit ), exclude.intercept = FALSE, title = "Relationship between movie budget and IMDB rating", subtitle = "Source: Internet Movie Database", ggtheme = ggplot2::theme_gray(), stats.label.color = c("#CC79A7", "darkgreen", "#0072B2", "red") ) + # modifying the plot outside of ggstatsplot using ggplot2 functions ggplot2::scale_y_discrete( labels = c( "Intercept (c)", "1st degree (b1)", "2nd degree (b2)", "3rd degree (b3)" ) ) + ggplot2::labs(y = "term (polynomial regression)"), # adding additional text element to the plot since title, subtitle, caption are all already occupied sub.text = expression( paste( "linear model: ", bolditalic(y), " ~ ", italic(c) + italic(b)[1] * bolditalic(x) + italic(b)[2] * bolditalic(x) ^ 2 + italic(b)[3] * bolditalic(x) ^ 3, sep = "" ) ), sub.size = 12 )
If you find any bugs or have any suggestions/remarks, please file an issue on GitHub: https://github.com/IndrajeetPatil/ggstatsplot/issues