是否有一个函数告诉我彼此相邻的单元格
这是我到目前为止的代码用六角形的顶点做点事。
library(dggridR)
library(dplyr)
library(ggplot2)
#Construct a global grid with cells approximately 1000 miles across
dggs <- dgconstruct(spacing=1000, metric=FALSE, resround='down')
#Load included test data set
data(dgquakes)
#Get the corresponding grid cells for each earthquake epicenter (lat-long pair)
dgquakes$cell <- dgGEO_to_SEQNUM(dggs, dgquakes$lat, dgquakes$lon)$seqnum
#Get the number of earthquakes in each equally-sized cell
quakecounts <- dgquakes %>% group_by(cell) %>% summarise(count=n())
quakecounts <- dgquakes %>% group_by(cell) %>% summarise(total=sum())
#Construct a global grid with cells approximately 1000 miles across
dggs <- dgconstruct(spacing=1000, metric=FALSE, resround='down')
nocells <- dgmaxcell(dggs)
grid <- dgrectgrid(dggs,
minlat=-90, minlon=-180,
maxlat=90, maxlon=180, frame = TRUE)
label <- 1:nocells
qgrid <- data.frame(label)
# get the lat-long so we can plot it properly
cellcentres <- dgSEQNUM_to_GEO(dggs, 1:nocells)
qgrid$cent_long <- cellcentres$lon_deg
qgrid$cent_lat <- cellcentres$lat_deg
# create plot
p <- ggplot() +
geom_path(data=grid, aes(x=long, y=lat, group=group), color="black") + # draw each hexagon
geom_label (data=qgrid,aes(x=cent_long, y=cent_lat, label=label)) + # draw label at cell centres
coord_equal()
p
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dgrectgrid()
的输出是一个空间对象,因此您可以在“ Terra”软件包中尝试“相邻”函数,或者在“ SF”软件包中使用“ ST_Touches”函数:library(sf)
neighbours_sf&lt; - st_touches(grid)
或
library> library>
neighbours_terra&lt; - aideacter(vect(vect(grid),grid),(grid),(grid),grid),(grid),grid) type =“触摸”)
The output of
dgrectgrid()
is a spatial object, so you can try either the 'adjacent' function in the 'terra' package, or the 'st_touches' function in the 'sf' package:library(sf)
neighbours_sf <- st_touches(grid)
or
library(terra)
neighbours_terra <- adjacent(vect(grid), type="touches")