最近在读<Modern Applied Statistics With S-PLUS>,
115页讲到Q-Q图时,书中给出了一个Trellis的实现。
Q-Q图很简单,把样本数据和理论分布算出来的quantiles,画个散点图而已。
分别用base graph和ggplot2实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | qqplot <- function(y, distribution=qnorm) { x <- distribution(ppoints(y)) plot(x, sort(y), xlab="Theoretical Quantiles", ylab="Sample Quantiles", main="Normal Q-Q Plot" ) lines(y,y) } qqplot2 <- function(y, distribution=qnorm) { require(ggplot2) x <- distribution(ppoints(y)) d <- data.frame">data.frame(x=x, y=sort(y)) p <- ggplot(d, aes(x=x, y=y)) + geom_point() + geom_line(aes(x=x, y=x)) + opts(title="Normal Q-Q Plot") + xlab("Theoretical Quantiles") + ylab("Sample Quantiles") return(p) } |


0 Comments.