Description Usage Arguments Value Examples
View source: R/crossplot_outliers.R
This function is useful for getting outliers and labeling outlying
points on a plot. Based a data.frame
or ggplot
and
variables from the user, crossplot_outliers
returns the
corresponding outliers. The user can input either the number of
desired outliers, num.outliers
, or the percentile
corresponding to the outliers percentile.outliers
, but not
both. Note: NA
values will be replaced with the mean value.
1 2 | crossplot_outliers(x, vars = NULL, num.outliers = NULL,
percentile.outliers = NULL)
|
x |
a |
vars |
the variables for which the outliers will be
calculated. If |
num.outliers |
the number of outliers for each variable in
|
percentile.outliers |
the percentile used to measure outliers
for each variable in |
a data.frame
with the plot data for the outliers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | data(mtcars)
##Get the outliers for hp and and mpg from mtcars
crossplot_outliers(mtcars,vars = c("hp","mpg"), percentile.outliers = 0.05)
crossplot_outliers(mtcars,vars = c("hp","mpg"), num.outliers = 2)
##Outliers from a ggplot
p <- ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point(aes(size = wt))
crossplot_outliers(p, vars = c("hp", "mpg"), num.outliers = 2)
crossplot_outliers(p, vars = c("mpg", "hp", "wt"), percentile.outliers = .05)
## -- Full Example using crossplot() and labels -- ##
mtcars$name <- rownames(mtcars)
mtcars.outliers <- crossplot_outliers(mtcars, vars = c("mpg", "hp"), num.outliers = 2)
p <- crossplot(mtcars, x.var = "mpg", y.var = "hp", size.var = "wt",
shapes.var = "cyl", label.var = "name", points.alpha = 0.7) +
geom_text(data = mtcars.outliers)
print(p)
##If there are missing values in vars, these will be replaced by the
##mean so they are less likely to affect the outliers
data(mtcars)
mtcars$mpg[7:12] <- rep(NA, 6)
##Get the outliers for hp and and mpg from mtcars
crossplot_outliers(mtcars,vars = c("hp","mpg"), percentile.outliers = 0.05)
crossplot_outliers(mtcars,vars = c("hp","mpg"), num.outliers = 2)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.