如何将数据分类为shapefile的六边形并进行绘制?

问题描述 投票:0回答:1

我对R还是这个网站都是陌生的。我目前的发行项目遇到了麻烦。我的目标是创建一个具有六边形的地图,这些六边形具有基于不同属性的颜色渐变。例如,六边形中的记录数,种类数,稀疏度等。我从两个shapefile开始。

六边形之一:

具有10242个要素和4个字段的简单要素集合

几何类型:MULTIPOLYGON

尺寸:XY

bbox:xmin:-180 ymin:-90 xmax:180 ymax:90

CRS:4326

[前十个功能:

 ID CENTRELAT  CENTRELON     AREA                       geometry

 1 -43.06618   41.95708 41583.14 MULTIPOLYGON (((43.50039 -4...

 2 -73.41802 -144.73583 41836.20 MULTIPOLYGON (((-147.695 -7...

 4862 -82.71189  -73.45815 50247.96 MULTIPOLYGON (((-78.89901 -...

 7162  88.01938   53.07438 50258.17 MULTIPOLYGON (((36.63494 87...

 3 -75.32015 -145.44626 50215.61 MULTIPOLYGON (((-148.815 -7...

 4 -77.21239 -146.36437 50225.85 MULTIPOLYGON (((-150.2982 -...

 5 -79.11698 -147.60550 50234.84 MULTIPOLYGON (((-152.3518 -...

 6 -81.03039 -149.37750 50242.49 MULTIPOLYGON (((-155.3729 -...

 7 -82.94618 -152.11105 50248.70 MULTIPOLYGON (((-160.2168 -...

 8 -84.84996 -156.85274 50253.03 MULTIPOLYGON (((-169.0374 -...

[其中一个用于地图:几何类型:POLYGON;尺寸:XY; bbox:xmin:-180 ymin:-90 xmax:180 ymax:83.64513; CRS:4326

这是此链接中的land shapefile:natural earth data

我用st_read函数加载了它们。并使用以下代码创建了地图:

 ggplot() +
  geom_sf(data = hex5) +
  geom_sf(data = land) +
  coord_sf(1, xlim = c(100, 180), ylim = c(0, 90))

The map

我有一个包含物种名称,经度和纬度的数据框。大约有6300个条目。

scientific              lat         lon
1   Acoetes melanonota  11.75690    124.8010
2   Acoetes melanonota  11.97500    102.7350
3   Acoetes melanonota  13.33000    100.9200
4   Acrocirrus muroranensis 42.31400    140.9670
5   Acrocirrus uchidai  43.04800    144.8560
6   Acrocirrus validus  35.30000    139.4830
7   Acutomunna minuta   29.84047    130.9178
8   Admetella longipedata   13.35830    120.5090
9   Admetella longipedata   13.60310    120.7570
10  Aega acuticauda 11.95750    124.1780

如何将这些数据归类到地图的六边形并使用渐变为它们着色?

非常感谢!

r ggplot2 shapefile sf binning
1个回答
0
投票

据我了解,您有一些点和一些多边形。您想通过它们所在的多边形来总结点的值。我给出了一个可能的解决方案的可复制示例:

library(sf)
library(data.table)
library(dplyr)

# Create an exagonal grid
sfc = sf::st_sfc(sf::st_polygon(list(rbind(c(0,0), c(1,0), c(1,1), c(0,0)))))
G = sf::st_make_grid(sfc, cellsize = .1, square = FALSE)
# Convert to sf object
G = sf::st_as_sf(data.table(id_hex=1:76, geom=sf::st_as_text(G)), wkt='geom')
# Create random points on the grid with random value
n=500
p = data.table(id_point=1:n, 
               value = rnorm(n),
               x=sample(seq(0,1,0.01), n, replace=T),
               y=sample(seq(0,1,0.01), n, replace=T)
               )
p = p[x >= y]
P = sf::st_as_sf(p, coords=c('x', 'y'))
# Plot geometry
plot(sf::st_geometry(G))
plot(P, add=TRUE)

“”

# Join the geometries to associate each polygon to the points it contains
# Group by and summarise
J = sf::st_join(G, P, join=sf::st_contains) %>% 
  dplyr::group_by(id_hex) %>% 
  dplyr::summarise(sum_value=sum(value, na.rm=F), 
                   count_value=length(value),
                   mean_value=mean(value, na.rm=F))

plot(J)

“”

# Plot interactive map with mapview package
mapview::mapview(J, zcol="count_value") + 
  mapview::mapview(P)

enter image description here

reprex package(v0.3.0)在2020-04-25创建

© www.soinside.com 2019 - 2024. All rights reserved.