Theme shortcuts: subtheme
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
theme_sub_axis(), which covers
theme_sub_axis_x(), theme_sub_axis_y(), theme_sub_axis_bottom(), theme_sub_axis_top(),
theme_sub_axis_left(), theme_sub_axis_right(), theme_sub_legend(), theme_sub_panel(),
theme_sub_plot() and theme_sub_strip() 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.
These functions are shortcuts for theme() with shorter argument names: each sets the elements of
one part of the plot, so theme_sub_axis_bottom(title = ...) sets what theme(axis.title.x.bottom = ...) sets. There is one for all the axes, for both x axes, for both y axes and for each of the
four, and one each for the legend, the panels, the whole plot and the facet strips. Giggly sets the
same elements with Giggly.Plot.theme/2, but has none of these functions
(theme_sub_axis_bottom()). 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 is 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.
Styling the bottom axis with theme()
# A standard plot
p <- ggplot(mtcars, aes(disp, mpg, colour = drat)) +
geom_point()
red_text <- element_text(colour = "red")
red_line <- element_line(colour = "red")
# The theme settings below:
p + theme(
axis.title.x.bottom = red_text,
axis.text.x.bottom = red_text,
axis.line.x.bottom = red_line,
axis.ticks.x.bottom = red_line
)
An element is a keyword list of its properties, so red_text and red_line are keyword lists.
p =
mtcars
|> Plot.new(%{x: :disp, y: :mpg, colour: :drat})
|> Plot.geom_point()
red_text = [colour: "red"]
red_line = [colour: "red"]
p
|> Plot.theme(
axis_title_x_bottom: red_text,
axis_text_x_bottom: red_text,
axis_line_x_bottom: red_line,
axis_ticks_x_bottom: red_line
)
|> render.()
The same with theme_sub_axis_bottom()
# Are equivalent to these less verbose theme settings
p + theme_sub_axis_bottom(
title = red_text,
text = red_text,
line = red_line,
ticks = red_line
)
Not in Giggly yet: theme_sub_axis_bottom().