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

Ben Bolker bolker at zoo.ufl.edu
Thu Nov 30 13:40:08 CET 2006


ahimsa campos-arceiz <ahimsa <at> camposarceiz.com> writes:

> 
> Hi,
> 
> I'm plotting a multiple histogram using the function multhist {package
> plotrix}, something like:
> 
> library(plotrix)
> mh <- list(rnorm(200, mean=200, sd=50), rnorm(200, mean=250, sd=50))
> multhist(mh)
> 
> In this graph y-axis represents the frequency of observations.... but I
> would like it to be scaled into relative frequencies,
> 
> does anybody know how to do this with multhist or similar function?
> 
> thanks a lot,
> 
> Ahimsa
> 

   In its current form, multhist doesn't allow the
freq=FALSE or prob=TRUE arguments to be passed to
hist() (the ... argument passes optional arguments
to barplot, not hist).  You could hack it as follows:

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



More information about the R-help mailing list