[R] How to draw a rect() behind a hist() ?
ehlers at ucalgary.ca
Wed Dec 1 04:42:39 CET 2010
On 2010-11-30 17:27, Jason Edgecombe wrote:
> I have the following code:
> hist(gps$heartpercent, breaks=5)
> rect(90, par("usr"), 100, par("usr"), col = "red")
> How do I get the rectangle to appear behind the histogram. Barring that,
> how can I make certain bars of the histogram to be a certain color?
Here are a couple of ways:
1. using hist(); just plot the histogram twice.
x <- rnorm(1000, 100, 5)
rect(90, 0, 98, par('usr'), 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;
Again, you can define bar colours with a colour vector.
Lattice is more customizable albeit a little harder
More information about the R-help