Agenda


  • create scatter plot
  • modify symbol
    • color
    • shape
    • and shape
  • add horizontal & vertical lines
  • fit regression line

Intro


A scatter plot displays the relationship between two continuous variables. In ggplot2, we can build a scatter plot using geom_point(). Scatterplots can show you visually

  • the strength of the relationship between the variables
  • the direction of the relationship between the variables
  • and whether outliers exist

Scatter Plot

plot(mtcars$disp, mtcars$mpg)

Title & Axis Labels


plot(mtcars$disp, mtcars$mpg,
     main = 'Displacement vs Miles Per Gallon',
     xlab = 'Displacement', ylab = 'Miles Per Gallon')

Shape


plot(mtcars$disp, mtcars$mpg, pch = 6)

Shape


Shape


plot(mtcars$disp, mtcars$mpg, pch = nlevels(factor(mtcars$cyl)))

Shape


plot(mtcars$disp, mtcars$mpg, pch = unclass(mtcars$cyl))

Size


plot(mtcars$disp, mtcars$mpg, cex = 1.5)

Size


Color


plot(mtcars$disp, mtcars$mpg, pch = 5, col = 'blue', bg = 'red')

Color


plot(mtcars$disp, mtcars$mpg, pch = 24, col = 'red', bg = 'blue')

Color


plot(mtcars$disp, mtcars$mpg, pch = 5, col = factor(mtcars$cyl))

Horizontal Line


plot(mtcars$disp, mtcars$mpg)
abline(h = 20)

Vertical Line


plot(mtcars$disp, mtcars$mpg)
abline(v = 200)

Regression Line (Intercept & Slope)


plot(mtcars$disp, mtcars$mpg)
abline(a = 29.59, b = -0.0412)

Regression Line (Coefficients)


plot(mtcars$disp, mtcars$mpg)
abline(c(29.59, -0.0412))

Regression Line (Using Model)


plot(mtcars$disp, mtcars$mpg)
model <- lm(mpg ~ disp, data = mtcars)
abline(coef(model))