The Grammar of Graphics

David Selby

7 June 2016

Outline

  1. Types of charts

  2. The Grammar of Graphics

  3. Examples

Types of charts

The Grammar of Graphics

A book what I readed

Wilkinson (2005) Figure 2.2

“The grammar of graphics determines how algebra, geometry, aesthetics, statistics, scales and coordinates interact”

Variables

Operations applied before scale transformations

  • Mathematical transforms (sin, log)
  • Sorting and ranking
  • Aggregation
  • Quantiles
  • Residuals

Algebra

Let \(A := (x,y,z)\) and \(B := (a,a,b)\)

  • Cross, \(A\times B\) \[ = ([x,y],~[y,a],~[z,b]) \]

  • Nest, \(A/B\)
    \[ = (x|a,~y|a,~z|b) \]

  • Blend, \(A+B\) \[ = (x,y,z,a,a,b) \]

Scales

  • Categorical
  • Interval
  • Time
  • One-bend (log, power)
  • Two-bend (arcsine, logit, probit)
  • Probability

“Graphics do not care about the scales on which they are drawn”

Scales

Statistics

After scale & variable transformations
Before coordinate transformations

  • Regression coefficients
  • Smoothers
  • Binning and aggregation
  • Confidence intervals
  • Density estimation

Geometry

Things we can actually see

Functions Partitions Networks
point
line
area
interval
path
schema
contour
polygon
edge

Collision modifiers: stack, dodge, jitter

Coordinates

“Ordinary graphics such as intervals and polygons take on radically different appearances under different planar transformations.”

  • Cartesian coordinates
  • Triangular coordinates
  • Polar coordinates
  • Map projections
  • Conformal mapping

Coordinates

A new graphic? Or a transformation of an existing one?

Aesthetics

Form Surface Motion Sound Text
position
size
shape
rotation
resolution
colour
texture
blur
transparency
direction
speed
acceleration
tone
volume
rhythm
voice
label

“Much of the skill in graphic design is knowing what combinations of attributes to avoid.”

Wilkinson (2005) Figure 10.11

Putting it all together

Let’s make a pie chart

Titanic dataset

head(Titanic)
##   Class    Sex   Age Survived Freq
## 1   1st   Male Child       No    0
## 2   2nd   Male Child       No    0
## 3   3rd   Male Child       No   35
## 4  Crew   Male Child       No    0
## 5   1st Female Child       No    0
## 6   2nd Female Child       No    0

Algebra: Class × Survived × Freq

Let’s make a pie chart

pie <- ggplot(Titanic) +
  scale_x_discrete() + scale_y_continuous() + scale_fill_discrete() +
  geom_bar(width = 1, position = "fill") +
  aes(x = "", fill = Survived, weight = Freq) +
  theme_titanic()

Let’s make a pie chart

pie <- pie + coord_polar(theta = "y")

Let’s make a pie chart

pie <- pie + facet_grid(. ~ Class)

Let’s make a pie chart

Nightingale (1858)

Napoleon’s March

Minard (1869)

Napoleon’s March

gg <- ggplot(troops, aes(x = long, y = lat))
gg <- gg + geom_path(aes(size = survivors,
                         colour = direction,
                         group = group), lineend = "round")
gg <- gg + geom_text(data = cities, aes(label = city),
                     size = 3, family = "serif", fontface = "italic")

Napoleon’s March

Implementations

  • Graphics Production Language (SPSS)

  • ggplot2 / ggvis (R)

  • Bokeh (Python)

  • Gadfly (Julia)

  • D3 / Vega (JavaScript)

  • Grammarphone (Spotify API)

Asciiplot

source('asciiplot.R')
asciiplot(df = iris,
          aes = list(x='Sepal.Width', y='Sepal.Length'),
          geom = 'point')
##                                                     O                     
##                   O   O     O                       O                     
##                             O                                             
##                       O                                                   
##                           O O       O           O                         
##                             O                                             
##                                 O   O                                     
##                       O     O       O                                     
##               O           O O   O     O                                   
##                       O     O       O                                     
##         O     O     O O   O     O   O O O                                 
##       O               O   O             O                                 
##       O           O O O   O O           O                                 
##                             O       O                                     
##               O   O O O   O O                       O     O             O 
##               O     O O   O O                                             
##         O O   O   O         O           O   O     O     O         O       
##                                                   O                       
##               O     O                 O O   O     O O         O           
## O       O O   O             O   O   O O O   O   O                         
##                             O   O       O                                 
##                                     O                                     
##         O                       O   O   O       O                         
##                           O O       O                                     
##                             O

Grammarphone

  • Implements a sound geom for line graphs

  • Maps values in data to frequency/volume of music

  • Awarded 1st prize at Anvil Hack II

  • Definitely useful

  • Try it here

The End

References

  • Wilkinson, Leland. The Grammar of Graphics. 2nd edition. Springer Science & Business Media, 2005.

  • Wickham, Hadley. “A layered grammar of graphics.” Journal of Computational and Graphical Statistics 19.1 (2010): 3–28.

Source code available on Github