Intervals: geom_linerange
Mix.install([
{:giggly, path: Path.join(__DIR__, "..")},
{:kino, "~> 0.19"}
])
alias Giggly.Plot
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_linerange(), which covers
geom_crossbar(), geom_errorbar(), geom_errorbarh() and geom_pointrange() 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 each draw an interval: a range from ymin to ymax at an x, or from xmin to
xmax at a y when turned on its side. geom_linerange() draws it as a line,
geom_pointrange() as a line with a point at y, geom_errorbar() as a line capped at each end,
and geom_crossbar() as a box with a line across it at y. geom_errorbarh() is deprecated in
favour of geom_errorbar(orientation = "y"). Giggly has Giggly.Plot.geom_linerange/3,
Giggly.Plot.geom_pointrange/3, Giggly.Plot.geom_errorbar/3 and Giggly.Plot.geom_crossbar/3,
turned on their side by mapping xmin and xmax or by orientation:, and dodged with
position: :dodge or {:dodge, width: width}, a line range and a point range, which have no
width of their own, only within a width given; it has no
geom_errorbarh(). 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.
Lines, points, crossbars and error bars
# Create a simple example dataset
df <- data.frame(
trt = factor(c(1, 1, 2, 2)),
resp = c(1, 5, 3, 4),
group = factor(c(1, 2, 1, 2)),
upper = c(1.1, 5.3, 3.3, 4.2),
lower = c(0.8, 4.6, 2.4, 3.6)
)
p <- ggplot(df, aes(trt, resp, colour = group))
p + geom_linerange(aes(ymin = lower, ymax = upper))
p + geom_pointrange(aes(ymin = lower, ymax = upper))
p + geom_crossbar(aes(ymin = lower, ymax = upper), width = 0.2)
p + geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2)
The cell writes the table out, trt and group as factors of their numbers with
Giggly.Factor.new/2.
df = %{
"trt" => Giggly.Factor.new([1, 1, 2, 2]),
"resp" => [1, 5, 3, 4],
"group" => Giggly.Factor.new([1, 2, 1, 2]),
"upper" => [1.1, 5.3, 3.3, 4.2],
"lower" => [0.8, 4.6, 2.4, 3.6]
}
p = Plot.new(df, %{x: :trt, y: :resp, colour: :group})
p
|> Plot.geom_linerange(%{ymin: :lower, ymax: :upper})
|> render.()
p
|> Plot.geom_pointrange(%{ymin: :lower, ymax: :upper})
|> render.()
p
|> Plot.geom_crossbar(%{ymin: :lower, ymax: :upper}, width: 0.2)
|> render.()
p
|> Plot.geom_errorbar(%{ymin: :lower, ymax: :upper}, width: 0.2)
|> render.()
Horizontal intervals
# Flip the orientation by changing mapping
ggplot(df, aes(resp, trt, colour = group)) +
geom_linerange(aes(xmin = lower, xmax = upper))
Mapping xmin and xmax rather than ymin and ymax turns the intervals on their side.
df
|> Plot.new(%{x: :resp, y: :trt, colour: :group})
|> Plot.geom_linerange(%{xmin: :lower, xmax: :upper})
|> render.()
Lines through the group means
# Draw lines connecting group means
p +
geom_line(aes(group = group)) +
geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2)
p
|> Plot.geom_line(%{group: :group})
|> Plot.geom_errorbar(%{ymin: :lower, ymax: :upper}, width: 0.2)
|> render.()
Dodging bars and error bars
# If you want to dodge bars and errorbars, you need to manually
# specify the dodge width
p <- ggplot(df, aes(trt, resp, fill = group))
p +
geom_col(position = "dodge") +
geom_errorbar(aes(ymin = lower, ymax = upper), position = "dodge", width = 0.25)
Each layer is dodged by its own width here, so the narrower error bars do not line up with the bars.
p = Plot.new(df, %{x: :trt, y: :resp, fill: :group})
p
|> Plot.geom_col(position: :dodge)
|> Plot.geom_errorbar(%{ymin: :lower, ymax: :upper}, position: :dodge, width: 0.25)
|> render.()
Dodging by one width
# Because the bars and errorbars have different widths
# we need to specify how wide the objects we are dodging are
dodge <- position_dodge(width=0.9)
p +
geom_col(position = dodge) +
geom_errorbar(aes(ymin = lower, ymax = upper), position = dodge, width = 0.25)
{:dodge, width: 0.9} gives the width to dodge within, so both layers are dodged within 0.9 and
each error bar sits on its bar.
dodge = {:dodge, width: 0.9}
p
|> Plot.geom_col(position: dodge)
|> Plot.geom_errorbar(%{ymin: :lower, ymax: :upper}, position: dodge, width: 0.25)
|> render.()
Dodging with padding
# When using geom_errorbar() with position_dodge2(), extra padding will be
# needed between the error bars to keep them aligned with the bars.
p +
geom_col(position = "dodge2") +
geom_errorbar(
aes(ymin = lower, ymax = upper),
position = position_dodge2(width = 0.5, padding = 0.5)
)
R ignores width = 0.5 here: the error bars have edges of their own, which position_dodge2()
dodges by, so it draws what padding = 0.5 alone draws. Giggly's cell leaves the width out, since
{:dodge2, options} takes a width: only for marks without edges, such as points.
p
|> Plot.geom_col(position: :dodge2)
|> Plot.geom_errorbar(%{ymin: :lower, ymax: :upper}, position: {:dodge2, padding: 0.5})
|> render.()