Reference lines: geom_abline
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
geom_abline(), which covers
geom_hline() and geom_vline() 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 geoms add reference lines to a plot: vertical ones at an xintercept, horizontal ones at a
yintercept, and diagonal ones given a slope and an intercept. Given as arguments, a line is
the same in every panel; mapped from a data frame, the lines can differ from panel to panel. Unlike
other geoms, they take nothing from the plot's mapping. ggplot2's page says they do not affect the
x and y scales, but a vertical or horizontal line's intercept widens its axis to show it, as a
point's position does: the line at 1 in the second example widens the x axis past the lightest
car, 1.513. A diagonal line's slope and intercept change no scale. Giggly draws them with
Giggly.Plot.geom_vline/3, Giggly.Plot.geom_hline/3 and Giggly.Plot.geom_abline/3, each
given its lines as options, xintercept: 5, or mapped from a table. 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 Giggly.Datasets.mtcars/0, R's mtcars, to mtcars, R's name for it.
Vertical and horizontal lines
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
# Fixed values
p + geom_vline(xintercept = 5)
p + geom_vline(xintercept = 1:5)
p + geom_hline(yintercept = 20)
1:5 is R's sequence of the whole numbers from 1 to 5, a line for each, which Giggly gives as a
list.
p =
mtcars
|> Plot.new(%{x: :wt, y: :mpg})
|> Plot.geom_point()
p
|> Plot.geom_vline(xintercept: 5)
|> render.()
p
|> Plot.geom_vline(xintercept: [1, 2, 3, 4, 5])
|> render.()
p
|> Plot.geom_hline(yintercept: 20)
|> render.()
Diagonal lines
p + geom_abline() # Can't see it - outside the range of the data
p + geom_abline(intercept = 20)
With no arguments, the line is y = x, which runs below the panel, out of sight; given only an
intercept, its slope is 1.
p
|> Plot.geom_abline()
|> render.()
p
|> Plot.geom_abline(intercept: 20)
|> render.()
A line of best fit
# Calculate slope and intercept of line of best fit
coef(lm(mpg ~ wt, data = mtcars))
p + geom_abline(intercept = 37, slope = -5)
R fits mpg to wt with a linear model and prints its intercept and slope, which the line then
rounds. Not in Giggly yet: models fitted by lm().
Fitting the line with geom_smooth()
# But this is easier to do with geom_smooth:
p + geom_smooth(method = "lm", se = FALSE)
p is still the first example's plot, mtcars' weights against their miles per gallon. The
smooth fits the same line as lm(mpg ~ wt).
p
|> Plot.geom_smooth(method: :lm, se: false)
|> render.()
Lines that differ between facets
# To show different lines in different facets, use aesthetics
p <- ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
facet_wrap(~ cyl)
mean_wt <- data.frame(cyl = c(4, 6, 8), wt = c(2.28, 3.11, 4.00))
p + geom_hline(aes(yintercept = wt), mean_wt)
facet_wrap(~ cyl) is Giggly.Plot.facet_wrap(plot, :cyl), and mean_wt is written out as a
table of its own, given to the layer as data:. Each line comes from the row of mean_wt with
its panel's cyl.
p =
mtcars
|> Plot.new(%{x: :mpg, y: :wt})
|> Plot.geom_point()
|> Plot.facet_wrap(:cyl)
mean_wt = %{"cyl" => [4.0, 6.0, 8.0], "wt" => [2.28, 3.11, 4.00]}
p
|> Plot.geom_hline(%{yintercept: :wt}, data: mean_wt)
|> render.()
Other aesthetics
# You can also control other aesthetics
ggplot(mtcars, aes(mpg, wt, colour = wt)) +
geom_point() +
geom_hline(aes(yintercept = wt, colour = wt), mean_wt) +
facet_wrap(~ cyl)
The points and the lines share the colour scale, so each line takes the colour of its mean weight on the points' gradient.
mtcars
|> Plot.new(%{x: :mpg, y: :wt, colour: :wt})
|> Plot.geom_point()
|> Plot.geom_hline(%{yintercept: :wt, colour: :wt}, data: mean_wt)
|> Plot.facet_wrap(:cyl)
|> render.()