Created
October 10, 2012 21:19
-
-
Save cwickham/3868481 to your computer and use it in GitHub Desktop.
ggplot2 stat for adding lines to quantile-quantile plots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require(proto) | |
| stat_qqline <- function (mapping = NULL, data = NULL, geom = "abline", position = "identity", | |
| distribution = qnorm, dparams = list(), na.rm = FALSE, ...) { | |
| StatQqline$new(mapping = mapping, data = data, geom = geom, position = position, | |
| distribution = distribution, dparams = dparams, na.rm = na.rm, ...) | |
| } | |
| StatQqline <- proto(ggplot2:::Stat, { | |
| objname <- "qqline" | |
| default_geom <- function(.) GeomAbline | |
| default_aes <- function(.) aes(slope = ..slope.., intercept = ..int..) | |
| required_aes <- c("sample") | |
| calculate <- function(., data, scales, quantiles = NULL, distribution = qnorm, dparams = list(), na.rm = FALSE) { | |
| data <- remove_missing(data, na.rm, "sample", name = "stat_qqline") | |
| sample <- sort(data$sample) | |
| n <- length(sample) | |
| y <- quantile(data$sample, c(0.25, 0.75)) | |
| x <- distribution(c(0.25, 0.75)) | |
| slope <- diff(y)/diff(x) | |
| int <- y[1L] - slope * x[1L] | |
| data.frame(slope = slope, int = int) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment