qqline in ggplot2?

595 views
Skip to first unread message

jrandall

unread,
May 1, 2009, 9:40:49 PM5/1/09
to ggplot2
I am making a QQ plot using the following code:

>ggplot(data, na.rm=T) + geom_point(aes(sample=residual), stat="qq",distribution=qnorm)

How can I add a line to this plot that passes through the first and
third quartiles (such as can be done with qqline() on a qqnorm()
plot)?

Mike Lawrence

unread,
May 2, 2009, 7:50:37 PM5/2/09
to jrandall, ggplot2
Compute the slope & intercept beforehand:

slope = (quantile(data$residual,p=.75)-quantile(data$residual,.25))/(qnorm(.75)-qnorm(.25))
intercept = quantile(data$residual,.25) - slope*qnorm(.25)
qq_line = data.frame(intercept=intercept,slope=slope)

Then:

ggplot(data, na.rm=T) +
geom_point(aes(sample=residual), stat="qq",distribution=qnorm) +
geom_abline(
data = qq_line
,aes(
intercept = intercept
,slope=slope
)
)
--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tr.im/mikes_public_calendar

~ Certainty is folly... I think. ~
Reply all
Reply to author
Forward
0 new messages