Skip to content
Prev 243548 / 398500 Next

How to draw a rect() behind a hist() ?

On 2010-11-30 17:27, Jason Edgecombe wrote:
Here are a couple of ways:
1. using hist(); just plot the histogram twice.

  x <- rnorm(1000, 100, 5)
  hist(x)
  rect(90, 0, 98, par('usr')[4], col = 'red')
  hist(x, add = TRUE)

For coloured bars, use a colour vector:

  hist(x, breaks = 5, col = c(3,3,4,4,2))

2. using lattice;

  histogram(x,
   panel=function(...){
     panel.rect(90,0,98,1000,col='bisque',border=NA)
     panel.histogram(...,col='transparent',lwd=2)
   }
  )

Again, you can define bar colours with a colour vector.
Lattice is more customizable albeit a little harder
to learn.

Peter Ehlers