Lesson 3 of 4

Bar & Distribution Charts

It is Saturday and Maya the bakery owner has logged every order, 30 of them, into a little table: the headline item the customer came for, what they spent, and whether it was the morning rush or a quiet afternoon. Two questions are nagging her. Which pastry is pulling its weight? and what does a typical order actually look like? A scatter or a line, the charts from Lesson 2, answer neither. These questions need bars and distributions.

In Lesson 2 you used geom_point() for a relationship and geom_line() for a value moving through time. Now you meet the charts that compare categories and reveal the shape of a single column of numbers.

By the end of this lesson you will be able to:

  • Use geom_col() to compare an amount across categories, and geom_bar() to compare counts, and say exactly which one does the counting
  • Draw a histogram with geom_histogram() to see how one continuous variable is spread, and choose a sensible bin width
  • Read and draw a boxplot with geom_boxplot() to compare a distribution across groups
  • Match a question to the chart that answers it

Prerequisites: Lesson 1, the grammar of graphics (data, aes(), geoms, layering with +) and Lesson 2, scatter and line charts. Every new term is defined as it appears.

The workhorse for categories

Bars compare categories

A bar chart answers one question: how do categories compare on some number? Each category gets its own bar, and the bar's height is the number. Maya's pastries are the categories; the dollars each one earned is the number.

Start, as always, by building Maya's Saturday right here. Each lesson runs in a fresh R session, so the data lives on this page (run this once):

RInteractive R
library(ggplot2) orders <- data.frame( pastry = c("Coffee","Coffee","Croissant","Coffee","Muffin", "Croissant","Coffee","Sourdough","Croissant","Muffin", "Coffee","Sourdough","Croissant","Coffee","Muffin", "Coffee","Coffee","Croissant","Coffee","Muffin", "Coffee","Croissant","Coffee","Muffin","Coffee", "Sourdough","Croissant","Coffee","Muffin","Coffee"), order_value = c(5,4,8,6,9, 11,5,24,12,10, 7,30,9,6,13, 4,5,7,4,8, 6,9,5,7,4, 22,8,5,9,6), # dollars per order daypart = c(rep("Morning",15), rep("Afternoon",15)) ) head(orders)

  

Maya already knows her per-pastry revenue is worth plotting, so first she totals it up, one row per pastry, with the dplyr verbs from earlier in the course:

RInteractive R
library(dplyr) by_pastry <- orders %>% group_by(pastry) %>% summarise(orders = n(), revenue = sum(order_value)) by_pastry #> # A tibble: 4 x 3 #> pastry orders revenue #> <chr> <int> <dbl> #> 1 Coffee 14 72 #> 2 Croissant 7 64 #> 3 Muffin 6 56 #> 4 Sourdough 3 76

  

Now revenue is an amount she already has, one value per category. The geom that draws a bar at a height you supply is geom_col() ("col" for column): map pastry to x and revenue to y, and it does no arithmetic, it just draws your numbers.

RInteractive R
ggplot(by_pastry, aes(x = pastry, y = revenue)) + geom_col()

  

The bars rank the pastries by money. Sourdough wins, on only 3 orders, while Coffee, ordered far more often, comes second. Hold on to that surprise.