Skip to content

Commit a2e9caf

Browse files
tinyplot 0.6.0 (#586)
* bump tinyplot version * drop default (persistent) theme * explicit type arg needed for subsequent plt_add * same for dodging layers; requires explicit arg * legend title * tinyplot >= 0.6.0 * update tinyplot vignette and catch for legend = FALSE * document * air format and backticks typo --------- Co-authored-by: Daniel <mail@danielluedecke.de>
1 parent 5c83711 commit a2e9caf

5 files changed

Lines changed: 164 additions & 40 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ Suggests:
9898
rtdists,
9999
RWiener,
100100
sandwich,
101+
scales,
101102
see (>= 0.11.0),
102103
survival,
103104
testthat (>= 3.2.1),
104-
tinyplot,
105+
tinyplot (>= 0.6.0),
105106
tinytable,
106107
vdiffr,
107108
withr

R/tinyplot.R

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,55 @@
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
2647
tinyplot.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) {

R/visualisation_recipe.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#' @param point,line,pointrange,ribbon,facet,grid Additional
4646
#' aesthetics and parameters for the geoms (see customization example).
4747
#' @param ... Arguments passed from `plot()` to `visualisation_recipe()`, or
48-
#' to `tinyplot()` and `tinytheme()` if you use that method.
48+
#' to `tinyplot()` if you use that method.
4949
#'
5050
#' @details There are two options to remove the confidence bands or errors bars
5151
#' from the plot. To remove error bars, simply set the `pointrange` geom to

man/visualisation_recipe.estimate_predicted.Rd

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

0 commit comments

Comments
 (0)