11# ' @rdname visualisation_recipe.estimate_predicted
2- # ' @param theme A character string specifying the theme to use for the plot.
3- # ' Defaults to `"tufte"`. For other options please see [`tinyplot::tinytheme()`].
4- # ' Use `NULL` if no theme should be applied.
2+ # ' @param type The type of `tinyplot` visualization. It is recommended that
3+ # ' users leave as `NULL` (the default), in which case the plot type will be
4+ # ' determined automatically by the underlying `modelbased` object.
5+ # ' @param dodge Dodge value for grouped plots. If `NULL` (the default), then
6+ # ' the dodging behavior is determined by the number of groups and
7+ # ' `getOption("modelbased_tinyplot_dodge")`.
8+ # ' @param ... Other arguments passed to \code{\link[tinyplot]{tinyplot}}.
59# '
610# ' @examplesIf all(insight::check_if_installed(c("tinyplot", "marginaleffects"), quietly = TRUE))
711# ' # ==============================================
812# ' # tinyplot
913# ' # ==============================================
1014# ' \donttest{
15+ # ' library(tinyplot)
1116# ' data(efc, package = "modelbased")
1217# ' efc <- datawizard::to_factor(efc, c("e16sex", "c172code", "e42dep"))
1318# ' m <- lm(neg_c_7 ~ e16sex + c172code + barthtot, data = efc)
1419# '
1520# ' em <- estimate_means(m, "c172code")
16- # ' tinyplot:: plt(em)
21+ # ' plt(em)
1722# '
23+ # ' # pass additional tinyplot arguments for customization, e.g.
24+ # ' plt(em, theme = "classic")
25+ # ' plt(em, theme = "classic", flip = TRUE)
26+ # ' # etc.
27+ # '
28+ # ' # Aside: use tinyplot::tinytheme() to set a persistent theme
29+ # ' tinytheme("classic")
30+ # '
31+ # ' # continuous variable example
1832# ' em <- estimate_means(m, "barthtot")
19- # ' tinyplot:: plt(em)
33+ # ' plt(em)
2034# '
35+ # ' # grouped example
2136# ' m <- lm(neg_c_7 ~ e16sex * c172code + e42dep, data = efc)
2237# ' em <- estimate_means(m, c("e16sex", "c172code"))
23- # ' tinyplot::plt(em)
38+ # ' plt(em)
39+ # '
40+ # ' # use plt_add (alias tinyplot_add) to add layers
41+ # ' plt_add(type = "l", lty = 2)
42+ # '
43+ # ' # Reset to default theme
44+ # ' tinytheme()
2445# ' }
2546# ' @exportS3Method tinyplot::tinyplot
2647tinyplot.estimate_means <- function (
2748 x ,
49+ type = NULL ,
50+ dodge = NULL ,
2851 show_data = FALSE ,
2952 numeric_as_discrete = NULL ,
30- theme = " tufte" ,
3153 ...
3254) {
3355 insight :: check_if_installed(" tinyplot" )
@@ -48,9 +70,8 @@ tinyplot.estimate_means <- function(
4870 data <- aes $ data
4971 aes <- aes $ aes
5072
51- # save additional arguments, once for theming and once for the plot
73+ # save additional arguments, will pass via do.call to tinyplot
5274 dots <- list (... )
53- theme_dots <- dots
5475
5576 # preparation of settings / arguments ----------------------------------
5677
@@ -73,6 +94,11 @@ tinyplot.estimate_means <- function(
7394 }
7495 }
7596
97+ # type placeholder
98+ if (! is.null(type )) {
99+ aes $ type <- type
100+ }
101+
76102 # handle non-standard plot types -------------------------------
77103
78104 if (aes $ type == " grouplevel" ) {
@@ -110,31 +136,56 @@ tinyplot.estimate_means <- function(
110136 # Set dodge value for grouped point or pointrange plots.
111137 # The value 0.07 was chosen to reduce overlap in this context; adjust via
112138 # option if needed.
113- dodge_value <- getOption(" modelbased_tinyplot_dodge" , 0.07 )
114- if (! is.null(aes $ color ) && aes $ type %in% c(" pointrange" , " point" )) {
139+
140+ dodge_value <- if (! is.null(dodge )) {
141+ dodge
142+ } else {
143+ getOption(" modelbased_tinyplot_dodge" , 0.07 )
144+ }
145+ if (
146+ ! is.null(aes $ color ) &&
147+ aes $ type %in% c(" pointrange" , " point" , " l" , " errorbar" , " ribbon" )
148+ ) {
115149 dots $ dodge <- dodge_value
116150 }
117151
118- # # TODO: legend labels?
119152 # # TODO: show residuals?
120153
121154 # x/y labels --------------------------------
122155 dots $ xlab <- aes $ labs $ x
123156 dots $ ylab <- aes $ labs $ y
124157
158+ # legend labels --------------------------------
159+
160+ # we also need to account for custom legend options passed through dots
161+ if (is.null(dots $ legend )) {
162+ dots $ legend = list (title = aes $ labs $ colour )
163+ } else if (inherits(dots $ legend , " list" )) {
164+ if (! (" title" %in% names(dots $ legend ))) {
165+ dots $ legend = utils :: modifyList(
166+ dots $ legend ,
167+ list (title = aes $ labs $ colour ),
168+ keep.null = TRUE
169+ )
170+ }
171+ } else if (! isFALSE(dots $ legend )) {
172+ dots $ legend = tryCatch(
173+ utils :: modifyList(
174+ as.list(dots $ legend ),
175+ list (title = aes $ labs $ colour ),
176+ keep.null = TRUE
177+ ),
178+ error = function (e ) dots $ legend
179+ )
180+ }
181+
125182 # add aesthetics to the plot description
126183 plot_args <- insight :: compact_list(c(
127184 list (plot_description , data = data , type = aes $ type ),
128185 plot_args ,
129186 dots
130187 ))
131188
132- # default theme
133- if (! is.null(theme )) {
134- theme_dots [c(elements , " facet" , " xlab" , " ylab" , " flip" )] <- NULL
135- do.call(tinyplot :: tinytheme , c(list (theme = theme ), theme_dots ))
136- }
137-
138189 # add data points if requested --------------------------------
139190
140191 if (show_data ) {
0 commit comments