I've run the code for the two answers above, and the plots do not look the same, because the R qqplot function applies a transformation to the data.

The code on this page generates a qqplot like the one in the example, although it's using lattice, not ggplot http://genome.sph.umich.edu/wiki/Code_Sample:_Generating_QQ_Plots_in_R

I was trying to work out how to calculate and plot the 95%CI on ggplot a while ago. The code is below but there's clearly something wrong with the plotted interval values.

```
p.values = rnorm(100, mean = 0.5, sd = 0.1)
obs1 = -log10 (sort(p.values))
exp1 = -log10 (1:length (obs1)/length (obs1))
r1 = tbl_df (data.frame(obs1, exp1))
c95 = rep(0,length(exp1))
c05 = rep(0,length(exp1))
## the jth order statistic from a uniform(0,1) sample has a beta(j,n-j+1) distribution (Casella & Berger, 2002)
for(i in 1:length(exp1)){
c95[i] <- qbeta(0.95,i,length(exp1)-i+1)
c05[i] <- qbeta(0.05,i,length(exp1)-i+1)
}
r1 = cbind(r1, c05, c95)
ggplot (r1, aes(obs1, exp1)) +
geom_point() +
geom_abline(intercept = 0, slope = 1, alpha = .5) +
geom_smooth(method = "lm", se = FALSE) +
geom_ribbon(aes (ymin = c05, ymax = c95, alpha = 0.5)) +
theme_bw()
```