Skip to content

Instantly share code, notes, and snippets.

@DSuveges
Last active November 27, 2017 16:25
Show Gist options
  • Save DSuveges/caf89e96e86113315625e08b7344264c to your computer and use it in GitHub Desktop.
Save DSuveges/caf89e96e86113315625e08b7344264c to your computer and use it in GitHub Desktop.
A small collection of R snippets that I found useful
##
## Based on a trait name and the corresponding p-values, a QQ plot is generated with a
## marked genomic inflation factor.
##
QQ_plot = function(pvals, trait){
mlogpv = -log10(pvals); # Observed data
mlogpv = mlogpv[! is.na(mlogpv)] # Remving Na-s
mlogexp = -log10(qunif(ppoints(length(pvals)))); # Generating expected data
# Initialize empty plot:
plot(-5,-5,xlim=c(0,max(mlogexp)),ylim=c(0,max(mlogpv)),
xlab=expression(paste('Theoretical ', -log[10], '(p)')),
ylab="",
main=trait, cex.lab = 1.3);
# equality line:
abline(a= 0, b =1 , col='firebrick');
# Adding pvalues:
points(sort(mlogexp),sort(mlogpv),pch=19,cex=0.5,col='navy');
# Adding y label:
title(ylab=expression(paste('Observed ', -log[10], '(p)')),
line=2, cex.lab=1.2)
# Calculating genomic infation factor:
chisq <- qchisq(1-pvals,1)
lambda = round(median(chisq)/qchisq(0.5,1), digits=3);
textColor = if(lambda <= 1.2 ) "black" else "red"
# Report lambda:
text(par('usr')[1] + diff(par('usr')[1:2])/10,
par('usr')[4] - diff(par('usr')[3:4])/10,
bquote(lambda*'='~.(lambda)),
cex = 2, pos = 4, col = textColor);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment