Вот простой пример использования st_make_grid()
для создания сетки в вашем многоугольнике и brewer.pal()
для создания цветовой палитры, чтобы заполнить их.
library(tidyverse)
library(sf)
#> Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3
p1 <- st_polygon(list(rbind(c(0,0),c(1,0),c(1,1),c(0,1), c(0,0))))
grid <- st_make_grid(p1, n = 2)
number_of_cells <- length(grid)
colors <- RColorBrewer::brewer.pal(number_of_cells, name = 'PuBuGn')
ggplot(grid) +
geom_sf(fill = colors)
#Example using multipolygon shapefile
nc <- read_sf(system.file("shape/nc.shp", package="sf"))
#st_combine is used to make a single polygon since nc is a multipolygon shapefile
nc_grid <- st_make_grid(st_combine(nc), n = 20)
ggplot() + geom_sf(data = st_union(nc)) + geom_sf(data =nc_grid, fill = NA)
nc_grid_sf <- as_Spatial(nc_grid) %>% st_as_sf() %>% st_transform(st_crs(nc))
# Keep grid only within polygon
st_intersection(nc_grid, st_union(nc)) %>%
ggplot() +
geom_sf()
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
Создано в 2020-01 -15 при представлении пакета (v0.3.0)