Powered by AppSignal & Oban Pro

Connections: stat_connect

notebooks/stat_connect.livemd

Connections: stat_connect

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

About this notebook

This notebook follows the examples on ggplot2's reference page for stat_connect(), 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.

stat_connect() joins each observation to the next with a line of a chosen shape: "hv" goes horizontally and then vertically, "vh" vertically and then horizontally, "mid" steps half-way between the two x values, and "linear" goes straight. A shape of one's own is a matrix of points on a path from (0, 0) to (1, 1), which is stretched to fit between each pair of observations. Giggly has none of it yet.

Steps

ggplot(head(economics, 20), aes(date, unemploy)) +
  stat_connect(connection = "hv")

head() keeps the first 20 months of ggplot2's economics dataset, whose unemploy is the number of unemployed in the US, in thousands.

Not in Giggly yet: stat_connect().

Connections of one's own

# Setup custom connections
x <- seq(0, 1, length.out = 20)[-1]
smooth <- cbind(x, scales::rescale(1 / (1 + exp(-(x * 10 - 5)))))
zigzag <- cbind(c(0.4, 0.6, 1), c(0.75, 0.25, 1))

ggplot(head(economics, 10), aes(date, unemploy)) +
  geom_point() +
  stat_connect(aes(colour = "zigzag"), connection = zigzag) +
  stat_connect(aes(colour = "smooth"), connection = smooth)

smooth is an S-shaped curve and zigzag goes up, down and up again. Mapping colour to a constant names each layer in the legend.

Not in Giggly yet: stat_connect() and its connection option given as a matrix.