Skip to content

Instantly share code, notes, and snippets.

@stephenturner
Created April 5, 2013 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenturner/5321887 to your computer and use it in GitHub Desktop.
Save stephenturner/5321887 to your computer and use it in GitHub Desktop.
create qq plot where you can specify the upper limit on the y-axis.
qq = function(pvector, ymax=NA, ...) {
if (!is.numeric(pvector)) stop("D'oh! P value vector is not numeric.")
pvector <- pvector[!is.na(pvector) & pvector<1 & pvector>0]
o = -log10(sort(pvector,decreasing=F))
e = -log10( ppoints(length(pvector) ))
if (!is.numeric(ymax) | ymax<max(o)) ymax <- max(o)
plot(e,o,pch=19,cex=1, xlab=expression(Expected~~-log[10](italic(p))), ylab=expression(Observed~~-log[10](italic(p))), xlim=c(0,max(e)), ylim=c(0,ymax), ...)
abline(0,1,col="red")
}
# automatically set ymax
qq(runif(1000))
# if you specify a ymax that's below what it should have been, it will still automatically scale
qq(runif(1000), ymax=1)
# if you specify ymax above max observed -logp, the y axis will expand
qq(runif(1000), ymax=10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment