Histogram

Introduction

Agenda


  • build histogram
  • specify bins
  • modify
    • color
    • fill
    • alpha
    • bin width
    • line type
    • line size
  • map aesthetics to variables

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)

Plot

Introduction


Example


ggplot(ecom) +
  geom_histogram(aes(n_visit))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Instructions


ggplot(ecom) +
  geom_histogram(aes(n_visit))
ggplot(ecom) +
  geom_histogram(aes(n_visit))

Specify Bins

Introduction


Example


ggplot(ecom) +
  geom_histogram(aes(n_visit), bins = 7)

Instructions


ggplot(ecom) +
  geom_histogram(aes(n_visit), bins = 7)
ggplot(ecom) +
  geom_histogram(aes(n_visit), bins = 7)

Fill - Part 1

Introduction


Example


ggplot(ecom) +
  geom_histogram(aes(n_visit), bins = 7, fill = 'blue')

Instructions


ggplot(ecom) +
  geom_histogram(aes(n_visit), bins = 7, fill = 'blue')
ggplot(ecom) +
  geom_histogram(aes(n_visit), bins = 7, fill = 'blue')

Fill - Part 2

Introduction


Example


ggplot(ecom) +
  geom_histogram(aes(n_visit, fill = device), bins = 7)

Instructions


ggplot(ecom) +
  geom_histogram(aes(n_visit, fill = device), bins = 7)
ggplot(ecom) +
  geom_histogram(aes(n_visit, fill = device), bins = 7)

Color

Introduction


Example


ggplot(ecom) +
  geom_histogram(aes(n_visit), bins = 7, fill = 'white', color = 'blue')

Instructions


ggplot(ecom) +
  geom_histogram(aes(n_visit), bins = 7, fill = 'white', color = 'blue')
ggplot(ecom) +
  geom_histogram(aes(n_visit), bins = 7, fill = 'white', color = 'blue')

Bin Width

Introduction


Example


ggplot(ecom) +
  geom_histogram(aes(n_visit), binwidth = 2, fill = 'blue', color = 'black')

Instructions


ggplot(ecom) +
  geom_histogram(aes(n_visit), binwidth = 2, fill = 'blue', color = 'black')
ggplot(ecom) +
  geom_histogram(aes(n_visit), binwidth = 2, fill = 'blue', color = 'black')