如何在R中的直方图中突出显示观察区域

时间:2022-06-05 22:44:55

I want to create a histogram from a number of observations (i.e. d <- c(1,2.1,3.4,4.5) ) and then highlight the bin that a particular observation falls in, such that I have an output that looks like this: alt text http://img686.imageshack.us/img686/5061/observationhist.png

我想从许多观察中创建一个直方图(即d < - c(1,2.1,3.4,4.5)),然后突出显示特定观察所在的bin,这样我的输出看起来像这样: alt text http://img686.imageshack.us/img686/5061/observationhist.png

how do I do this in R?

我怎么在R?

2 个解决方案

#1


7  

Expanding on dangerstat's answer, here is a little function that will automatically find which bin contains the value that you want to highlight:

扩展dangerstat的答案,这里有一个小函数,它会自动找到哪个bin包含你想要突出显示的值:

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 the bin with 1.2 in it in red.

将突出显示其中1.2为红色的bin。

#2


5  

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

Clearly this will color the last column so tweak the col= bit for your needs

显然,这将为最后一列着色,因此根据您的需要调整col = bit

Thanks

dangerstat

#1


7  

Expanding on dangerstat's answer, here is a little function that will automatically find which bin contains the value that you want to highlight:

扩展dangerstat的答案,这里有一个小函数,它会自动找到哪个bin包含你想要突出显示的值:

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 the bin with 1.2 in it in red.

将突出显示其中1.2为红色的bin。

#2


5  

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

Clearly this will color the last column so tweak the col= bit for your needs

显然,这将为最后一列着色,因此根据您的需要调整col = bit

Thanks

dangerstat