June 29, 2015

Plot 1

library(ggplot2)
p <- qplot(x = wt, y = mpg, data = mtcars)
p + geom_abline()

Plot 2

p + geom_abline(intercept = 20)

Plot 3

coefficients <- coef(lm(formula = mpg ~ wt, data = mtcars))
p + geom_abline(intercept = coefficients[["(Intercept)"]], 
                slope = coefficients[["wt"]])

Plot 4

coefficients <- coef(lm(formula = mpg ~ wt, data = mtcars))
p + geom_abline(intercept = coefficients[["(Intercept)"]], 
                slope = coefficients[["wt"]], color = "red", 
                size = 2)

Plot 5

p + stat_smooth(method = "lm", se = TRUE)

Plot 6

p + stat_smooth(method = "lm", se = FALSE)

Plot 7

p <- ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) + 
    geom_point()
df <- data.frame(a = rnorm(n = 10, mean = 25), 
                 b = rnorm(n = 10, mean = 0))
p + geom_abline(mapping = aes(intercept = a, slope = b), data = df)

Plot 8

p + geom_smooth(mapping = aes(group = cyl), method = "lm")

Plot 9

p + geom_smooth(mapping = aes(group = cyl), method = "lm", 
                fullrange = TRUE)

Plot 10

p + geom_abline(intercept = coefficients[["(Intercept)"]], 
                slope = coefficients[["wt"]]) + coord_flip()

Plot 11

p + geom_abline(intercept = coefficients[["(Intercept)"]], 
                slope = coefficients[["wt"]]) + coord_polar()