Skip to content

Commit 1e4b828

Browse files
authored
make scree plot default for non-models in plot.check_outliers (#411)
make scree plot default for non-models, styler, fix failing tests (follow-up on #407) ``` r library(see) library(performance) x <- check_outliers(mtcars, method = "mahalanobis") plot(x) #> $threshold_outliers #> integer(0) #> #> $threshold #> [1] 31.26413 #> #> $elbow_outliers #> [1] 27 31 29 9 #> #> $elbow_threshold #> [1] 3.401786 ``` ![](https://i.imgur.com/QSnXBJZ.png)<!-- --> <sup>Created on 2025-09-01 with [reprex v2.1.1](https://reprex.tidyverse.org)</sup>
2 parents 4866de2 + 97d7cbd commit 1e4b828

11 files changed

Lines changed: 71 additions & 39 deletions

R/plot.check_outliers.R

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#' the outliers' distance values.
1616
#'
1717
#' `type = "dots"` is only used for outlier plots of fitted models; for
18-
#' outlier plots of raw data values, `type` must be either `"scree"` or `"bars"`.
18+
#' outlier plots of raw data values, `type` should be one of the other options.
1919
#' @param show_labels Logical. If `TRUE`, text labels are displayed.
2020
#' @param size_text Numeric value specifying size of text labels.
2121
#' @param rescale_distance Logical. If `TRUE`, distance values are rescaled
@@ -56,7 +56,7 @@
5656
#' )
5757
#' model <- lm(disp ~ mpg + hp, data = mt2)
5858
#' plot(check_outliers(model))
59-
#' plot(check_outliers(mt2$mpg, method = "zscore"), type = "bar")
59+
#' plot(check_outliers(mt2$mpg, method = "zscore"), type = "bars")
6060
#' @examplesIf require("ggrepel")
6161
#' plot(check_outliers(mt2[-3], method = "mahalanobis", ID = "ID"))
6262
#' @export
@@ -76,25 +76,38 @@ plot.see_check_outliers <- function(
7676
verbose = TRUE,
7777
...
7878
) {
79-
type <- insight::validate_argument(type, c("dots", "scree", "count", "bars"))
80-
influential_obs <- attributes(x)$influential_obs
79+
# need to know the method first, because we change the default plot type
80+
# depending on the method
8181
outlier_methods <- attr(x, "method", exact = TRUE)
8282

83+
# validate that the method is correct
8384
if (length(outlier_methods) == 0) {
8485
insight::format_error(
8586
"Invalid outlier method detected. Please ensure `check_outliers()` was called with valid parameters."
8687
)
8788
} else if (
88-
length(outlier_methods) == 2 && all(outlier_methods == c("optics", "optics_xi"))
89+
length(outlier_methods) == 2 &&
90+
all(outlier_methods == c("optics", "optics_xi"))
8991
) {
9092
outlier_methods <- outlier_methods[[1]]
9193
}
9294

93-
if (
94-
type == "dots" &&
95-
!is.null(influential_obs) &&
96-
(is.null(outlier_methods) || length(outlier_methods) == 1)
97-
) {
95+
# set default plot type depending on the method
96+
if (missing(type) && !isTRUE(type == "dots")) {
97+
type <- "scree"
98+
}
99+
100+
# if ((missing(type) || is.null(type))) {
101+
# type <- "scree"
102+
# }
103+
104+
# validate arguments
105+
type <- insight::validate_argument(type, c("dots", "scree", "count", "bars"))
106+
influential_obs <- attributes(x)$influential_obs
107+
108+
if (length(outlier_methods) > 1 || type == "bars") {
109+
.plot_outliers_multimethod(x, rescale_distance = rescale_distance)
110+
} else if (type == "dots" && !is.null(influential_obs)) {
98111
.plot_diag_outliers_dots(
99112
influential_obs,
100113
show_labels = show_labels,
@@ -106,23 +119,22 @@ plot.see_check_outliers <- function(
106119
alpha_dot = alpha_dot,
107120
colors = colors
108121
)
109-
} else if (type == "scree" && length(outlier_methods) == 1) {
122+
} else if (type == "count") {
123+
.plot_diag_outliers_dots_count(
124+
x,
125+
show_labels = show_labels,
126+
size_text = size_text,
127+
rescale_distance = rescale_distance,
128+
... # to change bins because of warning
129+
)
130+
} else {
110131
.plot_scree(
111132
x,
112133
rescale_distance = rescale_distance,
113134
elbow_threshold = elbow_threshold,
114135
verbose = verbose,
115136
...
116137
)
117-
} else if (type == "count" && length(outlier_methods) == 1) {
118-
.plot_diag_outliers_dots_count(
119-
x,
120-
show_labels = show_labels,
121-
size_text = size_text,
122-
rescale_distance = rescale_distance
123-
)
124-
} else {
125-
.plot_outliers_multimethod(x, rescale_distance = rescale_distance)
126138
}
127139
}
128140

man/plot.see_check_outliers.Rd

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

man/see-package.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/plot.check_outliers/plot-see-check-outliers-multimethods.svg renamed to tests/testthat/_snaps/windows/plot.check_outliers/plot-see-check-outliers-multimethods.svg

File renamed without changes.

tests/testthat/_snaps/plot.check_outliers/plot-see-check-outliers-works-bars-method.svg renamed to tests/testthat/_snaps/windows/plot.check_outliers/plot-see-check-outliers-works-bars-method.svg

File renamed without changes.

tests/testthat/_snaps/plot.check_outliers/plot-see-check-outliers-works-bars-rescaled.svg renamed to tests/testthat/_snaps/windows/plot.check_outliers/plot-see-check-outliers-works-bars-rescaled.svg

File renamed without changes.

tests/testthat/_snaps/plot.check_outliers/plot-see-check-outliers-works-count-method.svg renamed to tests/testthat/_snaps/windows/plot.check_outliers/plot-see-check-outliers-works-count-method.svg

Lines changed: 2 additions & 2 deletions
Loading

tests/testthat/_snaps/plot.check_outliers/plot-see-check-outliers-works-default-method.svg renamed to tests/testthat/_snaps/windows/plot.check_outliers/plot-see-check-outliers-works-default-method.svg

File renamed without changes.

tests/testthat/_snaps/plot.check_outliers/plot-see-check-outliers-works-scree-method-z-score.svg renamed to tests/testthat/_snaps/windows/plot.check_outliers/plot-see-check-outliers-works-scree-method-z-score.svg

File renamed without changes.

tests/testthat/_snaps/plot.check_outliers/plot-see-check-outliers-works-scree-method.svg renamed to tests/testthat/_snaps/windows/plot.check_outliers/plot-see-check-outliers-works-scree-method.svg

File renamed without changes.

0 commit comments

Comments
 (0)