Я хотел бы добавить сетку к многоугольнику и закрасить ее geom_tile
, я пытаюсь вставить сетку в многоугольник, но, похоже, не работает:
library(rgdal)
library(raster)
library(rgeos)
library(dismo)
URL2 <- "https://biogeo.ucdavis.edu/data/gadm3.6/Rsp/gadm36_CZE_1_sp.rds"
data2 <- readRDS(url(URL2))
data <- data2[11, ]
# Plot the shape to see if everything is fine.
plot(data)
# Create an empty raster.
grid <- raster(extent(data))
# Choose its resolution. I will use 2 degrees of latitude and longitude.
res(grid) <- 2
# Make the grid have the same coordinate reference system (CRS) as the shapefile.
proj4string(grid)<-proj4string(data)
# Transform this raster into a polygon and you will have a grid, but without Brazil (or your own shapefile).
gridpolygon <- rasterToPolygons(grid)
# Intersect our grid with Brazil's shape (or your shapefile). R will need a considerable time to do that (~15 minutes in our example). Just let it work while you do other things, or drink a coffee, or whatever. Note that the time needed to finish this depends on the size (area) of your shape, and on your grid resolution. So, don't expect to intersect a 0.5 lat/long grid with a world shapefile in 5 minutes (=]).
dry.grid <- intersect(data, gridpolygon)
# Plot the intersected shape to see if everything is fine.
plot(dry.grid)
Этот подход НЕ добавляет сетку в Полигон и я не знаю, где ошибка.