Summaries: stat_summary
Mix.install([
{:giggly, path: Path.join(__DIR__, "..")},
{:kino, "~> 0.19"}
])
alias Giggly.Plot
mtcars = Giggly.Datasets.mtcars()
diamonds = Giggly.Datasets.diamonds()
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
stat_summary(), which covers
stat_summary_bin() 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.
stat_summary() summarises the y values at each unique x, or the x values at each y, and
stat_summary_bin() those in each bin of x: where stat_bin() only counts, they compute any
summary. The summary is one function that returns a middle and the ends of a range (fun.data),
or a function for each (fun, fun.min and fun.max); given neither, it is the mean and its
standard error, mean_se(). Each summary is drawn as a point with a line through its range,
geom_pointrange(), unless another geom is given. Giggly has Giggly.Plot.stat_summary/3: its
fun_data: names the whole summary, :mean_se unless given, or one of the Hmisc package's,
alone or in a tuple with its arguments ({:mean_sdl, mult: 1}), and its fun:, fun_min: and
fun_max: give a summary each, by name or as a function of the values; its geom: draws them as
point ranges, points, lines, bars, crossbars, error bars, line ranges or a ribbon. Not in Giggly
yet: stat_summary_bin().
mean_cl_boot() resamples the data at random. ggplot2's examples set no seed, so R draws these
intervals a little differently on every run; Giggly draws its resamples from seed:, 0 unless
given ({:mean_cl_boot, seed: 7}), so it draws the same intervals every time, and they differ
from any one of R's by sampling alone. Over 500 seeds on these cars, Giggly's ends and R's have
the same average, within 0.005 mpg, and the same spread: an end moves about a tenth of an mpg from
seed to seed, a point at this size, and three quarters of one at most, about 8 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. It binds R's mtcars
and ggplot2's diamonds, Giggly.Datasets.mtcars/0 and Giggly.Datasets.diamonds/0, to their
names in R.
Means with bootstrapped intervals
d <- ggplot(mtcars, aes(cyl, mpg)) + geom_point()
d + stat_summary(fun.data = "mean_cl_boot", colour = "red", linewidth = 2, size = 3)
mean_cl_boot(), from the Hmisc package, gives the mean with a confidence interval found by
resampling the data; Giggly names it :mean_cl_boot. Drawn from Giggly's seed, its ends differ
from R's by sampling, as the About says. "red" is the same colour in CSS as in R, #FF0000.
d =
mtcars
|> Plot.new(%{x: :cyl, y: :mpg})
|> Plot.geom_point()
d
|> Plot.stat_summary(fun_data: :mean_cl_boot, colour: "red", linewidth: 2, size: 3)
|> render.()
Horizontal summaries
# Orientation follows the discrete axis
ggplot(mtcars, aes(mpg, factor(cyl))) +
geom_point() +
stat_summary(fun.data = "mean_cl_boot", colour = "red", linewidth = 2, size = 3)
Giggly writes factor(cyl) as {:factor, [:cyl]}. With y alone discrete, the mpg values
are summarised at each cyl and the ranges run across. Drawn from Giggly's seed, the ends differ
from R's by sampling, as the About says; mpg runs along the wider axis here, so a tenth of an
mpg is about 2 points and three quarters of one about 14.
mtcars
|> Plot.new(%{x: :mpg, y: {:factor, [:cyl]}})
|> Plot.geom_point()
|> Plot.stat_summary(fun_data: :mean_cl_boot, colour: "red", linewidth: 2, size: 3)
|> render.()
One summary function
# You can supply individual functions to summarise the value at
# each x:
d + stat_summary(fun = "median", colour = "red", size = 2, geom = "point")
d + stat_summary(fun = "mean", colour = "red", size = 2, geom = "point")
d + aes(colour = factor(vs)) + stat_summary(fun = mean, geom="line")
The last plot adds a mapping to d with + aes(), and joins the means of each group with a
line. Giggly draws the first two, naming each summary by an atom, fun: :median and
fun: :mean.
d
|> Plot.stat_summary(fun: :median, colour: "red", size: 2, geom: :point)
|> render.()
d
|> Plot.stat_summary(fun: :mean, colour: "red", size: 2, geom: :point)
|> render.()
Not in Giggly yet: adding a mapping with + aes().
A range from three functions
d + stat_summary(fun = mean, fun.min = min, fun.max = max, colour = "red")
Each point is the mean mpg of its cyl, and its line runs from the smallest mpg to the
largest.
d
|> Plot.stat_summary(fun: :mean, fun_min: :min, fun_max: :max, colour: "red")
|> render.()
Summaries as bars
d <- ggplot(diamonds, aes(cut))
d + geom_bar()
d + stat_summary(aes(y = price), fun = "mean", geom = "bar")
The first plot counts the diamonds of each cut, the cuts from Fair to Ideal, the order of their levels; the second draws the mean price of each cut as a bar.
d = Plot.new(diamonds, %{x: :cut})
d
|> Plot.geom_bar()
|> render.()
d
|> Plot.stat_summary(%{y: :price}, fun: :mean, geom: :bar)
|> render.()
Summaries in bins
# Orientation of stat_summary_bin is ambiguous and must be specified directly
ggplot(diamonds, aes(carat, price)) +
stat_summary_bin(fun = "mean", geom = "bar", orientation = 'y')
Not in Giggly yet: stat_summary_bin() and its orientation option.
Zooming in without losing data
# Don't use ylim to zoom into a summary plot - this throws the
# data away
p <- ggplot(mtcars, aes(cyl, mpg)) +
stat_summary(fun = "mean", geom = "point")
p
p + ylim(15, 30)
# Instead use coord_cartesian
p + coord_cartesian(ylim = c(15, 30))
ylim() sets the limits of the y scale, which drops the rows outside them before the means are
taken, and so changes the means; coord_cartesian() zooms in after the stat, leaving them as
they were. Giggly draws the first two: Giggly.Plot.ylim/3 drops the rows too, so its means move
as R's do.
p =
mtcars
|> Plot.new(%{x: :cyl, y: :mpg})
|> Plot.stat_summary(fun: :mean, geom: :point)
render.(p)
p
|> Plot.ylim(15, 30)
|> render.()
Not in Giggly yet: coord_cartesian()'s ylim option.
Summary functions from Hmisc
# A set of useful summary functions is provided from the Hmisc package:
stat_sum_df <- function(fun, geom="crossbar", ...) {
stat_summary(fun.data = fun, colour = "red", geom = geom, width = 0.2, ...)
}
d <- ggplot(mtcars, aes(cyl, mpg)) + geom_point()
# The crossbar geom needs grouping to be specified when used with
# a continuous x axis.
d + stat_sum_df("mean_cl_boot", mapping = aes(group = cyl))
d + stat_sum_df("mean_sdl", mapping = aes(group = cyl))
d + stat_sum_df("mean_sdl", fun.args = list(mult = 1), mapping = aes(group = cyl))
d + stat_sum_df("median_hilow", mapping = aes(group = cyl))
stat_sum_df() is a helper the example writes: a stat_summary() drawn as red crossbars.
mean_sdl() is the mean give or take a multiple of the standard deviation, which mult = 1
sets to one, and median_hilow() the median with a range holding the middle of the data.
Giggly's helper is a function of the plot, the summary and the mapping, and a summary's arguments
go in its tuple, so fun.args = list(mult = 1) is {:mean_sdl, mult: 1}. The first plot's ends,
drawn from Giggly's seed, differ from R's by sampling, as the About says.
stat_sum_df = fn plot, fun, mapping ->
Plot.stat_summary(plot, mapping, fun_data: fun, colour: "red", geom: :crossbar, width: 0.2)
end
d =
mtcars
|> Plot.new(%{x: :cyl, y: :mpg})
|> Plot.geom_point()
d
|> stat_sum_df.(:mean_cl_boot, %{group: :cyl})
|> render.()
d
|> stat_sum_df.(:mean_sdl, %{group: :cyl})
|> render.()
d
|> stat_sum_df.({:mean_sdl, mult: 1}, %{group: :cyl})
|> render.()
d
|> stat_sum_df.(:median_hilow, %{group: :cyl})
|> render.()
Skewed distributions
ggplot2's example takes a random sample of the movies dataset, from the ggplot2movies
package, with R's set.seed(596), and runs only where that package is installed.
# An example with highly skewed distributions:
if (require("ggplot2movies")) {
set.seed(596)
mov <- movies[sample(nrow(movies), 1000), ]
m2 <-
ggplot(mov, aes(x = factor(round(rating)), y = votes)) +
geom_point()
m2 <-
m2 +
stat_summary(
fun.data = "mean_cl_boot",
geom = "crossbar",
colour = "red", width = 0.3
) +
xlab("rating")
m2
# Notice how the overplotting skews off visual perception of the mean
# supplementing the raw data with summary statistics is _very_ important
xlab("rating") is Giggly.Plot.labs/2's x: "rating", since ggplot2 has superseded xlab()
with labs().
Not in Giggly yet: the movies dataset.
A log scale before or after the stat
# Next, we'll look at votes on a log scale.
# Transforming the scale means the data are transformed
# first, after which statistics are computed:
m2 + scale_y_log10()
# Transforming the coordinate system occurs after the
# statistic has been computed. This means we're calculating the summary on the raw data
# and stretching the geoms onto the log scale. Compare the widths of the
# standard errors.
m2 + coord_transform(y="log10")
}
Not in Giggly yet: coord_transform() and everything the plot above waits for.