Agenda


Learn to quickly build a set of plots that are routinely used to explore data:

  • Scatter Plot
  • Bar Plot
  • Box Plot
  • Line Chart
  • Histogram

Libraries


library(ggplot2)
library(dplyr)

qplot


Shortcut designed for those familiar with base plots. You can quickly produce a number of different types of plots. Below are the key arguments:

  • x : data for X axis
  • y : data for Y axis
  • geom : symbols to represent data
  • data : a data frame or a tibble

Scatter Plot


qplot(disp, mpg, data = mtcars)

Scatter Plot


qplot(disp, mpg, data = mtcars, geom = c('point', 'line'))

Scatter Plot


qplot(disp, mpg, data = mtcars, color = factor(cyl))

Scatter Plot


qplot(disp, mpg, data = mtcars, shape = factor(cyl))

Scatter Plot


qplot(disp, mpg, data = mtcars, size = qsec)

Bar Plot


qplot(factor(cyl), data = mtcars, geom = c('bar'))

Bar Plot


qplot(factor(cyl), data = mtcars, geom = c('bar'), fill = factor(am))

Box Plot


qplot(factor(cyl), mpg, data = mtcars, geom = c('boxplot'))

Box Plot


qplot(factor(1), mpg, data = mtcars, geom = c('boxplot'))

Box Plot


qplot(factor(cyl), mpg, data = mtcars, geom = c('boxplot', 'jitter'))

Line Chart


qplot(x = date, y = unemploy, data = economics, geom = c('line'))

Line Chart


qplot(x = date, y = unemploy, data = economics, geom = c('line'),
      color = 'red')

Histogram


qplot(mpg, data = mtcars, bins = 5)