Skip to content

Histograms with bin proportions on the y-axis

3 messages · Nick Gayeski, R. Michael Weylandt, (Ted Harding)

#
To open a graphics device, you usually use a function like png() or
eps() then do your plotting then dev.off() to close it when you're
done.

Sounds like you need something like hist(x, breaks = 100, freq = TRUE)

Hope that helps,

Michael

On Sun, May 20, 2012 at 3:07 PM, Nick Gayeski
<nick at wildfishconservancy.org> wrote:
#
See in-line below.
On 20-May-2012 19:07:55 Nick Gayeski wrote:
The following illustrates how to "customise" the behaviour of
hist() to achieve a desired effect (such as the one you want):

  N <- 500000   ## sample size
  Y <- rnorm(N,mean=400000,sd=100000)  ## the sample
  ## Now make a non-plotted object which contains the histogram info:
  H0 <- hist(Y,breaks=100,plot=FALSE)  ## the bin values are counts
  C <- H0$counts ## extract the counts
  ## Now convert these to proportions (percent or fraction as you wish):
  P <- 100*C/N  ## or P <- C/N for fractional proportions
  ## Now copy the histigram object H0 to H1 and modify H1:
  H1 <- H0 ; H1$counts <- P  ## replaces the "counts" by the proportions
  ## Now plot it:
  plot(H1,ylab="% proportions in bins")
As Michael Weylandt has suggested, you can encapsulate the final
plot() command between the opening of a graphics device and the
closure of the device. So if you want an EPS file you can use the
postscript device:

  postscript("myhistoplot.eps",horizontal=FALSE)
  plot(H1,ylab="% proportions in bins")
  dev.off()

and the result will be an EPS file "myhistoplot.eps". You will
probably want to at least vary the aspect ratio (height:width)
and maybe some other things, so give the command

  ?postscript

to find out what the various (and many .. ) options are.

Hoping this helps to get you started!
Ted.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 20-May-2012  Time: 22:27:13
This message was sent by XFMail