Quantile-quantile plots: geom_qq
Mix.install([
{:giggly, path: Path.join(__DIR__, "..")},
{:kino, "~> 0.19"}
])
About this notebook
This notebook follows the examples on ggplot2's reference page for
geom_qq(), which covers geom_qq_line(),
stat_qq_line() and stat_qq() 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.
A quantile-quantile plot compares a sample with a theoretical distribution. geom_qq() and
stat_qq() sort the values of the column mapped to sample and plot each against the
distribution's quantile at the same proportion, so a sample from that distribution, the normal by
default, falls close to a straight line. geom_qq_line() and stat_qq_line() draw the line
through the points at two quantiles of both distributions, the quartiles by default. Giggly has
none of this yet.
A normal Q-Q plot
R's rt() draws 200 random values from a t distribution with 5 degrees of freedom, different on
every run.
df <- data.frame(y = rt(200, df = 5))
p <- ggplot(df, aes(sample = y))
p + stat_qq() + stat_qq_line()
Not in Giggly yet: mapping sample, stat_qq() and stat_qq_line().
Another theoretical distribution
# Use fitdistr from MASS to estimate distribution params:
# if (requireNamespace("MASS", quietly = TRUE)) {
# params <- as.list(MASS::fitdistr(df$y, "t")$estimate)
# }
# Here, we use pre-computed params
params <- list(m = -0.02505057194115, s = 1.122568610124, df = 6.63842653897)
ggplot(df, aes(sample = y)) +
stat_qq(distribution = qt, dparams = params["df"]) +
stat_qq_line(distribution = qt, dparams = params["df"])
This compares the sample with a t distribution, R's qt(), whose degrees of freedom dparams
gives, rather than with the normal.
Not in Giggly yet: mapping sample, and stat_qq() and stat_qq_line() with their
distribution and dparams options.
The distribution of a column
# Using to explore the distribution of a variable
ggplot(mtcars, aes(sample = mpg)) +
stat_qq() +
stat_qq_line()
Not in Giggly yet: mapping sample, stat_qq() and stat_qq_line().
A line for each group
ggplot(mtcars, aes(sample = mpg, colour = factor(cyl))) +
stat_qq() +
stat_qq_line()
Mapping colour to the number of cylinders splits the cars into groups, with points and a line
for each.
Not in Giggly yet: stat_qq() and stat_qq_line(), and mapping sample.