[R] Plot histograms in a loop

arun smartpink111 at yahoo.com
Tue May 28 01:02:24 CEST 2013


Hi,
Try either:
set.seed(28)
stats1<- as.data.frame(matrix(rnorm(5*10000),ncol=5))

pdf(paste("test",1,".pdf",sep=""))
par(mfrow=c(2,1))
lst1<- lapply(names(stats1),function(i) {hist(stats1[,i],100,col="lightblue",main=paste0("Histogram of ",i),xlab=i );qqnorm(stats1[,i])})
dev.off()

#or

pdf(paste("test1",1,".pdf",sep=""))
par(mfrow=c(2,1))
for(colName in names(stats1)){
hist(stats1[,colName],100,col="lightblue",xlab=colName,main=paste0("Histogram of ",colName))
qqnorm(stats1[,colName])
}
dev.off()

A.K.


>I have a dataset with more than 50 columns, and I need to check 
distribution for each variable. The idea was to plot histograms and qq 
plots for each >of them and check if distribution is normal. I tried 
something like this: 
>
>for(colName in names(stats)){ 
 >   pdf(paste("test",1,".pdf",sep="")) 
 >   hist(stats$get(colName)) 100, col="lightblue") 
 >   qqnorm(stats$get(colName))     
>} 
>dev.off() 
>
>but that doesn't work. It would be great if I could also manage 
to store all of them in one file, what I think this code should do... 
>
>Thanks, 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test11.pdf
Type: application/pdf
Size: 351753 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130527/ec3d88af/attachment.pdf>


More information about the R-help mailing list