16 February 2017

The Grammar of Graphics

The grammar of graphics determines how

  1. variables
  2. algebra
  3. scales
  4. statistics
  5. geometry
  6. coordinates
  7. aesthetics

interact.

How to use ggplot2

Select variables (load the data)

ggplot(iris) +

Map aesthetics (x, y, colour, fill, size, …)

  aes(x = Petal.Width, y = Petal.Length, colour = Species) +

Set scales for your aesthetics (continuous, discrete, log, sqrt, …)

  scale_x_continuous() + scale_y_continuous() +
  scale_colour_discrete() +

Choose geometries and/or statistics

  geom_point() + stat_density_2d() +

Pick a coordinate system and transformations

  coord_cartesian()

Introducing ggChernoff…

The ggChernoff package

Install and load the package

devtools::install_github('Selbosh/ggChernoff')
library(ggplot2)
library(ggChernoff)

Create a simple scatter plot of faces

p <- ggplot(iris) +
  aes(x = Petal.Width, y = Petal.Length, fill = Species) +
  geom_chernoff()

Map to the new smile aesthetic

p + aes(smile = Sepal.Width)

Further information

References

  • Leland Wilkinson (2006). The Grammar of Graphics (2nd edition). Springer.
  • Hadley Wickham (2010). A layered grammar of graphics. Journal of Computational and Graphical Statistics, 19(1), 3–28.
  • Hadley Wickham (2016). ggplot2: Elegant Graphics for Data Analysis (2nd edition). Springer.

R packages