Facets

Introduction

Data

Introduction


ecom

Data Dictionary


  • id: row id
  • referrer: referrer website/search engine
  • os: operating system
  • browser: browser
  • device: device used to visit the website
  • n_pages: number of pages visited
  • duration: time spent on the website (in seconds)
  • repeat: frequency of visits
  • country: country of origin
  • purchase: whether visitor purchased
  • order_value: order value of visitor (in dollars)

Grid

Introduction


Generate subplots that each display one subset of the data using

  • facet_grid()
  • facet_wrap()

Grid - Columns

Introduction


Example


ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() +
  facet_grid(. ~ device)

Instructions


ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() +
  facet_grid(. ~ device)
ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() +
  facet_grid(. ~ device)

Grid - Rows

Introduction


Example


ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() +
  facet_grid(device ~ .)

Instructions


ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() +
  facet_grid(device ~ .)
ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() +
  facet_grid(device ~ .)

Grid - Rows & Columns

Introduction


Example


ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() +
  facet_grid(purchase ~ device)

Instructions


ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() +
  facet_grid(purchase ~ device)
ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() +
  facet_grid(purchase ~ device)

Grid - Rows & Columns

Introduction


Example


ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() +
  facet_grid(device ~ purchase)

Instructions


ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() +
  facet_grid(device ~ purchase)
ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() +
  facet_grid(device ~ purchase)

Grid - Scales

Introduction


Example


ggplot(ecom, aes(n_pages, duration, color = factor(device))) +
  geom_point() + 
  facet_grid(. ~ device, scales = "free")

Instructions


Different range instead of fixed range.

ggplot(ecom, aes(n_pages, duration, color = factor(device))) +
  geom_point() + 
  facet_grid(. ~ device, scales = "free")
ggplot(ecom, aes(n_pages, duration, color = factor(device))) +
  geom_point() + 
  facet_grid(. ~ device, scales = "free")

Grid - Switch Axis

Introduction


Example


ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() + 
  facet_grid(device ~ purchase, switch = "both") 

Instructions


ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() + 
  facet_grid(device ~ purchase, switch = "both") 
ggplot(ecom, aes(n_pages, duration)) + 
  geom_point() + 
  facet_grid(device ~ purchase, switch = "both") 

Wrap

Introduction


ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(~device)

Wrap - Specify Rows

Introduction


Example


ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(~device, nrow = 2)

Instructions


ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(~device, nrow = 2)
ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(~device, nrow = 3)

Wrap - Rows & Columns

Introduction


Example


ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(~device + purchase)

Instructions


ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(~device + purchase)
ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(~device + purchase)

Wrap - Rows & Columns

Introduction


Example


ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(c("device", "purchase"))

Instructions


ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(c("device", "purchase"))
ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(c("device", "purchase"))

Wrap - Scales

Introduction


Example


ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(~device, scales = "free")

Instructions


ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(~device, scales = "free")
ggplot(ecom, aes(n_pages, duration)) +
  geom_point() +
  facet_wrap(~device, scales = "free")