crossplot_outliers: To get the outlier data form a ggplot

Description Usage Arguments Value Examples

View source: R/crossplot_outliers.R

Description

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.

Usage

1
2
crossplot_outliers(x, vars = NULL, num.outliers = NULL,
  percentile.outliers = NULL)

Arguments

x

a ggplot2 plot or a data.frame. Can be generated from crossplot.

vars

the variables for which the outliers will be calculated. If x is a ggplot2 object, use the variable names in the underlying data

num.outliers

the number of outliers for each variable in vars. Defaults to NULL

percentile.outliers

the percentile used to measure outliers for each variable in vars. Defaults to NULL

Value

a data.frame with the plot data for the outliers

Examples

 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)

ChandlerLutz/crossplotr documentation built on May 6, 2019, 9:56 a.m.