combine_plots
ggstatsplot
also contains a helper function combine_plots
to combine multiple plots. This is a wrapper around and lets you combine multiple plots and add combination of title, caption, and annotation texts with suitable default parameters.
purrr
The full power of ggstatsplot
can be leveraged with a functional programming package like purrr
that replaces many for loops with code that is both more succinct and easier to read and, therefore, purrr
should be preferrred.
An example is provided below.
library(glue)
library(dplyr)
library(ggplot2)
### creating a list column with `ggstatsplot` plots
plots <- datasets::iris %>%
dplyr::mutate(.data = ., Species2 = Species) %>% # just creates a copy of this variable
dplyr::group_by(.data = ., Species) %>%
tidyr::nest(data = .) %>% # creates a nested dataframe with list column called `data`
dplyr::mutate( # creating a new list column of ggstatsplot outputs
.data = .,
plot = data %>%
purrr::map(
.x = .,
.f = ~ ggstatsplot::ggscatterstats(
data = .,
x = Sepal.Length,
y = Sepal.Width,
messages = FALSE, # turns off all the warnings, notes, and reference messages
marginal.type = "boxplot",
title =
glue::glue("Species: {.$Species2} (n = {length(.$Sepal.Length)})")
)
)
)
### display the new object (notice that the class of the `plot` list column is S3: gg)
plots
#> # A tibble: 3 x 3
#> Species data plot
#> <fct> <list> <list>
#> 1 setosa <tibble [50 x 5]> <S3: ggExtraPlot>
#> 2 versicolor <tibble [50 x 5]> <S3: ggExtraPlot>
#> 3 virginica <tibble [50 x 5]> <S3: ggExtraPlot>
### creating a grid with cowplot
ggstatsplot::combine_plots(
plotlist = plots$plot, # list column containing all ggstatsplot objects
labels = c("(a)", "(b)", "(c)"),
nrow = 3,
ncol = 1,
title.text = "Relationship between sepal length and width for all Iris species",
title.size = 14,
title.color = "black",
caption.text = expression(
paste(
italic("Note"),
": Iris flower dataset was collected by Edgar Anderson."
),
caption.size = 10
)
)
Here is another example with ggbetweenstats
-
library(dplyr)
library(glue)
### creating a list column with `ggstatsplot` plots
plots <- datasets::mtcars %>%
dplyr::mutate(.data = ., cyl2 = cyl) %>% # just creates a copy of this variable
dplyr::group_by(.data = ., cyl) %>% #
tidyr::nest(data = .) %>% # creates a nested dataframe with list column called `data`
dplyr::mutate( # creating a new list column of ggstatsplot outputs
.data = .,
plot = data %>%
purrr::map(
.x = .,
.f = ~ ggstatsplot::ggbetweenstats(
data = .,
x = am,
y = mpg,
plot.type = "box", # type of plot needed
messages = FALSE, # turns off all the warnings, notes, and reference messages
xlab = "Transmission",
ylab = "Miles/(US) gallon",
title = glue::glue(
"Number of cylinders: {.$cyl2}" # this is where the duplicated cyl2 column is useful
)
)
)
)
#> Warning: aesthetic `x` was not a factor; converting it to factorWarning: aesthetic `x` was not a factor; converting it to factorWarning: aesthetic `x` was not a factor; converting it to factor
### display the new object (notice that the class of the `plot` list column is S3: gg)
plots
#> # A tibble: 3 x 3
#> cyl data plot
#> <dbl> <list> <list>
#> 1 6 <tibble [7 x 11]> <S3: gg>
#> 2 4 <tibble [11 x 11]> <S3: gg>
#> 3 8 <tibble [14 x 11]> <S3: gg>
### creating a grid with cowplot
ggstatsplot::combine_plots(
plotlist = plots$plot, # list column containing all ggstatsplot objects
nrow = 3,
ncol = 1,
labels = c("(a)","(b)","(c)"),
title.text = "MPG and car transmission relationship (for each cylinder count)",
title.size = 13,
title.color = "black",
caption.text = expression(
paste(
italic("Transmission"),
": 0 = automatic, 1 = manual"
),
caption.size = 10
)
)
If you find any bugs or have any suggestions/remarks, please file an issue on GitHub: https://github.com/IndrajeetPatil/ggstatsplot/issues