2

It is helpful to be able to view your data. When you have several variables, you can form a scatterplot matrix with, for example, pairs(). A scatterplot matrix gives you a set of 2D marginal projections of your data.

set.seed(8092)
X <- matrix(rnorm(80), ncol=4)
pairs(X)

enter image description here

You can also have data for different groups and want to compare their distributions. Two such distributions can be compared with a qq-plot.

set.seed(4415)
group1 <- rnorm(20)
group2 <- rnorm(20)
qqplot(group1, group2)
abline(c(0,1))

enter image description here

When you have several groups, it would be convenient if there were a pairs-type plot that would display a matrix of qq-plots.

colnames(X) <- c("group1", "group2", "group3", "group4")
qq.pairs(X)

Is there such a function? Is there a straightforward way to code it from scratch?

gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
  • http://stackoverflow.com/questions/19599745/qqline-in-ggplot2-with-facets – NPE Nov 11 '14 at 21:15
  • As a general remark, I would highly recommend `ggplot2`. It is awesome. – NPE Nov 11 '14 at 21:16
  • Thanks for the link @NPE. I'm not sure if I follow the code in the answer there. Is it plotting 1 set of empirical data against another set of empirical data, or empirical data against a theoretical normal? It seems to be the latter. – gung - Reinstate Monica Nov 11 '14 at 21:25
  • That link was meant as a pointer for further research, not as a complete solution to your exact problem. The code in that question can, however, be easily adapted to your needs. – NPE Nov 11 '14 at 21:30

2 Answers2

5

You can add whatever function you like via the *panel args. For example:

set.seed(8092)
X <- matrix(rnorm(80), ncol=4)

panel.qq <- function(x, y, ...) {
  usr <- par("usr"); on.exit(par(usr))
  par(usr = c(0, 1, 0, 1), new = TRUE)
  qqplot(x, y, xlab = deparse(substitute(x)), ylab = deparse(substitute(y)))
  abline(c(0,1), ...)
}

pairs(X, lower.panel = panel.qq)

enter image description here

Of course you can use panel=panel.qq to make all the squares qq plots

rawr
  • 20,481
  • 4
  • 44
  • 78
2

I got an error with just putting qqplot in the panel function which it appears @rawr solved by using 'new=TRUE'. My approach was to pull out the x and y values and use points:

panel.qq <- function(x,y, ...)
{    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(usr[1:2], -2,2) )
    QQ <- qqplot(x,y, plot.it = FALSE)
    x <- QQ$x; 
    y <- QQ$y; 
    points(x, y, col = "cyan", ...)
    abline(0,1)
}

set.seed(8092)
X <- matrix(rnorm(80), ncol=4)
pairs(X, upper.panel=panel.qq)

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487