Skip to content

y axis on hist

8 messages · Bud Gibson, Daniel E. Ho, Peter Dalgaard +2 more

#
Hi:

The y axis on the hist function seems to set its limits oddly. 
sometimes, it covers the full range of the data and sometimes it stops 
one major tick short.  I have had this behavior with a variety of data 
sets, and it can easily be reproduced by just running the following 
several times:

hist(rnorm(100000))

I have tried explicitly setting ylim to the range of values produced by 
rnorm (taking care to set some variable to rnorm(100000) and then 
graphing that), and I still get the y axis plotting behvior I just 
described.

TIA,
Bud
#
From: "Bud Gibson" <fpgibson at umich.edu>
I'm not sure I understand the problem correctly.  Doesn't the following seem
to correct the y-axis problem?

hist(rnorm(100000),ylim=c(0,25000))

Dan
daniel_ho at harvard.edu
#
Bud Gibson <fpgibson at umich.edu> writes:
I don't think this is stranger than the axes on any other plots. It's
just that the bounding box isn't printed on histograms. Try adding a
box() and you'll see what the issue is. To ensure that the last axis
label is "over the top", it is not enough to diddle the ylim to the
range of barheights; you'll need to ensure that the ylim is also a
pretty value, something like this:

z <- rnorm(100000)
h <- hist(z,plot=F)
plot(h,ylim=range(pretty(range(0,h$counts))))
#
Let me try to clarify.  I would like an automated way to set the y axis 
to cover the largest count (or density) in one of the automatically 
computed bins.  I thought hist did this.  Working from Dalgaard's book 
example on writing a function, it would seem I could do something like:

y <- rnorm(100000)
h <- hist(y)
ylim <- range(0,max(h$counts))
hist(y,ylim=ylim)

and be assured that the y axis covered the data points.  However, it 
does not, and the behavior does not seem entirely systematic to me.

Bud
Daniel E. Ho wrote:

            
#
Thanks!  I wondered if something like this was not going on, but could 
not figure out how to get to the root of it.

BTW, your book is very good.  Have you considered writing an intro or 
intermediate programming R that would provide almost some cookbook 
examples, much as O'Reilly has done with its Perl or Java series?
Peter Dalgaard BSA wrote:

            
#
Bud Gibson <fpgibson at umich.edu> writes:
Interesting idea, but I'm not sure I'm the one to write it. (For the
uninitiated, the Perl Cookbook is an 800 pp. with 20 chapters, each
containing an introduction plus 15-20 "recipes" in a strict 

 Problem
 Solution
 Discussion
 See Also

layout.)

Paul Johnsons "Rtips" page on
http://lark.cc.ukans.edu/~pauljohn/R/statsRus.html
is actually quite a long way along the road to being a document of
that kind. A little editing, a careful check that all of the solutions
really are The Best Way, and (the hard part) ensuring that there is
sufficient coverage of what readers wanted to know.
#
On 12 Dec 2002, Peter Dalgaard BSA wrote:

            
This sort of thing is exactly what Sweave and the Vignette mechanism are
good for.

	-thomas
#
Hi

Note also that box() can take a bty argument (see help(par)) so you
don't have to draw all four sides. For example ...

	hist(rnorm(100000))
	box(bty="l")

Paul
Peter Dalgaard BSA wrote: