Powered by AppSignal & Oban Pro

Continuous colour scales: scale_colour_continuous

notebooks/scale_colour_continuous.livemd

Continuous colour scales: scale_colour_continuous

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_colour_continuous(), which covers scale_fill_continuous(), scale_colour_binned() and scale_fill_binned() 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.

scale_colour_continuous() and scale_fill_continuous() are the scales ggplot2 uses by default when a column of numbers is mapped to colour or fill, blending smoothly through a palette; scale_colour_binned() and scale_fill_binned() cut the numbers into bins and give each bin one colour. Their default palette is a setting of the theme. Giggly has Giggly.Plot.scale_color_continuous/2 and Giggly.Plot.scale_fill_continuous/2, along the gradient from #132B43 to #56B1F7 by default or through the colours palette: gives, but no binned scales (scale_colour_binned()), and takes no palette from the theme (palette.colour.continuous). 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.

A palette given to the scale

# A standard plot
p <- ggplot(mpg, aes(displ, hwy, colour = cty)) +
  geom_point()

# You can use the scale to give a palette directly
p + scale_colour_continuous(palette = c("#FEE0D2", "#FC9272", "#DE2D26"))

Given colours, the palette is a gradient through them, from the lowest cty to the highest.

p =
  mpg
  |> Plot.new(%{x: :displ, y: :hwy, colour: :cty})
  |> Plot.geom_point()

p
|> Plot.scale_color_continuous(palette: ["#FEE0D2", "#FC9272", "#DE2D26"])
|> render.()

A palette from the theme

# The default colours are encoded into the theme
p + theme(palette.colour.continuous = c("#DEEBF7", "#9ECAE1", "#3182BD"))

Not in Giggly yet: the theme's palette.colour.continuous setting.

A default for every plot

update_theme() changes the theme every later plot is drawn in, and returns the one it replaced. Giggly has no global settings: every option is an argument.

# You can globally set default colour palette via the theme
old <- update_theme(palette.colour.continuous = c("#E5F5E0", "#A1D99B", "#31A354"))

# Plot now shows new global default
p

Not in Giggly yet: the theme's palette.colour.continuous setting.

Binned colours

# The default binned colour scale uses the continuous palette
p + scale_colour_binned() +
  theme(palette.colour.continuous = c("#EFEDF5", "#BCBDDC", "#756BB1"))

Not in Giggly yet: scale_colour_binned() and the theme's palette.colour.continuous setting.

Restoring the theme

# Restoring the previous theme
theme_set(old)

theme_set() makes the theme saved in old the default again, undoing update_theme(). It draws nothing. Giggly has no global settings, so there is nothing to restore.