Take the 2-minute tour ×

Say have a linear model LM that I want a qq plot of the residuals. Normally I would use the R base graphics:

qqnorm(residuals(LM), ylab="Residuals")

I can figure out how to get the qqnorm part of the plot, but I can't seem to manage the qqline:

ggplot(LM, aes(sample=.resid)) +

I suspect I'm missing something pretty basic, but it seems like there ought to be an easy way of doing this.

EDIT: Many thanks for the solution below. I've modified the code (very slightly) to extract the information from the linear model so that the plot works like the convenience plot in the R base graphics package.

ggQQ <- function(LM) # argument: a linear model
    y <- quantile(LM$resid[!is.na(LM$resid)], c(0.25, 0.75))
    x <- qnorm(c(0.25, 0.75))
    slope <- diff(y)/diff(x)
    p <- ggplot(LM, aes(sample=.resid)) +
        stat_qq(alpha = 0.5) +
        geom_abline(slope = slope, intercept = int, color="blue")

share|improve this question
add comment

3 Answers

up vote 17 down vote accepted

The following code will give you the plot you want. The ggplot package doesn't seem to contain code for calculating the parameters of the qqline, so I don't know if it's possible to achieve such a plot in a (comprehensible) one-liner.

qqplot.data <- function (vec) # argument: vector of numbers
  # following four lines from base R's qqline()
  y <- quantile(vec[!is.na(vec)], c(0.25, 0.75))
  x <- qnorm(c(0.25, 0.75))
  slope <- diff(y)/diff(x)

  d <- data.frame(resids = vec)

  ggplot(d, aes(sample = resids)) + stat_qq() + geom_abline(slope = slope, intercept = int)

share|improve this answer
Works perfectly! I took the liberty of slightly modifying the code to extract the vector directly from a linear model. Of course your solution will work with data that isn't in the form of a linear model, but I thought someone else might want a convenience function for building a qqplot from a LM. –  Peter Dec 5 '10 at 14:59
add comment

The standard Q-Q diagnostic for linear models plots quantiles of the standardized residuals vs. theoretical quantiles of N(0,1). @Peter's ggQQ function plots the residuals. The snippet below amends that and adds a few cosmetic changes to make the plot more like what one would get from plot(lm(...)).

ggQQ = function(lm) {
  # extract standardized residuals from the fit
  d <- data.frame(std.resid = rstandard(lm))
  # calculate 1Q/4Q line
  y <- quantile(d$std.resid[!is.na(d$std.resid)], c(0.25, 0.75))
  x <- qnorm(c(0.25, 0.75))
  slope <- diff(y)/diff(x)

  p <- ggplot(data=d, aes(sample=std.resid)) +
    stat_qq(shape=1, size=3) +           # open circles
    labs(title="Normal Q-Q",             # plot title
         x="Theoretical Quantiles",      # x-axis label
         y="Standardized Residuals") +   # y-axis label
    geom_abline(slope = slope, intercept = int, linetype="dashed")  # dashed reference line

Example of use:

# sample data (y = x + N(0,1), x in [1,100])
df <- data.frame(cbind(x=c(1:100),y=c(1:100+rnorm(100))))
share|improve this answer
add comment

Why not the following?

Given some vector, say,

myresiduals <- rnorm(100) ^ 2

ggplot(data=as.data.frame(qqnorm( myresiduals , plot=F)), mapping=aes(x=x, y=y)) + 
    geom_point() + geom_smooth(method="lm", se=FALSE)

But it seems strange that we have to use a traditional graphics function to prop up ggplot2.

Can't we get the same effect somehow by starting with the vector for which we want the quantile plot and then applying the appropriate "stat" and "geom" functions in ggplot2?

Does Hadley Wickham monitor these posts? Maybe he can show us a better way.

share|improve this answer
the scatter plot resembles the q-q plot of the qqnorm() but the line added by geom_smooth is not same as the one given by qqline(). The solutions given by Aaron and @jlhoward, on the other hand , give plots similar to the base R ones. Can you comment if it is my data, because of which it is misbehaving. –  ktyagi Apr 16 at 15:14
add comment

Your Answer


