Scatter Plot

Introduction

Agenda


  • build scatter plots
  • modify point
    • color
    • fill
    • alpha
    • shape
    • size
  • fit regression line

Plot

Introduction


Example


ggplot(mtcars) +
  geom_point(aes(disp, mpg))

Instruction


  • map x to disp
  • map y to mpg
# scatter plot of disp and mpg
ggplot(mtcars) +
  geom_point(aes(disp, mpg))

Aesthetics

Introduction


Now let us modify the aesthetics of the points. There are two ways:

  • map them to variables using the aes() function
  • specify values

Color

Introduction


Example


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = factor(cyl)))

Instructions


  • map color to cyl
# map color to cyl
ggplot(mtcars) +
  geom_point(aes(disp, mpg, _____)

Example


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = hp))

Instructions


  • map color to hp
# map color to cyl
ggplot(mtcars) +
  geom_point(aes(disp, mpg, _____)

Example


ggplot(mtcars) +
  geom_point(aes(disp, mpg), color = 'blue')

Instructions


  • set color to 'blue'
# set color to 'blue'
ggplot(mtcars) +
  geom_point(aes(disp, mpg), _____)

Example


ggplot(mtcars) +
  geom_point(aes(disp, mpg), color = 'blue', alpha = 0.4)

Instructions


  • set color to 'blue' and alpha to 0.4
# set color to 'blue' and alpha to 0.4
ggplot(mtcars) +
  geom_point(aes(disp, mpg), _____, _____)
ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = factor(cyl)))
ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = hp))
ggplot(mtcars) +
  geom_point(aes(disp, mpg), color = 'blue')
ggplot(mtcars) +
  geom_point(aes(disp, mpg), color = 'blue', alpha = 0.4)

Shape


Introduction


Example


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl)))

Instructions


  • map shape to cyl
# set shape to 3
ggplot(mtcars) +
  geom_point(aes(disp, mpg), ____)

Example


ggplot(mtcars) +
  geom_point(aes(disp, mpg), shape = 3)

Instructions


  • set shape to 3
# set shape to 3
ggplot(mtcars) +
  geom_point(aes(disp, mpg), ____)
# map shape to cyl
ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl)))
# set shape to 3
ggplot(mtcars) +
  geom_point(aes(disp, mpg), shape = 3)

Size

Introduction


Example


ggplot(mtcars) +
  geom_point(aes(disp, mpg, size = hp))

Instructions


  • map size to hp
ggplot(mtcars) +
  geom_point(aes(disp, mpg, ____))

Example


ggplot(mtcars) +
  geom_point(aes(disp, mpg), size = 3)

Instructions


  • set size to 3
ggplot(mtcars) +
  geom_point(aes(disp, mpg), ____)
ggplot(mtcars) +
  geom_point(aes(disp, mpg, size = hp))
ggplot(mtcars) +
  geom_point(aes(disp, mpg), size = 3)

Fit Line

Introduction


Example


ggplot(mtcars, aes(disp, mpg)) +
  geom_smooth(method = 'lm', se = FALSE)

Example


ggplot(mtcars, aes(disp, mpg)) +
  geom_smooth(method = 'lm', se = TRUE)

Example


ggplot(mtcars, aes(disp, mpg)) +
  geom_smooth(method = 'loess', se = FALSE)

Example


ggplot(mtcars, aes(disp, mpg)) +
  geom_abline(slope = 29.59985, intercept = -0.04122)

Fit Line - Practice


Instructions


ggplot(mtcars, aes(disp, mpg)) +
  geom_smooth(_____, _____)

Instructions


ggplot(mtcars, aes(disp, mpg)) +
  geom_smooth(_____, _____)

Instructions


ggplot(mtcars, aes(disp, mpg)) +
  geom_smooth(_____, _____)

Instructions


ggplot(mtcars, aes(disp, mpg)) +
  geom_abline(_____, _____)
ggplot(mtcars, aes(disp, mpg)) +
  geom_smooth(method = 'lm', se = FALSE)
ggplot(mtcars, aes(disp, mpg)) +
  geom_smooth(method = 'lm', se = TRUE)
ggplot(mtcars, aes(disp, mpg)) +
  geom_smooth(method = 'loess', se = FALSE)
ggplot(mtcars, aes(disp, mpg)) +
  geom_abline(slope = 29.59985, intercept = -0.04122)