r - 识别直方图中的点

标签 r histogram interactive

在 R 中,我尝试使用鼠标以交互方式识别直方图中的 bin 值。我想我需要相当于散点图的identify()函数的东西。但identify()似乎不适用于直方图。

最佳答案

使用locator()查找点,然后查找该值位于哪个区间,确保它小于条形的 y 值,然后返回计数:

set.seed(100)
h <- hist(rnorm(1:100))

# use locator() when doing this for real, i'm going to use a saved set of points
#l <- locator()

l <- list(x = c(-2.22, -1.82, -1.26, -0.79,-0.57, -0.25, 0.18, 0.75, 
0.72, 1.26), y = c(1.46, 7.81, 3.79, 9.08, 17.11, 11.61, 15, 
17.96, 5.9, 3.37))

# for debugging purposes - the nth value of the output should match where
# the n value is shown on the histogram
text(l, labels=1:10, cex=0.7, font=2)

fi <- findInterval(l$x, h$breaks)
sel <- (l$y > 0)  & (l$y < h$counts[fi])
replace(h$counts[fi], !sel, NA)

#[1]  3 NA  9 14 NA 22 20 NA 13  7

enter image description here

关于r - 识别直方图中的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67596891/

相关文章:

r - 使用 read_excel(na = ) 如何指定多个 NA 字符串?

r - 固定集合中的随机序列,每个序列至少包含一个字符

r - annotation_custom 与 ggplot2 中的 npc 坐标

r - 如何从boxplot()生成的图中删除默认轴?

data-visualization - Vegalite 多直方图

wolfram-mathematica - 如何在约束下以图形方式定义参数值?

opencv - 从 OpenCV 直方图中获取值

python - Pandas 直方图 : plot histogram for each column as subplot of a big figure

python - 在 Python 脚本中检查交互式 shell

python - 如何将语句写入两行?