答案 0 :(得分:5)
假设您正在使用library(hexbin)
中的hexbin函数,您可以使用bin ID来达到您想要的效果。
将该函数调用为hexbin(..., IDs = T)
,结果将显示一个字段,告诉您这些点落在哪个bin中。
工作示例:
library(hexbin)
x <- c(1, 1.2, 1, 3, 5, -2 ,1, 0, 0.8)
y <- c(1, 1, 0, -1, 0, 2, -1, 1, 1)
h <- hexbin(x, y, xbins = 3,IDs = T)
#what is the cell ID of point 1?
ID1 <- h@cID[1]
#how many points fall in that cell?
sum(h@cID == ID1) #answer is 4 in this case
答案 1 :(得分:0)
get_count <- function(x, y, h) {
my_dist <- function(x2, y2) {
return(sqrt((x - x2) ^ 2 + (y - y2) ^ 2))
}
distances <- mapply(my_dist, attr(h, 'xcm'), attr(h, 'ycm'))
return(attr(h, 'count')[which.min(distances)])
}