Skip to content

Commit

Permalink
add show_direction arg
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 29, 2024
1 parent 89174b6 commit 661faae
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
- `plot()` for `model_parameters()` now also plots group-levels of random effects
(i.e. for mixed models, when `model_parameters(x, ..., group_level = TRUE)`).

- `plot()` for `model_parameters()` gets a `show_direction` argument, to turn
off the direction of the effect in the plot.

- `plot()` for `simulate_parameters()` now better copes with models that have
multiple response levels (e.g. multinomial models).

Expand Down
37 changes: 33 additions & 4 deletions R/plot.parameters_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#' @param show_density Should the compatibility density (i.e., posterior,
#' bootstrap, or confidence density) of each parameter be shown?
#' (default: `FALSE`)
#' @param show_direction Should the "direction" of coefficients (e.g., positive
#' or negative coefficients) be highlighted using different colors?
#' (default: `TRUE`)
#' @param log_scale Should exponentiated coefficients (e.g., odds-ratios) be
#' plotted on a log scale? (default: `FALSE`)
#' @param n_columns For models with multiple components (like fixed and random,
Expand All @@ -29,6 +32,14 @@
#'
#' @return A ggplot2-object.
#'
#' @note By default, coefficients and their confidence intervals are colored
#' depending on whether they show a "positive" or "negative" association with
#' the outcome. E.g., in case of linear models, colors simply distinguish positive
#' or negative coefficients. For logistic regression models that are shown on the
#' odds ratio scale, colors distinguish odds ratios above or below 1. Use
#' `show_direction = FALSE` to disable this feature and only show a one-colored
#' forest plot.
#'
#' @examples
#' library(parameters)
#' m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
Expand All @@ -48,6 +59,7 @@ plot.see_parameters_model <- function(x,
show_estimate = TRUE,
show_interval = TRUE,
show_density = FALSE,
show_direction = TRUE,
log_scale = FALSE,
...) {
# retrieve settings ----------------
Expand Down Expand Up @@ -381,10 +393,20 @@ plot.see_parameters_model <- function(x,
color_scale <- scale_color_material()
}

p <- ggplot2::ggplot(x, ggplot2::aes(
y = .data$Parameter, x = .data$Coefficient,
size = rev(.data$CI), color = .data$group
)) +
# should positive/negative coefficients be highlighted?
if (show_direction) {
p <- ggplot2::ggplot(x, ggplot2::aes(
y = .data$Parameter, x = .data$Coefficient,
size = rev(.data$CI), color = .data$group
))
} else {
p <- ggplot2::ggplot(x, ggplot2::aes(
y = .data$Parameter, x = .data$Coefficient,
size = rev(.data$CI)
))
}

p <- p +
ggplot2::geom_vline(ggplot2::aes(xintercept = y_intercept), linetype = "dotted") +
theme_modern(legend.position = "none") +
color_scale
Expand Down Expand Up @@ -416,10 +438,17 @@ plot.see_parameters_model <- function(x,
color_scale <- scale_color_material()
}

if (show_direction) {
p <- ggplot2::ggplot(x, ggplot2::aes(y = .data$Parameter, x = .data$Coefficient, color = .data$group)) +
ggplot2::geom_vline(ggplot2::aes(xintercept = y_intercept), linetype = "dotted") +
theme_modern(legend.position = "none") +
color_scale
} else {
p <- ggplot2::ggplot(x, ggplot2::aes(y = .data$Parameter, x = .data$Coefficient)) +
ggplot2::geom_vline(ggplot2::aes(xintercept = y_intercept), linetype = "dotted") +
theme_modern(legend.position = "none") +
color_scale
}

if (show_density) {
p <- p + density_layer
Expand Down
14 changes: 14 additions & 0 deletions man/plot.see_parameters_model.Rd

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions tests/testthat/test-plot.parameters_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ test_that("`plot.see_parameters_model()` works", {
title = "plot.model_parameters_1",
fig = plot(result)
)
vdiffr::expect_doppelganger(
title = "plot.model_parameters_no_dir",
fig = plot(result, show_direction = FALSE)
)
})

test_that("`plot.see_parameters_model()` random parameters works", {
Expand Down

0 comments on commit 661faae

Please sign in to comment.