labelOutliers: Create a ggplot with outliers colored.

Description Usage Arguments Author(s) Examples

Description

Not a fully fleshed out function, but somewhat recreating what plot.lm does in labeling outliers. Labels outliers from a linear model in a plot (e.g. those with a std. resid. > 2) (note that plot.lm defaults to labelling 3 ids, regardless of extremity). Takes a data frame with x, y, and label columns, performs an LM and extracts 'extreme' values and labels them on the plot.

Usage

1

Arguments

df

Author(s)

Greg Ziegler

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
set.seed(1)
x <- rnorm(100)
y <- rnorm(100)
z <- rnorm(100)
df <- data.frame(id=sample(LETTERS[1:5],length(x),replace=TRUE),x,comp=x+y,z)
df$comp[4] <- 20
df$x[5] <- 10
labelOutliers(data.frame(x=df$x,y=df$comp,label=df$id))

function (df) 
{
    require(ggplot2)
    df <- na.omit(df)
    m = lm(df[, "x"] ~ df[, "y"])
    df.fortified = fortify(m)
    df$extreme = ifelse(abs(df.fortified$.stdresid) > 2, 1, 0)
    p <- ggplot(df, aes(x = x, y = y)) + geom_point() + geom_text(data = df[df$extreme == 
        1, ], aes(x = x, y = y, label = label), size = 3, hjust = -0.3) + 
        annotate("text", label = lm_eqn(df), parse = TRUE, x = Inf, 
            y = Inf, hjust = 1.1, vjust = 1.5)
    return(p)
  }

gziegler/ionomicsUtils documentation built on June 20, 2019, 8:04 p.m.