Powered by AppSignal & Oban Pro

Identity scales: scale_identity

notebooks/scale_identity.livemd

Identity scales: scale_identity

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

alias Giggly.Plot

mtcars = Giggly.Datasets.mtcars()

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_colour_identity(), which covers scale_fill_identity(), scale_shape_identity(), scale_linetype_identity(), scale_linewidth_identity(), scale_alpha_identity(), scale_size_identity(), scale_discrete_identity() and scale_continuous_identity() 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 identity scale is for data that already holds the aesthetic's values: a column of colour names is drawn in those colours, a column of numbers as those sizes, with no scaling between. It draws no legend unless it is also given breaks, labels and a guide. Each function works on the aesthetic in its name, though scale_colour_identity() and scale_fill_identity() can serve colour and fill at once through their aesthetics argument; scale_discrete_identity() and scale_continuous_identity() work on whatever aesthetics that argument names. Giggly has no identity scales yet, so every column it maps to an aesthetic goes through a scale that maps it. 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 one example it draws uses Giggly.Datasets.mtcars/0, R's mtcars: 32 cars from the 1974 Motor Trend US magazine, which the setup cell binds to mtcars, R's name for it.

Colours named in the data

luv_colours holds R's named colours with their places in the CIE Luv colour space; its col column names each colour, and each point is drawn in the colour it names.

ggplot(luv_colours, aes(u, v)) +
  geom_point(aes(colour = col), size = 3) +
  scale_color_identity() +
  coord_fixed()

Not in Giggly yet: the luv_colours dataset, scale_colour_identity() and coord_fixed().

Fill colours named in the data

Without scale_fill_identity(), the colour names are values like any others, each given a colour of the default scale's choosing and a key in the legend; with it, each tile is drawn in the colour it names.

df <- data.frame(
  x = 1:4,
  y = 1:4,
  colour = c("red", "green", "blue", "yellow")
)
ggplot(df, aes(x, y)) + geom_tile(aes(fill = colour))
ggplot(df, aes(x, y)) +
  geom_tile(aes(fill = colour)) +
  scale_fill_identity()

Not in Giggly yet: tiles (geom_tile()) and scale_fill_identity().

A legend for an identity scale

# To get a legend guide, specify guide = "legend"
ggplot(df, aes(x, y)) +
  geom_tile(aes(fill = colour)) +
  scale_fill_identity(guide = "legend")
# But you'll typically also need to supply breaks and labels:
ggplot(df, aes(x, y)) +
  geom_tile(aes(fill = colour)) +
  scale_fill_identity("trt", labels = letters[1:4], breaks = df$colour,
  guide = "legend")

Not in Giggly yet: geom_tile(), scale_fill_identity(), whose first argument titles the legend, and its breaks, labels and guide options, and the legend it asks for.

Sizes from the data

Through ggplot2's default size scale, cyl's values 4, 6 and 8 become point areas within a fixed range; through scale_size_identity(), they are the points' sizes as they stand.

# cyl scaled to appropriate size
ggplot(mtcars, aes(mpg, wt)) +
  geom_point(aes(size = cyl))

# cyl used as point size
ggplot(mtcars, aes(mpg, wt)) +
  geom_point(aes(size = cyl)) +
  scale_size_identity()

Giggly draws the first.

mtcars
|> Plot.new(%{x: :mpg, y: :wt})
|> Plot.geom_point(%{size: :cyl})
|> render.()

Not in Giggly yet: scale_size_identity().