【问题标题】:How do I highlight an observation's bin in a histogram in R如何在 R 的直方图中突出显示观察值的 bin
【发布时间】:2010-01-24 17:09:28
【问题描述】:

我想从多个观察值(即 d

我如何在 R 中做到这一点?

【问题讨论】:

标签: r histogram


【解决方案1】:

扩展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, ...)
}

现在

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

将以红色突出显示包含 1.2 的 bin。

【讨论】:

    【解决方案2】:
    x = rnorm(100)
    hist(x,br=10,col=c(rep(0,9),1))
    

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

    谢谢

    危险状态

    【讨论】:

    • 我会这样做。请注意,对于红色和蓝色等典型颜色,您可以输入字符串而不是数字:col=c("red", "blue", 9)
    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多