From 0bce0f880a38100c1cf22c8aaa325e2abd4f2d16 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 24 Sep 2025 11:43:38 +0200 Subject: [PATCH 1/5] Add simple vignette for tinyplot --- R/tinyplot.R | 2 +- pkgdown/_pkgdown.yml | 2 + vignettes/overview_of_vignettes.Rmd | 1 + vignettes/plotting.Rmd | 2 +- vignettes/plotting_tinyplot.Rmd | 175 ++++++++++++++++++++++++++++ 5 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 vignettes/plotting_tinyplot.Rmd diff --git a/R/tinyplot.R b/R/tinyplot.R index 631482b66..bbab46b0b 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -108,7 +108,7 @@ tinyplot.estimate_means <- function( # dodging ------------------------------- if (!is.null(aes$color) && aes$type %in% c("pointrange", "point")) { - dots$dodge <- 0.1 + dots$dodge <- 0.07 } ## TODO: legend labels? diff --git a/pkgdown/_pkgdown.yml b/pkgdown/_pkgdown.yml index d299c73fc..a7627691a 100644 --- a/pkgdown/_pkgdown.yml +++ b/pkgdown/_pkgdown.yml @@ -69,6 +69,8 @@ navbar: - text: "Visualization" - text: "Plotting estimated marginal means" href: articles/plotting.html + - text: "Plotting estimated marginal means with tinyplot" + href: articles/plotting_tinyplot.html - text: "Visualize effects and interactions" href: articles/estimate_relation.html - text: "The modelisation approach" diff --git a/vignettes/overview_of_vignettes.Rmd b/vignettes/overview_of_vignettes.Rmd index caeaa8962..4d37246c1 100644 --- a/vignettes/overview_of_vignettes.Rmd +++ b/vignettes/overview_of_vignettes.Rmd @@ -47,6 +47,7 @@ All package vignettes are available at [https://easystats.github.io/modelbased/] ### Visualization * [Plotting estimated marginal means](https://easystats.github.io/modelbased/articles/plotting.html) +* [Plotting estimated marginal means with tinyplot](https://easystats.github.io/modelbased/articles/plotting_tinyplot.html) * [Visualize effects and interactions](https://easystats.github.io/modelbased/articles/estimate_relation.html) * [The Modelisation Approach to Statistics](https://easystats.github.io/modelbased/articles/modelisation_approach.html) diff --git a/vignettes/plotting.Rmd b/vignettes/plotting.Rmd index 719a11d3e..0c07ffaf1 100644 --- a/vignettes/plotting.Rmd +++ b/vignettes/plotting.Rmd @@ -25,7 +25,7 @@ knitr::opts_chunk$set( options(modelbased_join_dots = FALSE) options(modelbased_select = "minimal") -pkgs <- c("marginaleffects", "ggplot2", "see") +pkgs <- c("marginaleffects", "ggplot2", "see", "tinyplot") if (!all(insight::check_if_installed(pkgs, quietly = TRUE))) { knitr::opts_chunk$set(eval = FALSE) } diff --git a/vignettes/plotting_tinyplot.Rmd b/vignettes/plotting_tinyplot.Rmd new file mode 100644 index 000000000..4bb1a535a --- /dev/null +++ b/vignettes/plotting_tinyplot.Rmd @@ -0,0 +1,175 @@ +--- +title: "Plotting estimated marginal means with tinyplot" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Plotting estimated marginal means with tinyplot} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +bibliography: bibliography.bib +--- + +```{r set-options, echo = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>", + dev = "png", + out.width = "100%", + fig.width = 7, + fig.height = 4, + dpi = 300, + message = FALSE, + warning = FALSE, + package.startup.message = FALSE +) + +options(modelbased_join_dots = FALSE) +options(modelbased_select = "minimal") + +pkgs <- c("marginaleffects", "ggplot2", "see", "tinyplot") +if (!all(insight::check_if_installed(pkgs, quietly = TRUE))) { + knitr::opts_chunk$set(eval = FALSE) +} +if (getRversion() < "4.1.0") { + knitr::opts_chunk$set(eval = FALSE) +} +``` + +This vignette provides a quick overview with different examples that show how to plot estimated marginal means, like [in this vignette](https://easystats.github.io/modelbased/articles/plotting.html), however, here we use the [`{tinyplot}`](https://grantmcdermott.com/tinyplot/) package instead of `{ggplot2}` to create the plots. + +## One predictor - categorical + +The simplest case is possibly plotting one categorical predictor. Predicted values for each level and its confidence intervals are shown. + +```{r} +library(modelbased) +data(efc, package = "modelbased") +efc <- datawizard::to_factor(efc, c("e16sex", "c172code", "e42dep")) + +m <- lm(neg_c_7 ~ e16sex + c172code + barthtot, data = efc) +estimate_means(m, "c172code") |> plt() +``` + +## One predictor - numeric + +For numeric predictors, the range of predictions at different values of the focal predictor are plotted, the uncertainty is displayed as confidence band. + +```{r} +estimate_means(m, "barthtot") |> plt() +``` + +## Two predictors - categorical + +For two categorical predictors, the first focal predictors is plotted along the x-axis, while the levels of the second predictor are mapped to different colors. + +```{r} +m <- lm(neg_c_7 ~ e16sex * c172code + e42dep, data = efc) +estimate_means(m, c("e16sex", "c172code")) |> plt() +``` + +## Two predictors - numeric * categorical + +For two predictors, where the first is numeric and the second categorical, range of predictions including confidence bands are shown, with the different levels of the second (categorical) predictor mapped to colors again. + +```{r} +m <- lm(neg_c_7 ~ barthtot * c172code + e42dep, data = efc) +estimate_means(m, c("barthtot", "c172code")) |> plt() +``` + +In general, plots can be further modified using functions or arguments from the **tinyplot** package. Thereby, other themes, color scales, faceting and so on, can be applies. + +```{r} +estimate_means(m, c("barthtot", "c172code")) |> + plt(facet = ~c172code) + +estimate_means(m, c("barthtot", "c172code")) |> + plt(palette = "okabe") +``` + +## Two predictors - categorical * numeric + +If the numeric predictor is the _second_ focal term, its values are still mapped to colors, however, by default to a continuous (gradient) scale, because a range of representative values for that numeric predictor is used by default. + +Focal predictors specified in `estimate_means()` are passed to `insight::get_datagrid()`. If not specified otherwise, representative values for numeric predictors are evenly distributed from the minimum to the maximum, with a total number of `length` values covering that range. + +I.e., by default, arguments `range = "range"` and `length = 10` in `insight::get_datagrid()`, and thus for numeric predictors, a _range_ of _length_ values is used to estimate predictions. + +```{r} +# by default, `range = "range"` and `length = 10` +estimate_means(m, c("c172code", "barthtot")) |> plt() +``` + +That means that the `length` argument can be used to control how many values (lines) for the numeric predictors are chosen. + +```{r} +estimate_means(m, c("c172code", "barthtot"), length = 20) |> plt() +``` + +Another option would be to use `range = "grid"`, in which case the mean and +/- one standard deviation around the mean are chosen as representative values for numeric predictors. + +```{r} +estimate_means(m, c("c172code", "barthtot"), range = "grid") |> plt() +``` + +It is also possible to specify representative values, at which the estimated marginal means of the outcome should be plotted. Again, consult the documentation at `?ìnsight::get_datagrid` for further details. + +```{r} +estimate_means( + m, + c( + "c172code = c('low level of education', 'high level of education')", + "barthtot = c(30, 50, 80)" + ) +) |> plt() + +estimate_means(m, c("c172code", "barthtot = [fivenum]")) |> plt() +``` + +## Three numeric predictors + +The default plot-setting for three numeric predictors can be rather confusing. + +```{r} +m <- lm(neg_c_7 ~ c12hour * barthtot * c160age, data = efc) +estimate_means(m, c("c12hour", "barthtot", "c160age")) |> plt() +``` + +Instead, it is recommended to use `length`, create a "reference grid", or again specify meaningful values directly in the `by` argument. + +```{r} +estimate_means(m, c("c12hour", "barthtot", "c160age"), length = 2) |> plt() + +estimate_means(m, c("c12hour", "barthtot", "c160age"), range = "grid") |> plt() +``` + +## Three categorical predictors + +Multiple categorical predictors are usually less problematic, since discrete color scales and faceting are used to distinguish between factor levels. + +```{r} +m <- lm(neg_c_7 ~ e16sex * c172code * e42dep, data = efc) +estimate_means(m, c("e16sex", "c172code", "e42dep")) |> plt() +``` + +## Smooth plots + +Remember that by default a range of ten values is chosen for numeric focal predictors. While this mostly works well for plotting linear relationships, plots may look less smooth for certain models that involve quadratic or cubic terms, or splines, or for instance if you have GAMs. + +```{r} +m <- lm(neg_c_7 ~ e16sex * c12hour + e16sex * I(c12hour^2), data = efc) +estimate_means(m, c("c12hour", "e16sex")) |> plt() +``` + +In this case, simply increase the number of representative values by setting `length` to a higher number. + +```{r} +estimate_means(m, c("c12hour", "e16sex"), length = 200) |> plt() +``` + +```{r echo=FALSE} +# reset options +options( + modelbased_join_dots = NULL, + modelbased_estimate = NULL, + modelbased_select = NULL +) +``` From 6230daee1b536a36cb8c3f30bd2e63bd99a90aad Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 24 Sep 2025 11:49:40 +0200 Subject: [PATCH 2/5] update --- R/options.R | 10 +++++++--- R/tinyplot.R | 6 +++++- R/visualisation_recipe.R | 4 ++++ man/modelbased-options.Rd | 9 ++++++--- man/visualisation_recipe.estimate_predicted.Rd | 3 +++ vignettes/plotting_tinyplot.Rmd | 4 ++-- 6 files changed, 27 insertions(+), 9 deletions(-) diff --git a/R/options.R b/R/options.R index af4d596ff..0c4f9297f 100644 --- a/R/options.R +++ b/R/options.R @@ -32,6 +32,10 @@ #' - `options(modelbased_full_labels = FALSE)` will remove redundant #' (duplicated) labels from rows. #' +#' - `options(easystats_display_format = )` will set the default format +#' for the `display()` methods. Can be one of `"markdown"`, `"html"`, or +#' `"tt"`. See [`display.estimate_contrasts()`] for details. +#' #' **For plotting** #' #' - `options(modelbased_join_dots = )` will set a default value for @@ -45,7 +49,7 @@ #' the `alpha` argument of the `ribbon` geom. Should be a number between `0` #' and `1`. #' -#' - `options(easystats_display_format = )` will set the default format -#' for the `display()` methods. Can be one of `"markdown"`, `"html"`, or -#' `"tt"`. See [`display.estimate_contrasts()`] for details. +#' - `options(modelbased_tinyplot_dodge = )` will set a default value +#' for the `dodge` argument (spacing between geoms) when using +#' `tinyplot::plt()`. Should be a number between `0` and `1`. NULL diff --git a/R/tinyplot.R b/R/tinyplot.R index bbab46b0b..ed84b6c2a 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -107,8 +107,12 @@ tinyplot.estimate_means <- function( # dodging ------------------------------- + # Set dodge value for grouped point or pointrange plots. + # The value 0.07 was chosen to reduce overlap in this context; adjust via + # option if needed. + dodge_value <- getOption("modelbased_tinyplot_dodge", 0.07) if (!is.null(aes$color) && aes$type %in% c("pointrange", "point")) { - dots$dodge <- 0.07 + dots$dodge <- dodge_value } ## TODO: legend labels? diff --git a/R/visualisation_recipe.R b/R/visualisation_recipe.R index d1d5d6cb0..86336be32 100644 --- a/R/visualisation_recipe.R +++ b/R/visualisation_recipe.R @@ -70,6 +70,10 @@ #' will set a default value for the `alpha` argument of the `ribbon` geom. #' Should be a number between `0` and `1`. #' +#' - `modelbased_tinyplot_dodge`: `options(modelbased_tinyplot_dodge = )` +#' will set a default value for the `dodge` argument (spacing between geoms) +#' when using `tinyplot::plt()`. Should be a number between `0` and `1`. +#' #' @examplesIf all(insight::check_if_installed(c("marginaleffects", "see", "ggplot2"), quietly = TRUE)) && getRversion() >= "4.1.0" #' library(ggplot2) #' library(see) diff --git a/man/modelbased-options.Rd b/man/modelbased-options.Rd index 33e0cc7e6..6a397b462 100644 --- a/man/modelbased-options.Rd +++ b/man/modelbased-options.Rd @@ -34,6 +34,9 @@ printing. by default or not. \item \code{options(modelbased_full_labels = FALSE)} will remove redundant (duplicated) labels from rows. +\item \verb{options(easystats_display_format = )} will set the default format +for the \code{display()} methods. Can be one of \code{"markdown"}, \code{"html"}, or +\code{"tt"}. See \code{\link[=display.estimate_contrasts]{display.estimate_contrasts()}} for details. } \strong{For plotting} @@ -46,9 +49,9 @@ value for the \code{modelbased_numeric_as_discrete} argument. Can also be \item \verb{options(modelbased_ribbon_alpha = )} will set a default value for the \code{alpha} argument of the \code{ribbon} geom. Should be a number between \code{0} and \code{1}. -\item \verb{options(easystats_display_format = )} will set the default format -for the \code{display()} methods. Can be one of \code{"markdown"}, \code{"html"}, or -\code{"tt"}. See \code{\link[=display.estimate_contrasts]{display.estimate_contrasts()}} for details. +\item \verb{options(modelbased_tinyplot_dodge = )} will set a default value +for the \code{dodge} argument (spacing between geoms) when using +\code{tinyplot::plt()}. Should be a number between \code{0} and \code{1}. } } diff --git a/man/visualisation_recipe.estimate_predicted.Rd b/man/visualisation_recipe.estimate_predicted.Rd index b20ca3538..64d73aabf 100644 --- a/man/visualisation_recipe.estimate_predicted.Rd +++ b/man/visualisation_recipe.estimate_predicted.Rd @@ -136,6 +136,9 @@ Can also be \code{FALSE}. \item \code{modelbased_ribbon_alpha}: \verb{options(modelbased_ribbon_alpha = )} will set a default value for the \code{alpha} argument of the \code{ribbon} geom. Should be a number between \code{0} and \code{1}. +\item \code{modelbased_tinyplot_dodge}: \verb{options(modelbased_tinyplot_dodge = )} +will set a default value for the \code{dodge} argument (spacing between geoms) +when using \code{tinyplot::plt()}. Should be a number between \code{0} and \code{1}. } } diff --git a/vignettes/plotting_tinyplot.Rmd b/vignettes/plotting_tinyplot.Rmd index 4bb1a535a..c3c32d5b8 100644 --- a/vignettes/plotting_tinyplot.Rmd +++ b/vignettes/plotting_tinyplot.Rmd @@ -75,7 +75,7 @@ m <- lm(neg_c_7 ~ barthtot * c172code + e42dep, data = efc) estimate_means(m, c("barthtot", "c172code")) |> plt() ``` -In general, plots can be further modified using functions or arguments from the **tinyplot** package. Thereby, other themes, color scales, faceting and so on, can be applies. +In general, plots can be further modified using functions or arguments from the **tinyplot** package. Thereby, other themes, color scales, faceting and so on, can be applied. ```{r} estimate_means(m, c("barthtot", "c172code")) |> @@ -110,7 +110,7 @@ Another option would be to use `range = "grid"`, in which case the mean and +/- estimate_means(m, c("c172code", "barthtot"), range = "grid") |> plt() ``` -It is also possible to specify representative values, at which the estimated marginal means of the outcome should be plotted. Again, consult the documentation at `?ìnsight::get_datagrid` for further details. +It is also possible to specify representative values, at which the estimated marginal means of the outcome should be plotted. Again, consult the documentation at `?insight::get_datagrid` for further details. ```{r} estimate_means( From 3aca3b25cd85264717edc11d9e4836b0e5100322 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 24 Sep 2025 11:54:21 +0200 Subject: [PATCH 3/5] fix --- vignettes/plotting.Rmd | 2 +- vignettes/plotting_tinyplot.Rmd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vignettes/plotting.Rmd b/vignettes/plotting.Rmd index 0c07ffaf1..719a11d3e 100644 --- a/vignettes/plotting.Rmd +++ b/vignettes/plotting.Rmd @@ -25,7 +25,7 @@ knitr::opts_chunk$set( options(modelbased_join_dots = FALSE) options(modelbased_select = "minimal") -pkgs <- c("marginaleffects", "ggplot2", "see", "tinyplot") +pkgs <- c("marginaleffects", "ggplot2", "see") if (!all(insight::check_if_installed(pkgs, quietly = TRUE))) { knitr::opts_chunk$set(eval = FALSE) } diff --git a/vignettes/plotting_tinyplot.Rmd b/vignettes/plotting_tinyplot.Rmd index c3c32d5b8..00fba662d 100644 --- a/vignettes/plotting_tinyplot.Rmd +++ b/vignettes/plotting_tinyplot.Rmd @@ -25,7 +25,7 @@ knitr::opts_chunk$set( options(modelbased_join_dots = FALSE) options(modelbased_select = "minimal") -pkgs <- c("marginaleffects", "ggplot2", "see", "tinyplot") +pkgs <- c("marginaleffects", "tinyplot") if (!all(insight::check_if_installed(pkgs, quietly = TRUE))) { knitr::opts_chunk$set(eval = FALSE) } From 284347a91da0e34282582a105975ca4dfa58f0a1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 24 Sep 2025 12:47:10 +0200 Subject: [PATCH 4/5] fix vignette --- DESCRIPTION | 2 +- vignettes/plotting_tinyplot.Rmd | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index f3a290993..2bf9b5d6d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: modelbased Title: Estimation of Model-Based Predictions, Contrasts and Means -Version: 0.13.0.7 +Version: 0.13.0.8 Authors@R: c(person(given = "Dominique", family = "Makowski", diff --git a/vignettes/plotting_tinyplot.Rmd b/vignettes/plotting_tinyplot.Rmd index 00fba662d..6981b72b7 100644 --- a/vignettes/plotting_tinyplot.Rmd +++ b/vignettes/plotting_tinyplot.Rmd @@ -42,6 +42,8 @@ The simplest case is possibly plotting one categorical predictor. Predicted valu ```{r} library(modelbased) +library(tinyplot) + data(efc, package = "modelbased") efc <- datawizard::to_factor(efc, c("e16sex", "c172code", "e42dep")) From 548bb3f822e29030efe311eff142c4f9d20b44d4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 24 Sep 2025 13:41:26 +0200 Subject: [PATCH 5/5] suppress warnings --- R/tinyplot.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/tinyplot.R b/R/tinyplot.R index ed84b6c2a..b1984bba0 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -166,7 +166,7 @@ tinyplot.estimate_means <- function( } # plot it! - do.call(tinyplot::tinyplot, plot_args) + suppressWarnings(do.call(tinyplot::tinyplot, plot_args)) } #' @exportS3Method tinyplot::tinyplot