Skip to content

Instantly share code, notes, and snippets.

@docsteveharris
Last active August 29, 2015 14:10
Show Gist options
  • Save docsteveharris/b36b908d7cb4de9b52b4 to your computer and use it in GitHub Desktop.
Save docsteveharris/b36b908d7cb4de9b52b4 to your computer and use it in GitHub Desktop.
qqplot in R
# Test data: 1000 patients with two different age distributions
tdt <- data.table(age.old=rnorm(1000, mean=65, sd=15), age.young=rnorm(1000,mean=55,sd=10))
# Function to compare two distributions using qplot
qqplot <- function(x,y, data=wdt, n=100) {
lab.x = x
lab.y = y
x <- with(data, get(x))
x <- sapply(seq(0,1,1/n), function(q) quantile(x, q, na.rm=TRUE))
y <- with(data, get(y))
y <- sapply(seq(0,1,1/n), function(q) quantile(y, q, na.rm=TRUE))
# Get axes symmetrical
axes.minmax <- c(floor(min(c(x,y))), ceiling(max(c(x,y))))
# print(axes.minmax)
qplot(x,y, asp=1, xlab=lab.x, ylab=lab.y) +
geom_abline(intercept=0,slope=1) +
coord_cartesian(x=axes.minmax, y=axes.minmax)
}
qqplot('age.young', 'age.old', data=tdt)
@timelyportfolio
Copy link

thanks for sharing; fairly obvious, but might be helpful for some to include

library(data.table)
library(ggplot2)

at the beginning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment