Not a full-blown answer, but the `ggproto`

object for `stat_qq`

here (that link's just pointing to the line in the tidyverse source code of `stat_qq.r`

on GitHub) might be of help.

##
code excerpt also here

```
StatQq <- ggproto("StatQq", Stat,
default_aes = aes(y = ..sample.., x = ..theoretical..),
required_aes = c("sample"),
compute_group = function(data, scales, quantiles = NULL,
distribution = stats::qnorm, dparams = list(),
na.rm = FALSE) {
sample <- sort(data$sample)
n <- length(sample)
# Compute theoretical quantiles
if (is.null(quantiles)) {
quantiles <- stats::ppoints(n)
} else {
stopifnot(length(quantiles) == n)
}
theoretical <- do.call(distribution, c(list(p = quote(quantiles)), dparams))
data.frame(sample, theoretical)
}
)
```

It's basically retrieving the sample data of the specified variable (`hwy`

in this case), and computing quantiles (using length), and sorting it for plotting. (Which, I know, isn't a great explanation)!