[R] Generating a vector for breaks in a histogram

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jul 3 16:00:07 CEST 2003


On Thu, 3 Jul 2003, michael watson (IAH-C) wrote:

> Fantastic.  You're right, I was looking for seq().
> 
> However, my plan for using it for hist() was foiled!  
> 
> I thought if I did something like:
> 
> > b <- seq(0,500,10)
> > hist(myvble,breaks=b)
> 
> It would bin myvble into the bins 0-50,50-100,100-150 etc and in that way I could ensure that two histograms are on the same scale with the same bins!
> 
> I get the following error:
> 
> Error in hist.default(Cy5, breaks = s) : some 'x' not counted; maybe 'breaks' do not span range of 'x'
> 
> Now this makes sense of course, my bins probably DON'T span the entire
> range of X.  SOOOOO I am still left with the same problem:
> 
> 1) two variables
> 2) I want to draw histograms of both
> 3) I want them to have the SAME x-y scale on the graph
> 4) I want them to have the SAME bin range
> 
> How do i do it?  Any suggestions?

SOOOOO choose a range of breaks to span the whole range of x!

Hint: hist(c(x1,  x2)) knows how to do it, and may even provide you a
suitable vector of breaks in its return value.

> Cheers
> Mick
> 
> -----Original Message-----
> From: Hotz, T. [mailto:th50 at leicester.ac.uk]
> Sent: 03 July 2003 14:16
> To: michael watson (IAH-C); r-help at stat.math.ethz.ch
> Subject: RE: [R] Generating a vector for breaks in a histogram
> 
> 
> Dear Mick,
> 
> Have a look at ?seq - seq(1,20,length=20) should do it.
> 
> HTH
> 
> Thomas
> 
> ---
> 
> Thomas Hotz
> Research Associate in Medical Statistics
> University of Leicester
> United Kingdom
> 
> Department of Epidemiology and Public Health
> 22-28 Princess Road West
> Leicester
> LE1 6TP
> Tel +44 116 252-5410
> Fax +44 116 252-5423
> 
> Division of Medicine for the Elderly
> Department of Medicine
> The Glenfield Hospital
> Leicester
> LE3 9QP
> Tel +44 116 256-3643
> Fax +44 116 232-2976
> 
> 
> > -----Original Message-----
> > From: michael watson (IAH-C) [mailto:michael.watson at bbsrc.ac.uk]
> > Sent: 03 July 2003 14:02
> > To: 'r-help at stat.math.ethz.ch'
> > Subject: [R] Generating a vector for breaks in a histogram
> > 
> > 
> > Hi
> > 
> > I have two lots of numbers which I would like to histogram 
> > using the hist() function.  For comparative reasons, I want 
> > them to be on the same scale, which I can use the xlim and 
> > ylim options to achieve.
> > 
> > However, having them on the same scale is meaningless unless 
> > they have the same "breaks".  Consulting the documentation, 
> > there are 4 ways of defining the number of breaks, only one 
> > of which is "definite", the others merely form suggestions, 
> > which I have found is not good enough.
> > 
> > The only definite way is to provide a vector to the hist() 
> > function which is a vector of the break points for the 
> > histogram.  So I need to generate a vector that contains say, 
> > 500, numbers in it, equi-distance apart between a min and a max.  EG:
> > 
> > > myfunc(n=20,min=1,max=20) 
> > 
> > would provide a vector, length 20, with the numbers 1 through 
> > 20 in it.
> > 
> > Is there a function in R that can do this?
> > 
> > Thanks
> > Mick
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list