Rug plots: geom_rug
Mix.install([
{:giggly, path: Path.join(__DIR__, "..")},
{:kino, "~> 0.19"}
])
alias Giggly.Plot
mpg = Giggly.Datasets.mpg()
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
geom_rug(), 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.
A rug plot adds the two one-dimensional distributions to a two-dimensional display, such as a
scatter plot: a short line at the edge of the panel for each row's x, and another for its y. It
draws every row, so it suits small tables. Giggly draws rugs with Giggly.Plot.geom_rug/3, along
the sides sides: names, as long as length: says, inside the panel or, with outside: true,
outside it, and jittered as Giggly.Plot.geom_jitter/3 jitters points. 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 cells draw Giggly.Datasets.mtcars/0, R's mtcars:
32 cars from the 1974 Motor Trend US magazine, and 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 mtcars
and mpg, R's names for them.
A scatter plot
p <- ggplot(mtcars, aes(wt, mpg)) +
geom_point()
p
p =
mtcars
|> Plot.new(%{x: :wt, y: :mpg})
|> Plot.geom_point()
render.(p)
Rugs along the sides
p + geom_rug()
p + geom_rug(sides="b") # Rug on bottom only
p + geom_rug(sides="trbl") # All four sides
By default the rug runs along the bottom and the left, sides = "bl". Giggly's sides: is a
list of the sides, in any order, where ggplot2's is a string of their first letters.
p
|> Plot.geom_rug()
|> render.()
p
|> Plot.geom_rug(sides: [:bottom])
|> render.()
p
|> Plot.geom_rug(sides: [:top, :right, :bottom, :left])
|> render.()
Jittering the rug
# Use jittering to avoid overplotting for smaller datasets
ggplot(mpg, aes(displ, cty)) +
geom_point() +
geom_rug()
ggplot(mpg, aes(displ, cty)) +
geom_jitter() +
geom_rug(alpha = 1/2, position = "jitter")
displ and cty take few distinct values, so many cars share a point and a rug line. Where a
rug runs along both axes, cars sharing both values are drawn once on each side, as ggplot2 draws
them, so the first plot's 234 cars draw 100 lines along each side.
mpg
|> Plot.new(%{x: :displ, y: :cty})
|> Plot.geom_point()
|> Plot.geom_rug()
|> render.()
Jittering moves each car by a small random amount, so they can be told apart. The points are
jittered by Erlang's random numbers, not R's, so they land elsewhere than ggplot2's, each within the
same 0.04 litres of its place across and 0.4 miles per gallon up or down, and the axes, which widen
to hold them, can end a little elsewhere too, by no more than about 0.08 litres and 0.8 miles per
gallon; nothing else in the drawing moves (geom_jitter()'s notebook says more). The rug is
jittered from the same seed as the points, so each of its lines marks a point, where ggplot2 draws
new random numbers for each layer and its rug's lines fall beside the points.
mpg
|> Plot.new(%{x: :displ, y: :cty})
|> Plot.geom_jitter()
|> Plot.geom_rug(alpha: 0.5, position: :jitter)
|> render.()
Rugs outside the panel
# move the rug tassels to outside the plot
# remember to set clip = "off".
p +
geom_rug(outside = TRUE) +
coord_cartesian(clip = "off")
The panel cuts off what is drawn past its edges, so lines running outside it show only where
Giggly.Plot.coord_cartesian/2 is given clip: :off.
p
|> Plot.geom_rug(outside: true)
|> Plot.coord_cartesian(clip: :off)
|> render.()
Rugs on the top and right
# set sides to top right, and then move the margins
p +
geom_rug(outside = TRUE, sides = "tr") +
coord_cartesian(clip = "off") +
theme(plot.margin = margin_auto(1, unit = "cm"))
A centimetre of margin on every side leaves the lines room past the panel's top and right edges.
Giggly writes the margin margin_auto(1, unit = "cm") gives as one length, {1, :cm}.
p
|> Plot.geom_rug(outside: true, sides: [:top, :right])
|> Plot.coord_cartesian(clip: :off)
|> Plot.theme(plot_margin: {1, :cm})
|> render.()
Longer rug lines
# increase the line length and
# expand axis to avoid overplotting
p +
geom_rug(length = unit(0.05, "npc")) +
scale_y_continuous(expand = c(0.1, 0.1))
A rug's lines are 3% of the panel long by default; {0.05, :npc} makes them 5%, as unit(0.05, "npc") does. ggplot2 reads the two numbers of expand = c(0.1, 0.1) as a multiple of the axis's
range and a distance in its units, left at each end, which Giggly writes [mult: 0.1, add: 0.1].
p
|> Plot.geom_rug(length: {0.05, :npc})
|> Plot.scale_y_continuous(expand: [mult: 0.1, add: 0.1])
|> render.()