Discrete colour scales: scale_colour_discrete
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_discrete(),
which covers scale_fill_discrete() 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_discrete() and scale_fill_discrete() are the scales ggplot2 uses by default when a
column of discrete values is mapped to colour or fill, giving each value its own colour from a
palette. Their default palette is a setting of the theme. Giggly draws a discrete column mapped to
colour or fill as scale_colour_discrete() does by default, in hues evenly spaced round the
colour wheel with a legend, and has Giggly.Plot.scale_color_discrete/2 and
Giggly.Plot.scale_fill_discrete/2, whose palette: is a palette's name, as the scales package
names them, or a list of colours, and takes no palette from the theme (palette.colour.discrete).
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. It binds ggplot2's
mpg, Giggly.Datasets.mpg/0, to mpg.
A palette given to the scale
scales::pal_brewer() is a palette from the scales package, here ColorBrewer's Dark2.
# A standard plot
p <- ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point()
# You can use the scale to give a palette directly
p + scale_colour_discrete(palette = scales::pal_brewer(palette = "Dark2"))
Giggly names the palette, "Dark2", where ggplot2 is handed the palette scales::pal_brewer()
makes; ggplot2 looks a name up as the same palette.
p =
mpg
|> Plot.new(%{x: :displ, y: :hwy, colour: :class})
|> Plot.geom_point()
p
|> Plot.scale_color_discrete(palette: "Dark2")
|> render.()
A palette from the theme
# The default colours are encoded into the theme
p + theme(palette.colour.discrete = scales::pal_grey())
Not in Giggly yet: the theme's palette.colour.discrete 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.discrete = scales::pal_viridis())
# Plot now shows new global default
p
Not in Giggly yet: the theme's palette.colour.discrete 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.