How to highlight the observation bin in a histogram in R

I want to create a histogram from a series of observations (i.e. d <- c (1,2.1,3.4,4.5)), and then select the cell into which a specific observation falls, so that I have a conclusion that looks like this : alt text

How do I do this in R?

+7
r histogram
source share
2 answers

Expanding the response to danger, here is a small function that will automatically detect which bin contains the value you want to highlight:

highlight <- function(x, value, col.value, col=NA, ...){ hst <- hist(x, ...) idx <- findInterval(value, hst$breaks) cols <- rep(col, length(hst$counts)) cols[idx] <- col.value hist(x, col=cols, ...) } 

Now

 x <- rnorm(100) highlight(x, 1.2, "red") 

will highlight a cell with 1.2 in it in red.

+7
source share
 x = rnorm(100) hist(x,br=10,col=c(rep(0,9),1)) 

Obviously, the color will be colored in the last column to adjust col = bit for your needs.

thanks

dangerstat

+5
source share