r - 查找 hexbin 对象之间重叠的六边形 bin 的坐标
问题描述
我有两个空间数据集,其坐标指示物种的观察结果,并希望估计这些数据集之间的重叠区域。由于点坐标不能代表一个区域,因此必须对两个数据集使用相似的 x(经度)和 y(纬度)类别来对坐标进行分类。
对于这个任务,我找到了实用的hexbin包,它可以进行hexagonal binning。这个包很棒,但至少我找不到直接输出hexbin
对象之间重叠箱的坐标/ID的函数。例如,hdiffplot
返回一个很好的重叠箱的图形概览,但是如何提取这些信息以进行进一步分析?
library(hexbin)
set.seed(1); df1 <- data.frame(x = rnorm(10, 0, 5), y = rnorm(10, 0, 5))
set.seed(2); df2 <- data.frame(x = rnorm(10, 0, 5), y = rnorm(10, 0, 5))
xrange <- c(floor(min(c(df1$x, df2$x))-1), ceiling(max(c(df1$x, df2$x))+1))
#-/+1 just to make the plot nicer
yrange <- c(floor(min(c(df1$y, df2$y))-1), ceiling(max(c(df1$y, df2$y)))+1)
hb1 <- hexbin(df1$x, df1$y, xbins = 10, xbnds = xrange, ybnds = yrange)
hb2 <- hexbin(df2$x, df2$y, xbins = 10, xbnds = xrange, ybnds = yrange)
hdiffplot(hb1,hb2, xbnds = xrange, ybnds = yrange)
解决方案
我在提出问题时想出了解决这个问题的方法。将其张贴在这里,希望有一天它会帮助某人。
hcell2xy
您可以使用该函数提取坐标。这是一个小函数,用于查找 bin 质心的唯一和重叠坐标:
#' @title Print overlapping and unique bin centroid coordinates for two hexbin objects
#' @param bin1,bin2 two objects of class hexbin.
#' @details The hexbin objects for comparison, bin1 and bin2, must have the same plotting limits and cell size.
#' @return Returns a list of data frames with unique coordinates for \code{bin1} and \code{bin2} as well as overlapping coordinates among bins.
hdiffcoords <- function(bin1, bin2) {
## Checks modified from: https://github.com/edzer/hexbin/blob/master/R/hdiffplot.R
if(is.null(bin1) | is.null(bin1)) {
stop("Need 2 hex bin objects")
} else {
if(bin1@shape != bin2@shape)
stop("Bin objects must have same shape parameter")
if(all(bin1@xbnds == bin2@xbnds) & all(bin1@ybnds == bin2@ybnds))
equal.bounds <- TRUE
else stop("Bin objects need the same xbnds and ybnds")
if(bin1@xbins != bin2@xbins)
stop("Bin objects need the same number of bins")
}
## Find overlapping and unique bins
hd1 <- data.frame(hcell2xy(bin1), count_bin1 = bin1@count, cell_bin1 = bin1@cell)
hd2 <- data.frame(hcell2xy(bin2), count_bin2 = bin2@count, cell_bin2 = bin2@cell)
overlapping_hd1 <- apply(hd1, 1, function(r, A){ sum(A$x==r[1] & A$y==r[2]) }, hd2)
overlapping_hd2 <- apply(hd2, 1, function(r, A){ sum(A$x==r[1] & A$y==r[2]) }, hd1)
overlaps <- merge(hd1[as.logical(overlapping_hd1),], hd2[as.logical(overlapping_hd2),])
unique_hd1 <- hd1[!as.logical(overlapping_hd1),]
unique_hd2 <- hd2[!as.logical(overlapping_hd2),]
## Return list of data.frames
list(unique_bin1 = unique_hd1, unique_bin2 = unique_hd2, overlapping = overlaps)
}
此信息应与hdiffplot
以图形格式返回的信息相同:
df <- hdiffcoords(hb1, hb2)
library(ggplot2)
ggplot() +
geom_point(data = df$unique_bin1, aes(x = x, y = y), color = "red", size = 10) +
geom_point(data = df$unique_bin2, aes(x = x, y = y), color = "cyan", size = 10) +
geom_point(data = df$overlapping, aes(x = x, y = y), color = "green", size = 10) + theme_bw()
任何意见/更正表示赞赏。
推荐阅读
- kotlin - 单击按钮后如何切换到另一个 XML 布局?
- swift - 如何在 Firestore 通过 Swift 完成下载数据之前执行一个函数?
- javascript - mongoose 中的 $lookup 返回空
- javascript - 如何确定在哪个文件中执行了函数?
- magento - 层级价格不适用于 Migration_Default 属性集 Magento 2
- c# - 未填写 umbraco 自定义表单时,Google ReCaptcha 未显示错误
- node.js - Mongoose 如何根据自定义字段更新(如果存在)。否则插入
- javascript - 警告:每个孩子都应该有一个唯一的键 - 在 ReactJS 中传递数组
- css - 列表项具有交替颜色的列表
- gitlab - 在一台服务器上以多个用户的身份运行 Gitlab-runner,用于多个项目