如何在R的直方图中突出显示观察点的bin [英] How do I highlight an observation's bin in a histogram in R

查看:99
本文介绍了如何在R的直方图中突出显示观察点的bin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从多个观测值创建直方图(即d <-c(1,2.1,3.4,4.5)),然后突出显示特定观测值所在的bin,这样我就得到了输出看起来像这样:

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:

如何在R中执行此操作?

how do I do this in R?

推荐答案

下面是扩展危险统计的答案的一个小函数,它将自动查找哪个bin包含要突出显示的值:

Expanding on dangerstat's answer, here is a little function that will automatically find which bin contains the value that 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, ...)
}

现在

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

将以红色突出显示其中带有1.2的垃圾箱.

will highlight the bin with 1.2 in it in red.

这篇关于如何在R的直方图中突出显示观察点的bin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆