6

Since hist() of the base R does not report percentages (and the freq=FALSE) does not help either, I turned to lattice.

histogram(rnorm(10000))

Please help me with the following:

  1. How can I get rid of the box arround the plot?
  2. How can I seperately define the cex of the x/y labels and x/y axis?
  3. How can I provide custom names to x and y axis?
Andrie
  • 176,377
  • 47
  • 447
  • 496
ECII
  • 10,297
  • 18
  • 80
  • 121
  • Can you elaborate on what your trouble with `hist` is, just out of curiosity? When I specify `freq = FALSE` I get a density (i.e. percentages) histogram. – joran Jan 31 '12 at 15:28
  • 1
    Hi @joran. To see the issue ECII's likely referring to, try `hist(runif(1000), freq=FALSE, col=gray(.6))`. The y-axis does correctly note the probability density, but the probability mass with each bar (the product of the density times the width), should be around a tenth of the density, since there are 10 bars... So I *think* ECII is asking for axis ticks that index the percentage of the observations falling within each bar. – Josh O'Brien Jan 31 '12 at 17:28

2 Answers2

6

Or, if you want to stick with hist(), you can modify it slightly, as shown below.

This function calls hist() once to get its return value, which is an object containing all sorts of useful information about the structure of the histogram. It then uses (a) the width of the bins and (b) the density for each bar to calculate (c) the percentage of the observations falling in each bar.

histPercent <- function(x, ...) {
   H <- hist(x, plot = FALSE)
   H$density <- with(H, 100 * density* diff(breaks)[1])
   plot(H, freq = FALSE, ...)
}

histPercent(rnorm(10000), col="dodgerblue", las=1,
            xlab="Echs-axis", ylab="Why-axis")

enter image description here

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • Fantastic. I thought it wouldn't be possible with base. However I must give it to Andrie since the question specified lattice. Thank you very much. – ECII Jan 31 '12 at 17:33
  • @ECII -- Yeah, I find it amusing that there's the name of a baseball team hardwired into R. Also, thanks for the question. I've had to explain "why the percentages in that histogram don't add up" enough times that I know just where you're coming from. – Josh O'Brien Jan 31 '12 at 17:39
  • Ah, I see now what you meant. – joran Jan 31 '12 at 17:56
5

This should get you started:

library(lattice)
histogram(rnorm(10000),     
    main=list(
        label="Main plot title",
        cex=1.5),
    xlab=list(
        label="Custom x-axis label",
        cex=0.75),
    ylab=list(
        label="My very own y-axis label",
        cex=1.2),
    scales=list(cex=0.5),
    par.settings = list(axis.line = list(col = 0))
)

enter image description here

Andrie
  • 176,377
  • 47
  • 447
  • 496