Solved – How to compare two datasets with Q-Q plot using ggplot2

distributionsggplot2qq-plotr

As both a stats and R novice, I have been having a really difficult time trying to generate qqplots with an aspect ratio of 1:1. ggplot2 seems to offer far more control over plotting than the default R plotting packages, but I can't see how to do a qqplot in ggplot2 to compare two datasets.

So my question, what is the ggplot2 equivalent of something like:

qqplot(datset1,dataset2)

Best Answer

The easiest thing to do is just look at how qqplot works. So in R type:

R> qqplot
function (x, y, plot.it = TRUE, xlab = deparse(substitute(x)), 
    ylab = deparse(substitute(y)), ...) 
{
    sx <- sort(x)
    sy <- sort(y)
    lenx <- length(sx)
    leny <- length(sy)
    if (leny < lenx) 
        sx <- approx(1L:lenx, sx, n = leny)$y
    if (leny > lenx) 
        sy <- approx(1L:leny, sy, n = lenx)$y
    if (plot.it) 
        plot(sx, sy, xlab = xlab, ylab = ylab, ...)
    invisible(list(x = sx, y = sy))
}
<environment: namespace:stats>

So to generate the plot we just have to get sx and sy, i.e:

x <- rnorm(10);y <- rnorm(20)

sx <- sort(x); sy <- sort(y)
lenx <- length(sx)
leny <- length(sy)
if (leny < lenx)sx <- approx(1L:lenx, sx, n = leny)$y
if (leny > lenx)sy <- approx(1L:leny, sy, n = lenx)$y

require(ggplot2)
g = ggplot() + geom_point(aes(x=sx, y=sy))
g

qqplot using ggplot2

Related Question