[R] histogram y-scaling

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Apr 19 16:15:30 CEST 2004


?hist reveals argument plot=TRUE, so try plot=FALSE.

On Mon, 19 Apr 2004, Randy Zelick wrote:

> Hello all,
> 
> Relative to WinXP & R1.8....

No such thing.  There is R 1.8.0 and R 1.8.1 but not R 1.8.

> I have two histograms to plot, and for comparison purposes I want them to
> have the same Y-scaling. I tried to find the size of the bin with the
> maximum count before generating the histogram, but this did not work (see
> below). What is a better way?
> 
> par(mfrow=c(2,1)) # set up for plotting in 2 rows and 1 column
> 
> x1<-seq(-0.5,58.5,1) # make a range of x values for histogram
> 
> I thought the following lines would allow me to capture the results of the
> hist function and determine the max bin count for scaling *before* making
> the plot, but R cleverly saw around my method and plots it anyway. With
> this code I get two plots.
> 
> q=hist(mt1,x1)       # stick results in a variable... alas also plots
> cts=q$counts         # get the bin counts
> mct1=max(cts)        # how many values in the bin with the most values
> hist(mt1,x1)         # generate histogram plot
> 
> # go on with histogram #2...

Something like
q1 <- hist(mt1, x1, plot = FALSE)
q2 <- hist(mt2, x2, plot = FALSE)
mctl <- max(q1$counts, q2$counts)
plot(q1, ylim=c(0, mctl))
plot(q2,  ylim=c(0, mctl))


-- 
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