Skip to content

Commit

Permalink
New x_limits argument (#307)
Browse files Browse the repository at this point in the history
* New x_limits argument

* lintr

---------

Co-authored-by: Indrajeet Patil <[email protected]>
  • Loading branch information
strengejacke and IndrajeetPatil authored Sep 28, 2023
1 parent 2533ebd commit e987ff6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: see
Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2'
Version: 0.8.0.4
Version: 0.8.0.5
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
QQ-plot. Previous behavior can be restored by setting `type = "density"`.
* Plot method for `binned_residuals()` gains a `show_smooth` argument, to show
or hide the smooth line.
* Plot method for `check_predictions()` gains a `x_limits` argument, to limit
the x-axis-range. This can be useful to "zoom in" certain parts of the plot.

# see 0.8.0

Expand Down
48 changes: 37 additions & 11 deletions R/plot.check_predictions.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ data_plot.performance_pp_check <- function(x, type = "density", ...) {
dataplot$grp <- rep(seq_len(ncol(x)), each = nrow(x))

attr(dataplot, "info") <- list(
"xlab" = attr(x, "response_name"),
"ylab" = ifelse(identical(type, "density"), "Density", "Counts"),
"title" = "Posterior Predictive Check",
"check_range" = attr(x, "check_range"),
"bandwidth" = attr(x, "bandwidth"),
"model_info" = attr(x, "model_info")
xlab = attr(x, "response_name"),
ylab = ifelse(identical(type, "density"), "Density", "Counts"),
title = "Posterior Predictive Check",
check_range = attr(x, "check_range"),
bandwidth = attr(x, "bandwidth"),
model_info = attr(x, "model_info")
)

class(dataplot) <- unique(c("data_plot", "see_performance_pp_check", class(dataplot)))
Expand All @@ -45,6 +45,8 @@ data_plot.performance_pp_check <- function(x, type = "density", ...) {
#' (default), `"discrete_dots"`, `"discrete_interval"` or `"discrete_both"` (the
#' `discrete_*` options are appropriate for models with discrete - binary, integer
#' or ordinal etc. - outcomes).
#' @param x_limits Numeric vector of length 2 specifying the limits of the x-axis.
#' If not `NULL`, will zoom in the x-axis to the specified limits.
#' @inheritParams data_plot
#' @inheritParams plot.see_check_normality
#' @inheritParams plot.see_parameters_distribution
Expand Down Expand Up @@ -75,6 +77,7 @@ print.see_performance_pp_check <- function(x,
style = theme_lucid,
colors = unname(social_colors(c("green", "blue"))),
type = c("density", "discrete_dots", "discrete_interval", "discrete_both"),
x_limits = NULL,
...) {
orig_x <- x
check_range <- isTRUE(attributes(x)$check_range)
Expand All @@ -90,7 +93,16 @@ print.see_performance_pp_check <- function(x,
x <- data_plot(x, type)
}

p1 <- .plot_pp_check(x, size_line, line_alpha, theme_style = style, colors = colors, type = type, ...)
p1 <- .plot_pp_check(
x,
size_line,
line_alpha,
theme_style = style,
colors = colors,
type = type,
x_limits = x_limits,
...
)

if (isTRUE(check_range)) {
p2 <- .plot_pp_check_range(orig_x, size_bar, colors = colors)
Expand All @@ -112,6 +124,7 @@ plot.see_performance_pp_check <- function(x,
style = theme_lucid,
colors = unname(social_colors(c("green", "blue"))),
type = c("density", "discrete_dots", "discrete_interval", "discrete_both"),
x_limits = NULL,
...) {
orig_x <- x
check_range <- isTRUE(attributes(x)$check_range)
Expand All @@ -127,7 +140,16 @@ plot.see_performance_pp_check <- function(x,
x <- data_plot(x, type)
}

p1 <- .plot_pp_check(x, size_line, line_alpha, theme_style = style, colors = colors, type = type, ...)
p1 <- .plot_pp_check(
x,
size_line,
line_alpha,
theme_style = style,
colors = colors,
type = type,
x_limits = x_limits,
...
)

if (isTRUE(check_range)) {
p2 <- .plot_pp_check_range(orig_x, size_bar, colors = colors)
Expand All @@ -139,7 +161,7 @@ plot.see_performance_pp_check <- function(x,



.plot_pp_check <- function(x, size_line, line_alpha, theme_style, colors, type = "density", ...) {
.plot_pp_check <- function(x, size_line, line_alpha, theme_style, colors, type = "density", x_limits = NULL, ...) {
info <- attr(x, "info")

# default bandwidth, for smooting
Expand Down Expand Up @@ -182,6 +204,10 @@ plot.see_performance_pp_check <- function(x,
)
}

if (!is.null(x_limits)) {
out <- out + ggplot2::coord_cartesian(xlim = x_limits)
}

out
}

Expand Down Expand Up @@ -393,13 +419,13 @@ plot.see_performance_pp_check <- function(x,

replicated <- rbind(
data.frame(
x = sapply(x[which(names(x) != "y")], min),
x = vapply(x[which(names(x) != "y")], min, numeric(1)),
group = "Minimum",
color = "Model-predicted data",
stringsAsFactors = FALSE
),
data.frame(
x = sapply(x[which(names(x) != "y")], max),
x = vapply(x[which(names(x) != "y")], max, numeric(1)),
group = "Maximum",
color = "Model-predicted data",
stringsAsFactors = FALSE
Expand Down
5 changes: 5 additions & 0 deletions man/print.see_performance_pp_check.Rd

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

0 comments on commit e987ff6

Please sign in to comment.