Skip to content

Commit 4c9c8a4

Browse files
authored
Add simple vignette for tinyplot (#571)
* Add simple vignette for tinyplot * update * fix * fix vignette * suppress warnings
1 parent 588fce3 commit 4c9c8a4

9 files changed

Lines changed: 207 additions & 9 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: modelbased
33
Title: Estimation of Model-Based Predictions, Contrasts and Means
4-
Version: 0.13.0.7
4+
Version: 0.13.0.8
55
Authors@R:
66
c(person(given = "Dominique",
77
family = "Makowski",

R/options.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#' - `options(modelbased_full_labels = FALSE)` will remove redundant
3333
#' (duplicated) labels from rows.
3434
#'
35+
#' - `options(easystats_display_format = <value>)` will set the default format
36+
#' for the `display()` methods. Can be one of `"markdown"`, `"html"`, or
37+
#' `"tt"`. See [`display.estimate_contrasts()`] for details.
38+
#'
3539
#' **For plotting**
3640
#'
3741
#' - `options(modelbased_join_dots = <logical>)` will set a default value for
@@ -45,7 +49,7 @@
4549
#' the `alpha` argument of the `ribbon` geom. Should be a number between `0`
4650
#' and `1`.
4751
#'
48-
#' - `options(easystats_display_format = <value>)` will set the default format
49-
#' for the `display()` methods. Can be one of `"markdown"`, `"html"`, or
50-
#' `"tt"`. See [`display.estimate_contrasts()`] for details.
52+
#' - `options(modelbased_tinyplot_dodge = <number>)` will set a default value
53+
#' for the `dodge` argument (spacing between geoms) when using
54+
#' `tinyplot::plt()`. Should be a number between `0` and `1`.
5155
NULL

R/tinyplot.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ tinyplot.estimate_means <- function(
107107

108108
# dodging -------------------------------
109109

110+
# Set dodge value for grouped point or pointrange plots.
111+
# The value 0.07 was chosen to reduce overlap in this context; adjust via
112+
# option if needed.
113+
dodge_value <- getOption("modelbased_tinyplot_dodge", 0.07)
110114
if (!is.null(aes$color) && aes$type %in% c("pointrange", "point")) {
111-
dots$dodge <- 0.1
115+
dots$dodge <- dodge_value
112116
}
113117

114118
## TODO: legend labels?
@@ -162,7 +166,7 @@ tinyplot.estimate_means <- function(
162166
}
163167

164168
# plot it!
165-
do.call(tinyplot::tinyplot, plot_args)
169+
suppressWarnings(do.call(tinyplot::tinyplot, plot_args))
166170
}
167171

168172
#' @exportS3Method tinyplot::tinyplot

R/visualisation_recipe.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
#' will set a default value for the `alpha` argument of the `ribbon` geom.
7171
#' Should be a number between `0` and `1`.
7272
#'
73+
#' - `modelbased_tinyplot_dodge`: `options(modelbased_tinyplot_dodge = <number>)`
74+
#' will set a default value for the `dodge` argument (spacing between geoms)
75+
#' when using `tinyplot::plt()`. Should be a number between `0` and `1`.
76+
#'
7377
#' @examplesIf all(insight::check_if_installed(c("marginaleffects", "see", "ggplot2"), quietly = TRUE)) && getRversion() >= "4.1.0"
7478
#' library(ggplot2)
7579
#' library(see)

man/modelbased-options.Rd

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/visualisation_recipe.estimate_predicted.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgdown/_pkgdown.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ navbar:
6969
- text: "Visualization"
7070
- text: "Plotting estimated marginal means"
7171
href: articles/plotting.html
72+
- text: "Plotting estimated marginal means with tinyplot"
73+
href: articles/plotting_tinyplot.html
7274
- text: "Visualize effects and interactions"
7375
href: articles/estimate_relation.html
7476
- text: "The modelisation approach"

vignettes/overview_of_vignettes.Rmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ All package vignettes are available at [https://easystats.github.io/modelbased/]
4747
### Visualization
4848

4949
* [Plotting estimated marginal means](https://easystats.github.io/modelbased/articles/plotting.html)
50+
* [Plotting estimated marginal means with tinyplot](https://easystats.github.io/modelbased/articles/plotting_tinyplot.html)
5051
* [Visualize effects and interactions](https://easystats.github.io/modelbased/articles/estimate_relation.html)
5152
* [The Modelisation Approach to Statistics](https://easystats.github.io/modelbased/articles/modelisation_approach.html)
5253

vignettes/plotting_tinyplot.Rmd

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
---
2+
title: "Plotting estimated marginal means with tinyplot"
3+
output: rmarkdown::html_vignette
4+
vignette: >
5+
%\VignetteIndexEntry{Plotting estimated marginal means with tinyplot}
6+
%\VignetteEngine{knitr::rmarkdown}
7+
%\VignetteEncoding{UTF-8}
8+
bibliography: bibliography.bib
9+
---
10+
11+
```{r set-options, echo = FALSE}
12+
knitr::opts_chunk$set(
13+
collapse = TRUE,
14+
comment = "#>",
15+
dev = "png",
16+
out.width = "100%",
17+
fig.width = 7,
18+
fig.height = 4,
19+
dpi = 300,
20+
message = FALSE,
21+
warning = FALSE,
22+
package.startup.message = FALSE
23+
)
24+
25+
options(modelbased_join_dots = FALSE)
26+
options(modelbased_select = "minimal")
27+
28+
pkgs <- c("marginaleffects", "tinyplot")
29+
if (!all(insight::check_if_installed(pkgs, quietly = TRUE))) {
30+
knitr::opts_chunk$set(eval = FALSE)
31+
}
32+
if (getRversion() < "4.1.0") {
33+
knitr::opts_chunk$set(eval = FALSE)
34+
}
35+
```
36+
37+
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.
38+
39+
## One predictor - categorical
40+
41+
The simplest case is possibly plotting one categorical predictor. Predicted values for each level and its confidence intervals are shown.
42+
43+
```{r}
44+
library(modelbased)
45+
library(tinyplot)
46+
47+
data(efc, package = "modelbased")
48+
efc <- datawizard::to_factor(efc, c("e16sex", "c172code", "e42dep"))
49+
50+
m <- lm(neg_c_7 ~ e16sex + c172code + barthtot, data = efc)
51+
estimate_means(m, "c172code") |> plt()
52+
```
53+
54+
## One predictor - numeric
55+
56+
For numeric predictors, the range of predictions at different values of the focal predictor are plotted, the uncertainty is displayed as confidence band.
57+
58+
```{r}
59+
estimate_means(m, "barthtot") |> plt()
60+
```
61+
62+
## Two predictors - categorical
63+
64+
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.
65+
66+
```{r}
67+
m <- lm(neg_c_7 ~ e16sex * c172code + e42dep, data = efc)
68+
estimate_means(m, c("e16sex", "c172code")) |> plt()
69+
```
70+
71+
## Two predictors - numeric * categorical
72+
73+
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.
74+
75+
```{r}
76+
m <- lm(neg_c_7 ~ barthtot * c172code + e42dep, data = efc)
77+
estimate_means(m, c("barthtot", "c172code")) |> plt()
78+
```
79+
80+
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.
81+
82+
```{r}
83+
estimate_means(m, c("barthtot", "c172code")) |>
84+
plt(facet = ~c172code)
85+
86+
estimate_means(m, c("barthtot", "c172code")) |>
87+
plt(palette = "okabe")
88+
```
89+
90+
## Two predictors - categorical * numeric
91+
92+
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.
93+
94+
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.
95+
96+
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.
97+
98+
```{r}
99+
# by default, `range = "range"` and `length = 10`
100+
estimate_means(m, c("c172code", "barthtot")) |> plt()
101+
```
102+
103+
That means that the `length` argument can be used to control how many values (lines) for the numeric predictors are chosen.
104+
105+
```{r}
106+
estimate_means(m, c("c172code", "barthtot"), length = 20) |> plt()
107+
```
108+
109+
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.
110+
111+
```{r}
112+
estimate_means(m, c("c172code", "barthtot"), range = "grid") |> plt()
113+
```
114+
115+
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.
116+
117+
```{r}
118+
estimate_means(
119+
m,
120+
c(
121+
"c172code = c('low level of education', 'high level of education')",
122+
"barthtot = c(30, 50, 80)"
123+
)
124+
) |> plt()
125+
126+
estimate_means(m, c("c172code", "barthtot = [fivenum]")) |> plt()
127+
```
128+
129+
## Three numeric predictors
130+
131+
The default plot-setting for three numeric predictors can be rather confusing.
132+
133+
```{r}
134+
m <- lm(neg_c_7 ~ c12hour * barthtot * c160age, data = efc)
135+
estimate_means(m, c("c12hour", "barthtot", "c160age")) |> plt()
136+
```
137+
138+
Instead, it is recommended to use `length`, create a "reference grid", or again specify meaningful values directly in the `by` argument.
139+
140+
```{r}
141+
estimate_means(m, c("c12hour", "barthtot", "c160age"), length = 2) |> plt()
142+
143+
estimate_means(m, c("c12hour", "barthtot", "c160age"), range = "grid") |> plt()
144+
```
145+
146+
## Three categorical predictors
147+
148+
Multiple categorical predictors are usually less problematic, since discrete color scales and faceting are used to distinguish between factor levels.
149+
150+
```{r}
151+
m <- lm(neg_c_7 ~ e16sex * c172code * e42dep, data = efc)
152+
estimate_means(m, c("e16sex", "c172code", "e42dep")) |> plt()
153+
```
154+
155+
## Smooth plots
156+
157+
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.
158+
159+
```{r}
160+
m <- lm(neg_c_7 ~ e16sex * c12hour + e16sex * I(c12hour^2), data = efc)
161+
estimate_means(m, c("c12hour", "e16sex")) |> plt()
162+
```
163+
164+
In this case, simply increase the number of representative values by setting `length` to a higher number.
165+
166+
```{r}
167+
estimate_means(m, c("c12hour", "e16sex"), length = 200) |> plt()
168+
```
169+
170+
```{r echo=FALSE}
171+
# reset options
172+
options(
173+
modelbased_join_dots = NULL,
174+
modelbased_estimate = NULL,
175+
modelbased_select = NULL
176+
)
177+
```

0 commit comments

Comments
 (0)