Skip to content
Prev 241148 / 398500 Next

par mfrow in "function" problem

Dear Casper,

This is because you create two histograms, the first with the direct
call to hist(), the second at: h = hist(x).  That is also why even
though you set the xlab and main to be blank in your first one,the
histogram with the normal line added actually was titled.  Normally,
hist() just overwrites the plot in the current device, but since there
are 4 slots in the device (par(mfrow = c(2, 2))), you then saw
multiple histograms.  You can avoid this by setting the plot = FALSE
argument in the histogram you do not want plotted.  Here is how I
would revise it:

par(mfrow=c(2,2))
#############################
myhist=function(x){
       hist(x,xlab="",main="")
       h=hist(x, plot = FALSE)
       xfit=seq(min(x),max(x),length=100)
       yfit=dnorm(xfit,mean(x),sd=sd(x))
       yfit=yfit*diff(h$mids[1:2])*length(x)
       lines(xfit, yfit, col="blue", lwd=2)
}
#############################

## an example
myhist(rnorm(400))


HTH,

Josh
On Wed, Nov 10, 2010 at 11:31 AM, casperyc <casperyc at hotmail.co.uk> wrote: