Powered by AppSignal & Oban Pro

Line types, sizes and shapes: aes_linetype_size_shape

notebooks/aes_linetype_size_shape.livemd

Line types, sizes and shapes: aes_linetype_size_shape

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

alias Giggly.Plot

mtcars = Giggly.Datasets.mtcars()
economics = Giggly.Datasets.economics()
economics_long = Giggly.Datasets.economics_long()

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 aes_linetype_size_shape, 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.

The linetype, linewidth, size and shape aesthetics change how lines and points look: a line's dash pattern and width, the size of a point or of text, and the symbol a point is drawn with. Each can be set to one value for every mark, or mapped to a column. Giggly sets each and maps each but a text's size, drawing the legend that reads it back; it takes no character as a shape and has no identity scales (scale_shape_identity()). 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, Giggly.Datasets.economics/0, ggplot2's economics: US economic time series, one row a month from 1967 to 2015, and Giggly.Datasets.economics_long/0, ggplot2's economics_long: the same series, a row for each month and series, which the setup cell binds to mtcars, economics and economics_long, the names R and ggplot2 give them.

Line types

A line type is a number from 0 to 6 or its name: 2 is "dashed", 4 is "dotdash". Giggly takes either, the name as an atom.

df <- data.frame(x = 1:10 , y = 1:10)
p <- ggplot(df, aes(x, y))
p + geom_line(linetype = 2)
p + geom_line(linetype = "dotdash")
p = Plot.new(%{"x" => Enum.to_list(1..10), "y" => Enum.to_list(1..10)}, %{x: :x, y: :y})

p
|> Plot.geom_line(linetype: 2)
|> render.()
p
|> Plot.geom_line(linetype: :dotdash)
|> render.()

Dash patterns as hex strings

# An example with hex strings; the string "33" specifies three units on followed
# by three off and "3313" specifies three units on followed by three off followed
# by one on and finally three off.
p + geom_line(linetype = "3313")

Giggly writes a dash pattern as a tuple of the lengths, on and off in turn, {3, 3, 1, 3} for "3313".

p
|> Plot.geom_line(linetype: {3, 3, 1, 3})
|> render.()

Mapping line type

# Mapping line type from a grouping variable
ggplot(economics_long, aes(date, value01)) +
  geom_line(aes(linetype = variable))
economics_long
|> Plot.new(%{x: :date, y: :value01})
|> Plot.geom_line(%{linetype: :variable})
|> render.()

Line widths

# Linewidth examples
ggplot(economics, aes(date, unemploy)) +
  geom_line(linewidth = 2, lineend = "round")
ggplot(economics, aes(date, unemploy)) +
  geom_line(aes(linewidth = uempmed), lineend = "round")

The first draws a line 2 mm wide with its ends rounded; the second maps linewidth to a column, the median weeks unemployed.

economics
|> Plot.new(%{x: :date, y: :unemploy})
|> Plot.geom_line(linewidth: 2, lineend: :round)
|> render.()
economics
|> Plot.new(%{x: :date, y: :unemploy})
|> Plot.geom_line(%{linewidth: :uempmed}, lineend: :round)
|> render.()

Point sizes

# Size examples
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(size = 4)
p + geom_point(aes(size = qsec))
p + geom_point(size = 2.5) +
  geom_hline(yintercept = 25, size = 3.5)

The size given to geom_hline() is its line's width, which ggplot2 now calls linewidth, warning that size is deprecated; Giggly takes only linewidth:, and the drawing is the same.

p = Plot.new(mtcars, %{x: :wt, y: :mpg})

p
|> Plot.geom_point(size: 4)
|> render.()
p
|> Plot.geom_point(%{size: :qsec})
|> render.()
p
|> Plot.geom_point(size: 2.5)
|> Plot.geom_hline(yintercept: 25, linewidth: 3.5)
|> render.()

Point shapes

p is still the plot of mtcars from the size examples. Without a shape, a point is ggplot2's default one, a solid circle.

# Shape examples
p + geom_point()
p
|> Plot.geom_point()
|> render.()

A shape is a number from 0 to 25, a single character drawn as the symbol, "." for the smallest visible dot, or NA for no point at all.

p + geom_point(shape = 5)
p + geom_point(shape = "k", size = 3)
p + geom_point(shape = ".")
p + geom_point(shape = NA)
p + geom_point(aes(shape = factor(cyl)))

Giggly takes a shape by its number or its name, :diamond_open for 5, and :none for NA, and draws all but the second and third, which draw a character as the shape. Giggly writes factor(cyl) as {:factor, [:cyl]}.

p
|> Plot.geom_point(shape: 5)
|> render.()
p
|> Plot.geom_point(shape: :none)
|> render.()
p
|> Plot.geom_point(%{shape: {:factor, [:cyl]}})
|> render.()

Not in Giggly yet: a character as a shape.

The 25 symbols

R's data.frame() repeats x = 1:5 to fill the 25 rows, so the symbols sit in five columns. scale_shape_identity() uses each value of z as a shape number as it stands.

# A look at all 25 symbols
df2 <- data.frame(x = 1:5 , y = 1:25, z = 1:25)
p <- ggplot(df2, aes(x, y))
p + geom_point(aes(shape = z), size = 4) +
  scale_shape_identity()
# While all symbols have a foreground colour, symbols 19-25 also take a
# background colour (fill)
p + geom_point(aes(shape = z), size = 4, colour = "Red") +
  scale_shape_identity()
p + geom_point(aes(shape = z), size = 4, colour = "Red", fill = "Black") +
  scale_shape_identity()

Not in Giggly yet: scale_shape_identity().