06/12, 2020
library(sf)
library(tidyverse)
library(rworldxtra)
data("countriesHigh")
Mundo <- st_as_sf(countriesHigh)
ggplot() + geom_sf(data = Mundo, aes(fill = POP_EST))
ggplot() + geom_sf(data = Mundo, aes(fill = continent))
Africa <- Mundo %>% dplyr::filter(continent == "Africa") ggplot() + geom_sf(data = Africa, aes(fill = POP_EST))
Africa <- Africa %>% mutate(Poblacion_mill = POP_EST/1e+06)
ggplot() + geom_sf(data = Africa, aes(fill = Poblacion_mill)) +
theme_bw() + scale_fill_viridis_c()
Africa <- Africa %>% mutate(PIB_per_Cap = GDP_MD_EST/POP_EST)
ggplot() + geom_sf(data = Africa, aes(fill = PIB_per_Cap)) +
theme_bw()
Africa <- Africa %>% dplyr::select(NAME, Poblacion_mill, PIB_per_Cap,
GLOCAF)
write_sf(Africa, "Africa.shp")
Africa2 <- read_sf("Africa.shp")
ggplot() + geom_sf(data = Africa2, aes(fill = GLOCAF)) + theme_dark()
getData("GADM")Peru <- getData(name = "GADM", country = "PER", level = 1) %>%
st_as_sf()
ggplot() + geom_sf(data = Peru) + theme(legend.position = "none")
Prec <- getData("worldclim", res = 10, var = "prec")
Prec
## class : RasterStack ## dimensions : 900, 2160, 1944000, 12 (nrow, ncol, ncell, nlayers) ## resolution : 0.1666667, 0.1666667 (x, y) ## extent : -180, 180, -60, 90 (xmin, xmax, ymin, ymax) ## crs : +proj=longlat +datum=WGS84 +no_defs ## names : prec1, prec2, prec3, prec4, prec5, prec6, prec7, prec8, prec9, prec10, prec11, prec12 ## min values : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ## max values : 885, 736, 719, 820, 955, 1850, 2088, 1386, 904, 980, 893, 914
plot(Prec)
Invierno <- Prec[[c(6, 7, 8)]] plot(Invierno)
Total_inv <- Prec[[1]] + Prec[[2]] + Prec[[3]] plot(Total_inv)
PP_Total <- sum(Prec) plot(PP_Total)
Raster_Africa <- PP_Total %>% crop(Africa) plot(Raster_Africa)
writeRaster(Raster_Africa, "PP_Africa.tif", overwrite = T)
AfricaRast2 <- raster("PP_Africa.tif")
plot(AfricaRast2)
proj4string(AfricaRast2)
## [1] "+proj=longlat +datum=WGS84 +no_defs"
newproj <- "+proj=laea +lon_0=18.28125 +lat_0=0 +datum=WGS84 +units=km +no_defs" Africa_igual <- projectRaster(AfricaRast2, crs = newproj) plot(Africa_igual)
Africa_DF <- AfricaRast2 %>% as("SpatialPixelsDataFrame") %>%
as.data.frame()
Africa_DF <- Africa_DF %>% rename(Prec = PP_Africa)
ggplot() + geom_tile(data = Africa_DF, aes(x = x, y = y, fill = Prec)) +
geom_sf(data = Africa, alpha = 0) + theme_bw() + scale_fill_viridis_c()
ggplot() + geom_tile(data = Africa_DF, aes(x = x, y = y, fill = Prec)) +
geom_sf(data = Africa, alpha = 0) + coord_sf(crs = "+proj=laea +lon_0=18.28125 +lat_0=0 +datum=WGS84 +units=km +no_defs") +
theme_bw() + scale_fill_viridis_c()