Powered by AppSignal & Oban Pro

Transparency scales: scale_alpha

notebooks/scale_alpha.livemd

Transparency scales: scale_alpha

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 scale_alpha(), which covers scale_alpha_continuous(), scale_alpha_binned(), scale_alpha_discrete() and scale_alpha_ordinal() too, 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.

An alpha scale maps a column to transparency, a way to fade the less important observations. scale_alpha() is short for scale_alpha_continuous(), for a column of numbers; scale_alpha_binned() cuts the numbers into bins first, and scale_alpha_discrete() and scale_alpha_ordinal() are for discrete and ordered values. Giggly has Giggly.Plot.scale_alpha_continuous/2 and Giggly.Plot.scale_alpha_discrete/2, with their range:, but not ggplot2's short name, scale_alpha(), nor binned or ordered alpha scales (scale_alpha_binned(), scale_alpha_ordinal()), and by default maps alpha to a column as ggplot2's default scale does, a column of numbers onto 0.1 to 1 and one of text spread from 0.1 to 1, and draws the legend that reads it back; a layer can also set it for every mark, as Giggly.Plot.geom_point(alpha: 0.4) does. 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.

Mapping transparency

p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(alpha = year))

# The default range of 0.1-1.0 leaves all data visible
p

Every car in mpg is from 1999 or 2008, so its points are drawn at the range's two ends, faint and opaque; the legend, titled year, has a key for each of 2000 to 2008 by 2, each as opaque as that year would be.

plot =
  mpg
  |> Plot.new(%{x: :displ, y: :hwy})
  |> Plot.geom_point(%{alpha: :year})

render.(plot)

Invisible points

# Include 0 in the range to make data invisible
p + scale_alpha(range = c(0, 1))

scale_alpha() is Giggly.Plot.scale_alpha_continuous/2. The cars from 1999 are drawn fully transparent, so only those from 2008 show.

plot
|> Plot.scale_alpha_continuous(range: {0, 1})
|> render.()

Titling the legend

# Changing the title
p + scale_alpha("cylinders")

R's first argument is the scale's name, which titles its legend; Giggly's is name:.

plot
|> Plot.scale_alpha_continuous(name: "cylinders")
|> render.()