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
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
0 commit comments