Skip to content

Commit 8ffd7db

Browse files
committed
fixes, docs
1 parent 45cb9b5 commit 8ffd7db

File tree

4 files changed

+66
-36
lines changed

4 files changed

+66
-36
lines changed

R/collapse_by_group.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ collapse_by_group <- function(grid, model, collapse_by = NULL, residuals = FALSE
3434

3535
model_data <- insight::get_data(model, source = "frame", verbose = FALSE)
3636

37-
if (is.null(collapse_by) || isTRUE(collapse_by)) {
37+
if (is.null(collapse_by) || isTRUE(residuals)) {
3838
collapse_by <- insight::find_random(model, flatten = TRUE)
3939
}
4040

@@ -47,15 +47,15 @@ collapse_by_group <- function(grid, model, collapse_by = NULL, residuals = FALSE
4747
}
4848

4949
if (!collapse_by %in% colnames(model_data)) {
50-
insight::format_error("Could not find `", collapse_by, "` column.")
50+
insight::format_error(paste0("Could not find `", collapse_by, "` column."))
5151
}
5252

5353
if (residuals) {
5454
rawdata <- residualize_over_grid(grid, model)
5555
y_name <- "Mean"
5656
} else {
5757
rawdata <- insight::get_data(model, source = "environment", verbose = FALSE)
58-
y_name <- attributes(grid)$response
58+
y_name <- insight::find_response(model)
5959

6060
# we need this column for labelling data points, but not for collapsing
6161
rawdata$rowname <- NULL
@@ -83,6 +83,10 @@ collapse_by_group <- function(grid, model, collapse_by = NULL, residuals = FALSE
8383
FUN = mean
8484
)
8585

86+
if (residuals) {
87+
y_name <- insight::find_response(model)
88+
}
89+
8690
colnames(agg_data)[ncol(agg_data)] <- y_name
8791
colnames(agg_data)[colnames(agg_data) == "group"] <- "group_col"
8892

R/visualisation_recipe.R

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
#'
2424
#' @param x A modelbased object.
2525
#' @param show_data Logical, if `TRUE`, display the "raw" data as a background
26-
#' to the model-based estimation. This argument will be ignored for plotting
27-
#' objects returned by `estimate_slopes()` or `estimate_grouplevel()`.
26+
#' to the model-based estimation. For mixed models, you can additional use the
27+
#' `collapse_group` argument to "collapse" data points by random effects
28+
#' grouping factors. Argument `show_data` will be ignored for plotting objects
29+
#' returned by `estimate_slopes()` or `estimate_grouplevel()`.
2830
#' @param join_dots Logical, if `TRUE` (default) and for categorical focal terms
2931
#' in `by`, dots (estimates) are connected by lines, i.e. plots will be a
3032
#' combination of dots with error bars and connecting lines. If `FALSE`, only
@@ -39,14 +41,17 @@
3941
#' predictor. Use `FALSE` to always use continuous color scales for numeric
4042
#' predictors. It is possible to set a global default value using `options()`,
4143
#' e.g. `options(modelbased_numeric_as_discrete = 10)`.
42-
#' @param show_residuals Logical, if `TRUE`, display residuals of the model
43-
#' as a background to the model-based estimation. Residuals will be computed
44-
#' for the predictors in the data grid, using [`residualize_over_grid()`].
45-
#' @param collapse_group For mixed effects models, name of the grouping variable
46-
#' of random effects. If `collapse_group = TRUE`, data points "collapsed" by the
47-
#' first random effect groups are added to the plot. Else, if `collapse_group`
48-
#' is a name of a group factor, data is collapsed by that specific random
49-
#' effect. See [`collapse_by_group()`] for further details.
44+
#' @param show_residuals Logical, if `TRUE`, display residuals of the model as a
45+
#' background to the model-based estimation. Residuals will be computed for the
46+
#' predictors in the data grid, using [`residualize_over_grid()`]. For mixed
47+
#' models, you can additional use the `collapse_group` argument to "collapse"
48+
#' data points from residuals by random effects grouping factors.
49+
#' @param collapse_group This argument only takes effect when either `show_data`
50+
#' or `show_residuals` is `TRUE`. For mixed effects models, name of the grouping
51+
#' variable of random effects. If `collapse_group = TRUE`, data points
52+
#' "collapsed" by the first random effect groups are added to the plot. Else, if
53+
#' `collapse_group` is a name of a group factor, data is collapsed by that
54+
#' specific random effect. See [`collapse_by_group()`] for further details.
5055
#' @param point,line,pointrange,ribbon,facet,grid Additional
5156
#' aesthetics and parameters for the geoms (see customization example).
5257
#' @param ... Arguments passed from `plot()` to `visualisation_recipe()`, or

R/visualisation_recipe_internal.R

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@
322322
}
323323

324324
# add raw data as first layer ----------------------------------
325-
if (show_data) {
325+
if (show_data && is.null(collapse_by)) {
326326
layers[[paste0("l", l)]] <- .visualization_recipe_rawdata(x, aes, numeric_as_discrete)
327327
# Update with additional args
328328
if (!is.null(point)) {
@@ -332,7 +332,7 @@
332332
}
333333

334334
# add residual data as next lowest layer
335-
if (show_residuals) {
335+
if (show_residuals && is.null(collapse_by)) {
336336
layers[[paste0("l", l)]] <- .visualization_recipe_residuals(
337337
x,
338338
aes,
@@ -346,12 +346,13 @@
346346
}
347347

348348
# next possible data layer is collapsing by group ---------------
349-
if (!is.null(collapse_by)) {
349+
if (!is.null(collapse_by) && (show_data || show_residuals)) {
350350
layers[[paste0("l", l)]] <- .visualization_recipe_collapsed_data(
351351
x,
352352
aes,
353353
collapse_by,
354-
numeric_as_discrete
354+
numeric_as_discrete,
355+
show_residuals
355356
)
356357
# Update with additional args
357358
if (!is.null(point)) {
@@ -597,17 +598,16 @@
597598
x,
598599
aes,
599600
collapse_by,
600-
numeric_as_discrete = 8
601+
numeric_as_discrete = 8,
602+
show_residuals = FALSE
601603
) {
602604
model <- attributes(x)$model
603-
grid <- attributes(x)$datagrid
604-
605-
rawdata <- collapse_by_group(grid, model, collapse_by)
605+
rawdata <- collapse_by_group(x, model, collapse_by, residuals = show_residuals)
606606

607607
# Add response to data if not there
608-
y <- insight::find_response(attributes(x)$model)
608+
y <- insight::find_response(model)
609609
if (!y %in% names(rawdata)) {
610-
rawdata[y] <- insight::get_response(attributes(x)$model, verbose = FALSE)
610+
rawdata[y] <- insight::get_response(model, verbose = FALSE)
611611
}
612612

613613
# if we have less than 8 values for the legend, a continuous color scale
@@ -626,13 +626,20 @@
626626
)
627627
}
628628

629-
.data_point_geom(model = model, aes = aes, data = rawdata, y = y)
629+
.data_point_geom(
630+
model = model,
631+
aes = aes,
632+
data = rawdata,
633+
y = y,
634+
size = 2.5,
635+
jitter = 0.1
636+
)
630637
}
631638

632639

633640
# helpers -----------------------------------------------------------------
634641

635-
.data_point_geom <- function(model, aes, data, y) {
642+
.data_point_geom <- function(model, aes, data, y, ...) {
636643
if (aes$type == "pointrange" && !is.numeric(data[[aes$x]])) {
637644
geom <- "jitter"
638645
} else {
@@ -656,6 +663,15 @@
656663
stroke = stroke
657664
)
658665

666+
dots <- list(...)
667+
if (!is.null(dots$size)) {
668+
out$size <- dots$size
669+
}
670+
if (!is.null(dots$jitter)) {
671+
out$position <- "jitter"
672+
out$width <- dots$jitter
673+
}
674+
659675
# check if we have matching columns in the raw data - some functions,
660676
# likes slopes, have mapped these aes to other columns that are not part
661677
# of the raw data - we set them to NULL

man/visualisation_recipe.estimate_predicted.Rd

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

0 commit comments

Comments
 (0)