Bar Chart

Introduction

Agenda


  • build
    • simple bar plot
    • stacked bar plot
    • grouped bar plot
    • proportional bar plot
  • map aesthetics to variables
  • specify values for
    • bar color
    • bar line color
    • bar line type
    • bar line size

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)

Aesthetics

Introduction


  • fill
  • color
  • linetype
  • size
  • position

Simple Bar Plot

Introduction


Example


ggplot(ecom) +
  geom_bar(aes(factor(device)))

Instructions


ggplot(ecom) +
  geom_bar(aes(factor(device)))
ggplot(ecom) +
  geom_bar(aes(factor(device)))

Bar Color

Introduction


Example


ggplot(ecom) +
  geom_bar(aes(factor(device)),
           fill = c('red', 'blue', 'green'))

Instructions


ggplot(ecom) +
  geom_bar(aes(factor(device)),
           fill = c('red', 'blue', 'green'))
ggplot(ecom) +
  geom_bar(aes(factor(device)),
           fill = c('red', 'blue', 'green'))

Stacked Bar Plot

Introduction


Example


ggplot(ecom) +
  geom_bar(aes(device, fill = factor(referrer)))

Instructions


ggplot(ecom) +
  geom_bar(aes(device, fill = factor(referrer)))
ggplot(ecom) +
  geom_bar(aes(device, fill = factor(referrer)))

Grouped Bar Plot

Introduction


Example


ggplot(ecom) +
  geom_bar(aes(device, fill = factor(referrer)), position = 'dodge')

Instructions


ggplot(ecom) +
  geom_bar(aes(device, fill = factor(referrer)), position = 'dodge')
ggplot(ecom) +
  geom_bar(aes(device, fill = factor(referrer)), position = 'dodge')

Proportional Bar Plot

Introduction


Example


ggplot(ecom) +
  geom_bar(aes(device, fill = factor(referrer)), position = 'fill')

Instructions


ggplot(ecom) +
  geom_bar(aes(device, fill = factor(referrer)), position = 'fill')
ggplot(ecom) +
  geom_bar(aes(device, fill = factor(referrer)), position = 'fill')

Horizontal Bar Plot

Introduction


Example


ggplot(ecom) +
  geom_bar(aes(factor(device), fill = factor(referrer))) +
  coord_flip()

Instructions


ggplot(ecom) +
  geom_bar(aes(factor(device), fill = factor(referrer))) +
  coord_flip()
ggplot(ecom) +
  geom_bar(aes(factor(device), fill = factor(referrer))) +
  coord_flip()

Weight

Introduction


Example


ggplot(ecom, aes(x = factor(device))) +
  geom_bar(aes(weight = order_value))

Instructions


ggplot(ecom, aes(x = factor(device))) +
  geom_bar(aes(weight = order_value))
ggplot(ecom, aes(x = factor(device))) +
  geom_bar(aes(weight = order_value))

Columns

Introduction


Example


device <- c('laptop', 'mobile', 'tablet')
visits <- c(30000, 12000, 5000)
traffic <- tibble::tibble(device, visits)
ggplot(traffic, aes(x = device, y = visits)) +
  geom_col(fill = 'blue') 

Instructions


device <- c('laptop', 'mobile', 'tablet')
visits <- c(30000, 12000, 5000)
traffic <- tibble::tibble(device, visits)
ggplot(traffic, aes(x = device, y = visits)) +
  geom_col(fill = 'blue') 
device <- c('laptop', 'mobile', 'tablet')
visits <- c(30000, 12000, 5000)
traffic <- tibble::tibble(device, visits)
ggplot(traffic, aes(x = device, y = visits)) +
  geom_col(fill = 'blue')