R/plot_outliers.R

Defines functions plot_outliers

Documented in plot_outliers

#' @title plot the outliers
#' @description The 'plot_outliers' function below draws a boxplot and a scatterplot of a numeric variable x and plots the values of the outliers (currently not offset, even if they overlap). For relatively small datasets, it can be a quick way to identify which outliers look reasonable and which are likely a result of transcription or measurement error, and thus should be either corrected or discarded.
#' @param x vector
#'
#'
#' @examples
#' plot_outliers(airquality$Wind, col = "darkgreen", main = "wind")
#' [Plot outliers and their values | modTools](https://modtools.wordpress.com/2020/03/13/plot-outliers-and-their-values/)
#' @rdname plot_outliers
#' @export

plot_outliers <- function(x, val_col = "blue", ...) {
    par_in <- par(no.readonly = TRUE)
    par(mfrow = c(1, 2))
    bp <- boxplot(x, ...)
    out <- bp$out
    message(length(out), " outliers detected")
    if (length(out) > 0) text(x = 0.5, y = bp$out, labels = round(out, 2), adj = 0, col = val_col)
    plot(x, pch = 20)
    if (length(out) > 0) text(x = 0.5, y = bp$out, labels = round(out, 2), adj = 0, col = val_col)
    par(par_in)
}
jixing475/manuscriptsJX documentation built on April 20, 2020, 9:55 a.m.