Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ Suggests:
rtdists,
RWiener,
sandwich,
scales,
see (>= 0.11.0),
survival,
testthat (>= 3.2.1),
tinyplot,
tinyplot (>= 0.6.0),
tinytable,
vdiffr,
withr
Expand Down
87 changes: 69 additions & 18 deletions R/tinyplot.R
Original file line number Diff line number Diff line change
@@ -1,33 +1,55 @@
#' @rdname visualisation_recipe.estimate_predicted
#' @param theme A character string specifying the theme to use for the plot.
#' Defaults to `"tufte"`. For other options please see [`tinyplot::tinytheme()`].
#' Use `NULL` if no theme should be applied.
#' @param type The type of `tinyplot` visualization. It is recommended that
#' users leave as `NULL` (the default), in which case the plot type will be
#' determined automatically by the underlying `modelbased` object.
#' @param dodge Dodge value for grouped plots. If `NULL` (the default), then
#' the dodging behavior is determined by the number of groups and
#' `getOption("modelbased_tinyplot_dodge")`.
#' @param ... Other arguments passed to \code{\link[tinyplot]{tinyplot}}.
#'
#' @examplesIf all(insight::check_if_installed(c("tinyplot", "marginaleffects"), quietly = TRUE))
#' # ==============================================
#' # tinyplot
#' # ==============================================
#' \donttest{
#' library(tinyplot)
#' data(efc, package = "modelbased")
#' efc <- datawizard::to_factor(efc, c("e16sex", "c172code", "e42dep"))
#' m <- lm(neg_c_7 ~ e16sex + c172code + barthtot, data = efc)
#'
#' em <- estimate_means(m, "c172code")
#' tinyplot::plt(em)
#' plt(em)
#'
#' # pass additional tinyplot arguments for customization, e.g.
#' plt(em, theme = "classic")
#' plt(em, theme = "classic", flip = TRUE)
#' # etc.
#'
#' # Aside: use tinyplot::tinytheme() to set a persistent theme
#' tinytheme("classic")
#'
#' # continuous variable example
#' em <- estimate_means(m, "barthtot")
#' tinyplot::plt(em)
#' plt(em)
#'
#' # grouped example
#' m <- lm(neg_c_7 ~ e16sex * c172code + e42dep, data = efc)
#' em <- estimate_means(m, c("e16sex", "c172code"))
#' tinyplot::plt(em)
#' plt(em)
#'
#' # use plt_add (alias tinyplot_add) to add layers
#' plt_add(type = "l", lty = 2)
#'
#' # Reset to default theme
#' tinytheme()
#' }
#' @exportS3Method tinyplot::tinyplot
tinyplot.estimate_means <- function(
x,
type = NULL,
dodge = NULL,
show_data = FALSE,
numeric_as_discrete = NULL,
theme = "tufte",
...
) {
insight::check_if_installed("tinyplot")
Expand All @@ -48,9 +70,8 @@ tinyplot.estimate_means <- function(
data <- aes$data
aes <- aes$aes

# save additional arguments, once for theming and once for the plot
# save additional arguments, will pass via do.call to tinyplot
dots <- list(...)
theme_dots <- dots

# preparation of settings / arguments ----------------------------------

Expand All @@ -73,6 +94,11 @@ tinyplot.estimate_means <- function(
}
}

# type placeholder
if (!is.null(type)) {
aes$type <- type
}

# handle non-standard plot types -------------------------------

if (aes$type == "grouplevel") {
Expand Down Expand Up @@ -110,31 +136,56 @@ tinyplot.estimate_means <- function(
# 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")) {

dodge_value <- if (!is.null(dodge)) {
dodge
} else {
getOption("modelbased_tinyplot_dodge", 0.07)
}
if (
!is.null(aes$color) &&
aes$type %in% c("pointrange", "point", "l", "errorbar", "ribbon")
) {
dots$dodge <- dodge_value
}

## TODO: legend labels?
## TODO: show residuals?

# x/y labels --------------------------------
dots$xlab <- aes$labs$x
dots$ylab <- aes$labs$y

# legend labels --------------------------------

# we also need to account for custom legend options passed through dots
if (is.null(dots$legend)) {
dots$legend = list(title = aes$labs$colour)
} else if (inherits(dots$legend, "list")) {
if (!("title" %in% names(dots$legend))) {
dots$legend = utils::modifyList(
dots$legend,
list(title = aes$labs$colour),
keep.null = TRUE
)
}
} else if (!isFALSE(dots$legend)) {
dots$legend = tryCatch(
utils::modifyList(
as.list(dots$legend),
list(title = aes$labs$colour),
keep.null = TRUE
),
error = function(e) dots$legend
)
}

# add aesthetics to the plot description
plot_args <- insight::compact_list(c(
list(plot_description, data = data, type = aes$type),
plot_args,
dots
))

# default theme
if (!is.null(theme)) {
theme_dots[c(elements, "facet", "xlab", "ylab", "flip")] <- NULL
do.call(tinyplot::tinytheme, c(list(theme = theme), theme_dots))
}

# add data points if requested --------------------------------

if (show_data) {
Expand Down
2 changes: 1 addition & 1 deletion R/visualisation_recipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#' @param point,line,pointrange,ribbon,facet,grid Additional
#' aesthetics and parameters for the geoms (see customization example).
#' @param ... Arguments passed from `plot()` to `visualisation_recipe()`, or
#' to `tinyplot()` and `tinytheme()` if you use that method.
#' to `tinyplot()` if you use that method.
#'
#' @details There are two options to remove the confidence bands or errors bars
#' from the plot. To remove error bars, simply set the `pointrange` geom to
Expand Down
40 changes: 31 additions & 9 deletions man/visualisation_recipe.estimate_predicted.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading