Last active
August 29, 2015 14:10
-
-
Save docsteveharris/b36b908d7cb4de9b52b4 to your computer and use it in GitHub Desktop.
qqplot in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for sharing; fairly obvious, but might be helpful for some to include
at the beginning.