[R] scaling y-axis to relative frequency in multiple histogram (multhist)

Ben Bolker bolker at zoo.ufl.edu
Thu Nov 30 17:09:32 CET 2006


ahimsa campos-arceiz wrote:
> Dear Ben,
> 
> thank you very much for your prompt reply.
> 
> but I'm afraid I'm missing something: when I apply your function
> pmulthist I
> obtain exactly the same results as with multhist (with the y axis
> representing frequencies rather than a probability).
> 
> I was checking but couldn't find the problem (*"allhist" is clearly set to
> be freq=False*)
> 
> I would appreciate a lot if you can point out what I'm missing.
> 
> Ahimsa
> 

  Sorry, I wasn't thinking.  The freq=FALSE/prob=TRUE switches
in hist() only affect the plotting of the data, not the
calculations.  In the second line of the function below
I've replaced $counts with $density, which does the trick.
I could/should add this to the function in the packages ...

pmulthist <-
function (x, breaks = "Sturges", ...)
{
  allhist <- hist(unlist(x), breaks = breaks, plot = FALSE)
  combhist <- t(sapply(x, function(z) hist(z, breaks = allhist$breaks,
      plot = FALSE)$density))
  barplot(combhist, beside = TRUE, names = signif(allhist$mids,
      2), ...)
}

  cheers
    Ben


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : https://stat.ethz.ch/pipermail/r-help/attachments/20061130/6ce15005/attachment.bin 


More information about the R-help mailing list