Skip to content
Merged
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 @@ -45,12 +67,11 @@
# now need to extract the aesthetics and data and use it to create a tinyplot
# object
aes <- .find_aes(x, model_info, numeric_as_discrete)
data <- aes$data

Check warning on line 70 in R/tinyplot.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/tinyplot.R,line=70,col=3,[object_overwrite_linter] 'data' is an exported object from package 'utils'. Avoid re-using such symbols.
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 @@ -63,7 +84,7 @@
}

# Don't plot raw data for transformed responses with no back-transformation
transform <- attributes(x)$transform

Check warning on line 87 in R/tinyplot.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/tinyplot.R,line=87,col=3,[object_overwrite_linter] 'transform' is an exported object from package 'base'. Avoid re-using such symbols.

if (isTRUE(model_info$is_linear) && !isTRUE(transform)) {
# add information about response transformation
Expand All @@ -73,6 +94,11 @@
}
}

# 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 @@
# 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)) {

Check warning on line 140 in R/tinyplot.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/tinyplot.R,line=140,col=22,[if_not_else_linter] Prefer `if (A) x else y` to the less-readable `if (!A) y else x` in a simple if/else statement.
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 All @@ -148,7 +199,7 @@
}

# add layer
plot_args$draw <- {

Check warning on line 202 in R/tinyplot.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/tinyplot.R,line=202,col=23,[unnecessary_nesting_linter] Reduce the nesting of this statement by removing the braces {}.
tinyplot::tinyplot(
# we need the original response name for the data points
# so we update the formula for the plot description
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 Expand Up @@ -74,7 +74,7 @@
#' 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"

Check warning on line 77 in R/visualisation_recipe.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/visualisation_recipe.R,line=77,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 131 characters.
#' library(ggplot2)
#' library(see)
#' # ==============================================
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
Loading