Powered by AppSignal & Oban Pro

Jittered points: geom_jitter

notebooks/geom_jitter.livemd

Jittered points: geom_jitter

Mix.install([
  {:giggly, path: Path.join(__DIR__, "..")},
  {:kino, "~> 0.19"}
  ])

alias Giggly.Plot

mpg = Giggly.Datasets.mpg()

render = fn plot ->
  plot
  |> Giggly.to_svg()
  |> IO.iodata_to_binary()
  |> Kino.Image.new(:svg)
end

About this notebook

This notebook follows the examples on ggplot2's reference page for geom_jitter(), in the order the page gives them. Each example shows ggplot2's code first and then Giggly's. An example Giggly cannot draw yet keeps its ggplot2 code and names what it is waiting for, so the page is also a list of what remains.

geom_jitter() is a shortcut for geom_point(position = "jitter"): it moves each point by a small random amount, so that points which would sit on one another, as discrete values make them do, can be told apart. Giggly has both, Giggly.Plot.geom_jitter/3, with width: and height:, and Giggly.Plot.geom_point/3's position: :jitter, or {:jitter, options} with its options.

Giggly's jitter draws Erlang's random numbers, not R's, so its points land elsewhere than ggplot2's, each within the same distance of its place, and an axis that widens to hold them can end a little elsewhere too, by no more than about twice that distance; everything else in a drawing is ggplot2's. Where ggplot2 draws new numbers each time a plot is drawn unless its position is given a seed, Giggly draws the same points every time, from a seed of 0 unless {:jitter, seed: seed} gives another.

A plot is described with Giggly.Plot and drawn with Giggly.to_svg/2, which returns an SVG document. The setup cell aliases Giggly.Plot, so the cells write Plot.new/2, and defines render, which draws a plot and shows it below the cell with Kino.Image. The data in these examples is Giggly.Datasets.mpg/0, ggplot2's mpg: fuel economy for 234 cars of 38 popular models from 1999 to 2008, which the setup cell binds to mpg, ggplot2's name for it.

Points without jitter

p <- ggplot(mpg, aes(cyl, hwy))
p + geom_point()

Many cars share both a number of cylinders and a highway mileage, so their points are drawn on top of one another.

p = Plot.new(mpg, %{x: :cyl, y: :hwy})

p
|> Plot.geom_point()
|> render.()

Jittered points

p + geom_jitter()

Each point moves up to 0.4 either way, 40% of the closest spacing of the values, which is 1 here, as both columns hold whole numbers; both axes widen to hold the points.

p
|> Plot.geom_jitter()
|> render.()

Mapping colour

# Add aesthetic mappings
p + geom_jitter(aes(colour = class))
p
|> Plot.geom_jitter(%{colour: :class})
|> render.()

Less jitter

# Use smaller width/height to emphasise categories
ggplot(mpg, aes(cyl, hwy)) +
  geom_jitter()
ggplot(mpg, aes(cyl, hwy)) +
  geom_jitter(width = 0.25)

Unless told otherwise, the jitter moves a point up to 40% of the data's resolution either way; a smaller width keeps each number of cylinders in a narrower column.

mpg
|> Plot.new(%{x: :cyl, y: :hwy})
|> Plot.geom_jitter()
|> render.()
mpg
|> Plot.new(%{x: :cyl, y: :hwy})
|> Plot.geom_jitter(width: 0.25)
|> render.()

More jitter

# Use larger width/height to completely smooth away discreteness
ggplot(mpg, aes(cty, hwy)) +
  geom_jitter()
ggplot(mpg, aes(cty, hwy)) +
  geom_jitter(width = 0.5, height = 0.5)

City and highway mileages are whole numbers, so half a mile per gallon either way fills the gaps between them.

mpg
|> Plot.new(%{x: :cty, y: :hwy})
|> Plot.geom_jitter()
|> render.()
mpg
|> Plot.new(%{x: :cty, y: :hwy})
|> Plot.geom_jitter(width: 0.5, height: 0.5)
|> render.()