[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
[1] "sf" "data.frame"
library(viridis)
mundo %>%
filter(continent == "Africa") %>%
ggplot(aes(fill = POP_EST)) +
geom_sf() +
scale_fill_viridis_c()
mundo %>%
filter(continent == "Africa") %>%
dplyr::select(NAME, POP_EST, GDP_MD_EST, GLOCAF) ->
africa
write_sf(africa, "ejemplo_africa.shp")
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
[1] "sf" "tbl_df" "tbl" "data.frame"
Obtención de datos con función getData() para Colombia:
level = 0
: división a nivel de país.level = 1
: división regional (departamentos)level = 2
: división municipal (municipios)colombia <- getData(name = "GADM", country = "COL", level = 0)
colombia_sf <- st_as_sf(colombia)
colombia_sf %>%
ggplot() +
geom_sf()
CRS arguments:
+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs
[1] "+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs"
st_as_sf()
la posición de las columnas longitud y latitud, respectivamente. Además es necesario incoporar la proyección del sistema de coordenadas.prueba <- data.frame(lon = -70, lat = 5, punto = "ejemplo")
prueba %>%
st_as_sf(coords = c(1, 2),
crs = "+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs") ->
prueba_sf
prueba_sf
Simple feature collection with 1 feature and 1 field
geometry type: POINT
dimension: XY
bbox: xmin: -70 ymin: 5 xmax: -70 ymax: 5
CRS: +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs
punto geometry
1 ejemplo POINT (-70 5)
raster1 <- raster("images-tif/CHELSA_Bio1_4km2.tif")
raster2 <- raster("images-tif/CHELSA_Bio6_4km2.tif")
raster1
class : RasterLayer
dimensions : 3900, 4980, 19422000 (nrow, ncol, ncell)
resolution : 0.016666, 0.016666 (x, y)
extent : -112.0029, -29.00618, -39.99518, 25.00222 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : D:/DocumentosEdimer/Github/Spatial-Data-Science/SIG_R/images-tif/CHELSA_Bio1_4km2.tif
names : CHELSA_Bio1_4km2
values : -169.4425, 293.2272 (min, max)
class : RasterLayer
dimensions : 3900, 4980, 19422000 (nrow, ncol, ncell)
resolution : 0.016666, 0.016666 (x, y)
extent : -112.0029, -29.00618, -39.99518, 25.00222 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : D:/DocumentosEdimer/Github/Spatial-Data-Science/SIG_R/images-tif/CHELSA_Bio6_4km2.tif
names : CHELSA_Bio6_4km2
values : -286.5303, 264 (min, max)
class : RasterStack
dimensions : 3900, 4980, 19422000, 2 (nrow, ncol, ncell, nlayers)
resolution : 0.016666, 0.016666 (x, y)
extent : -112.0029, -29.00618, -39.99518, 25.00222 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
names : CHELSA_Bio1_4km2, CHELSA_Bio6_4km2
min values : -169.4425, -286.5303
max values : 293.2272, 264.0000
raster_colombia <- promedios %>% crop(colombia_sf) # también funciona: crop(colombia)
levelplot(raster_colombia)
Discarded datum Unknown based on WGS84 ellipsoid in CRS definition,
but +towgs84= values preserved
[1] 236
Simple feature collection with 1 feature and 2 fields
geometry type: POINT
dimension: XY
bbox: xmin: -70 ymin: 5 xmax: -70 ymax: 5
CRS: +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs
punto geometry temperatura
1 ejemplo POINT (-70 5) 236
CHELSA_Bio1_4km2 CHELSA_Bio6_4km2
[1,] 251 221
class : RasterLayer
dimensions : 1209, 898, 1085682 (nrow, ncol, ncell)
resolution : 0.016666, 0.016666 (x, y)
extent : -81.8374, -66.87133, -4.229944, 15.91925 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : D:/DocumentosEdimer/Github/Spatial-Data-Science/SIG_R/colombia.grd
names : layer
values : -47.5102, 269.0904 (min, max)
[1] "+proj=longlat +datum=WGS84 +no_defs"
colombia_igual <- projectRaster(
solo_colombia,
crs = "+proj=laea +lon_0=-74.0917969 +lat_0=0 +datum=WGS84 +units=m +no_defs"
)
plot(colombia_igual, colNA = "black")
colombia_data <- solo_colombia %>%
as("SpatialPixelsDataFrame") %>%
as.data.frame()
head(colombia_data)
# Opcional: cambiando nombre de variable layer
colombia_data <- colombia_data %>% dplyr::rename(Temp = layer)
# Gráfico
ggplot() +
geom_tile(data = colombia_data, aes(x = x, y = y, fill = Temp)) +
geom_sf(data = colombia_sf, alpha = 0) +
scale_fill_viridis_c() +
theme_bw()