[R] how to make a histogram with non-numeric x's?

Jim Lemon jim at bitwrit.com.au
Wed Mar 24 10:30:29 CET 2010


On 03/24/2010 04:36 PM, cbarcelo wrote:
>
> Hello,
> I need to make a histogram with variable x bin ranges. For example my bins
> are 0-15, 15-25, 25-40 etc. Any suggestions?
> Thanks!
>
Hi cbarcelo,
You can just do this:

x<-sample(0:40,100,TRUE)
hist(x,breaks=c(0,15,25,40))

which will produce densities, or:

hist(x,breaks=c(0,15,25,40),freq=TRUE)

which will display counts, but complain about it, or:

barplot(hist(x,breaks=c(0,15,25,40))$counts,
  names.arg=c("(0-15]","(15-25]","(25-40]"))

which may be what you want.

Jim



More information about the R-help mailing list