r - 识别直方图中的点
问题描述
在 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
推荐阅读
- macos - 左右两侧的 OSX High Sierra 码头
- python - 无法使用 cx_Freeze 编译
- ios - 如何使用 IOS 12 在 UITableViewCell 中正确添加 UICollectionView
- angular - Angular 6中的过滤方法返回空数组
- r - R Shiny-动态文件输入标签
- android-layout - ScrollView 与 RelativeLayout 中的其他内容重叠
- prolog - SWI-Prolog Association List Library doesn't appear to work
- javascript - 如何等到在 CasperJS 中完成评估步骤?
- vba - 没有错误,但宏使用 F8 逐行工作,而不是在执行完整宏时 - excel、vba
- c# - 如何在 C# 中组合通用函数的列表?