[R] adding statistical output to a plot

David Winsemius dwinsemius at comcast.net
Sat May 29 14:31:14 CEST 2010


On May 29, 2010, at 8:11 AM, dunner wrote:

>
> Thanks to both of you,
>
> I used the below code instead, which has been cleaned and pruned a  
> little.
> It produces a trellis of qq plots with the proper annotations as per  
> Minitab
> from a dataframe passed to mtab.matrix(). However, I can't get  
> individual
> variable names (these are column names in the passed dataframe) to  
> annotate
> the plots. Perhaps "apply" simply doesn't deal with the colname and  
> I have
> to try a completely different method?

apply takes a column and passes it as a vector. I think it still has a  
name but perhaps not a colname. Getting the name of a passed object  
sometimes requires working with substitute(deparse(x)). Not sure what  
paste() would be doing when you are only processing one vector at a  
time.

> Or will apply pass enough information
> to the included function (mtab.norm) to allow me to annotate the plot?
> Again, I'm probably missing something banal and wholly obvious.
>
>
> mtab.norm<-function(x){
> library(nortest)
> library(lattice)
> x<-as.numeric(x)
> x<-as.vector(x)
> plot.ht<-4.6
> plot.wd<-4.6
> pt.ht=plot.ht/7
> txt.sz<-(plot.ht/5)
> qqnorm(x, pch=16, cex=pt.ht, col="darkorange",  
> xlab=paste(colnames(x)),
> ylab='', main='')
> qqline(x, col="black", lty=2)
> grid(col = "darkgrey", lty=2)
> st.x<-shapiro.test(x)
> mtext(paste("Shapiro-Wilks", "\n p =",signif(st.x 
> $p.value,digits=4),"\n W
> =",signif(st.x$statistic,digits=4)), line=1, cex=txt.sz, adj=0)
> adt.x<-ad.test(x)
> mtext(paste("Anderson-Darling", "\n p =",signif(adt.x 
> $p.value,digits=4), "\n
> W =", signif(adt.x$statistic,digits=4)), line=1, cex=txt.sz, adj=1)}
>
> mtab.matrix<-function(z){
> pdf(file=paste(colnames(z)[1],".pdf"), paper="a4r", width=0, height=0)
> par(mfcol=c(3,3), mar=c(5.1,4.1,5.1,2.1))
> par(bg="white")
> apply(z, 2, mtab.norm)
> dev.off()}
>
>
> Thanks again for all your help. I am obviously a relative beginner,  
> but an
> enthusiast!
>
> Dr. Ross Dunne,
> HRB Clinical Research Fellow,
> Dept. of Psychiatry, Trinity College Dublin.
>
> -- 
> View this message in context: http://r.789695.n4.nabble.com/adding-statistical-output-to-a-plot-tp2235565p2235629.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list