rm(list=ls())
list.of.packages <- c("tidyverse", "rgeos", "sf", "raster", "cartography", "SpatialPosition")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.0 v purrr 0.3.3
## v tibble 2.1.3 v dplyr 0.8.5
## v tidyr 1.0.2 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(readxl)
library(rgeos)
## Loading required package: sp
## rgeos version: 0.5-2, (SVN revision 621)
## GEOS runtime version: 3.6.1-CAPI-1.10.1
## Linking to sp version: 1.4-1
## Polygon checking: TRUE
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(raster)
##
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
##
## select
## The following object is masked from 'package:tidyr':
##
## extract
library(cartography)
nbi <- read_excel("C:/Users/oaparicio15/Desktop/QGIS/ColombiaDatos/NBI_META.xlsx")
Veamos cuáles son los atributos de los datos.
head(nbi)
nbi %>%
slice(which.max(NBI)) -> max_nbi
max_nbi
nbi %>%
slice(which.min(NBI)) -> min_nbi
min_nbi
nbi %>%
arrange(desc(NBI)) -> desc_nbi
desc_nbi
munic <- st_read("C:/Users/oaparicio15/Desktop/QGIS/ColombiaDatos/50_META/ADMINISTRATIVO/MGN_MPIO_POLITICO.shp")
## Reading layer `MGN_MPIO_POLITICO' from data source `C:\Users\oaparicio15\Desktop\QGIS\ColombiaDatos\50_META\ADMINISTRATIVO\MGN_MPIO_POLITICO.shp' using driver `ESRI Shapefile'
## Simple feature collection with 29 features and 9 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: -74.89921 ymin: 1.604238 xmax: -71.07753 ymax: 4.899101
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
head(munic$MPIO_CNMBR)
## [1] VILLAVICENCIO ACACÍAS BARRANCA DE UPIA CABUYARO
## [5] CASTILLA LA NUEVA CUBARRAL
## 29 Levels: ACACÍAS BARRANCA DE UPIA CABUYARO CASTILLA LA NUEVA ... VISTAHERMOSA
nbi_munic = left_join(munic, nbi, by=c("MPIO_CCDGO"="CODIGO"))
## Warning: Column `MPIO_CCDGO`/`CODIGO` joining factor and character vector,
## coercing into character vector
nbi_munic %>%
dplyr::select(MUNICIPIO, MPIO_CCDGO, NBI) -> check_nbi_munic
head(check_nbi_munic)
nbi_munic_new <- st_transform(nbi_munic, crs = 3116)
mun.osm <- getTiles(
x = nbi_munic_new,
type = "OpenStreetMap",
zoom = 8,
cachedir = TRUE,
crop = FALSE
)
# establecer márgenes
opar <- par(mar = c(0,0,1.2,0))
# plot osm tiles
tilesLayer(x = mun.osm)
# Trazao de los municipios (only borders are plotted)
plot(st_geometry(nbi_munic_new), col = NA, border = "black", add=TRUE)
# plot NBI
propSymbolsLayer(
x = nbi_munic_new,
var = "NBI",
inches = 0.16,
col = "brown4",
legend.pos = "topright",
legend.title.txt = "Total NBI"
)
# Diseño
layoutLayer(title = " Distribución NBI en Meta",
sources = "Sources: DANE, 2018\n© OpenStreetMap",
author = " Erika Aparicio",
frame = TRUE, north = FALSE, tabtitle = TRUE)
# Flecha norte
north(pos = "topleft")
#
#par(opar)
#display.carto.all(n=8) retirar el # para observar las paletas de colores
# establecer margenes
opar <- par(mar = c(0,0,1.2,0))
# establecer el color de fondo de la figura
par(bg="grey90")
# Trazar municipios (solo se traza el color de fondo)
plot(st_geometry(nbi_munic_new), col = NA, border = NA, bg = "#aadaff")
# plot NBI
choroLayer(
x = nbi_munic_new,
var = "NBI",
method = "geom",
nclass=6,
col = carto.pal(pal1 = "kaki.pal", n1 = 6),
border = "white",
lwd = 0.5,
legend.pos = "topright",
legend.title.txt = "NBI",
add = TRUE
)
# Diseño
layoutLayer(title = "Distribución NBI en Meta",
sources = "Source: DANE, 2018",
author = "Erika Aparicio",
frame = TRUE, north = TRUE, tabtitle = TRUE, col="black")
# Flecha del norte
north(pos = "topleft")
#
#par(opar)
nbi_munic_2 <- dplyr::mutate(nbi_munic_new, pobreza = ifelse(MISERIA > 20, "Extrema",
ifelse(HACINAMIENTO > 5, "Alta", "Intermedia")))
head(nbi_munic_2)
library(sf)
library(cartography)
# set margins
opar <- par(mar = c(0,0,1.2,0))
# Plot the municipalities
plot(st_geometry(nbi_munic_2), col="#f2efe9", border="#b38e43", bg = "#aad3df",
lwd = 0.5)
# trazar simbolos con diferente color
propSymbolsTypoLayer(
x = nbi_munic_2,
var = "NBI",
inches = 0.2,
symbols = "circle",
border = "white",
lwd = .4,
legend.var.pos = c(1360000, 950000),
legend.var.title.txt = "NBI",
var2 = "pobreza",
legend.var2.values.order = c("Extrema", "Alta",
"Intermedia"),
col = carto.pal(pal1 = "multi.pal", n1 = 3),
legend.var2.pos = c(1360000, 800000),
legend.var2.title.txt = "Pobreza"
)
# Diseño
layoutLayer(title="Distribución NBI en Meta",
author = "Erika Aparicio",
sources = "Source: DANE, 2018",
scale = 1, tabtitle = TRUE, frame = TRUE)
# flecha del norte
north(pos = "topleft")
#
#par(opar)
library(sf)
library(cartography)
# establecer margenes
opar <- par(mar = c(0,0,1.2,0))
# establecer el color de fondo de la figura
par(bg="grey25")
# plot municipalities
plot(st_geometry(nbi_munic_2), col = "#e4e9de", border = "darkseagreen4",
bg = "grey75", lwd = 0.5)
# plot NBI
choroLayer(
x = nbi_munic_new,
var = "NBI",
method = "geom",
nclass=5,
col = carto.pal(pal1 = "kaki.pal", n1 = 6),
border = "white",
lwd = 0.4,
legend.pos = c(1370000, 920000),
legend.title.txt = "NBI",
add = TRUE
)
# plot labels
labelLayer(
x = nbi_munic_2,
txt = "MUNICIPIO",
col= "white",
cex = 0.4,
font = 4,
halo = TRUE,
bg = "grey20",
r = 0.1,
overlap = FALSE,
show.lines = FALSE
)
# Diseño del mapa
layoutLayer(
title = "Municipios de Meta",
sources = "Source: DANE, 2018",
author = "Erika Aparicio",
frame = TRUE,
north = TRUE,
tabtitle = TRUE,
theme = "taupe.pal"
)
#
#par(opar)
# establecer margenes
opar <- par(mar = c(0,0,1.2,0))
# establecer el color de fondo de la figura
par(bg="grey25")
# plot municipalities
plot(st_geometry(nbi_munic_2), col = "#e4e9de", border = "darkseagreen4",
bg = "grey75", lwd = 0.5)
# plot NBI
choroLayer(
x = nbi_munic_new,
var = "MISERIA",
method = "geom",
nclass=5,
col = carto.pal(pal1 = "red.pal", n1 = 6),
border = "white",
lwd = 0.4,
legend.pos = c(1370000, 900000),
legend.title.txt = "Miseria",
add = TRUE
)
# plot labels
labelLayer(
x = nbi_munic_2,
txt = "MUNICIPIO",
col= "white",
cex = 0.4,
font = 4,
halo = TRUE,
bg = "grey25",
r = 0.1,
overlap = FALSE,
show.lines = FALSE
)
# Diseño del mapa
layoutLayer(
title = "Municipios de Meta",
sources = "Source: DANE, 2018",
author = "Erika Aparicio",
frame = TRUE,
north = TRUE,
tabtitle = TRUE,
theme = "taupe.pal"
)
# establecer margenes
opar <- par(mar = c(0,0,1.2,0))
# establecer el color de fondo de la figura
par(bg="grey25")
# plot municipalities
plot(st_geometry(nbi_munic_2), col = "#e4e9de", border = "darkseagreen4",
bg = "grey75", lwd = 0.5)
# plot NBI
choroLayer(
x = nbi_munic_new,
var = "INASISTENCIA",
method = "geom",
nclass=5,
col = carto.pal(pal1 = "wine.pal", n1 = 6),
border = "white",
lwd = 0.3,
legend.pos = c(1340000, 710000),
legend.title.txt = "Inasistencia escolar",
add = TRUE
)
# plot labels
labelLayer(
x = nbi_munic_2,
txt = "MUNICIPIO",
col= "white",
cex = 0.4,
font = 4,
halo = TRUE,
bg = "grey25",
r = 0.1,
overlap = FALSE,
show.lines = FALSE
)
# Diseño del mapa
layoutLayer(
title = "Municipios de Meta",
sources = "Source: DANE, 2018",
author = "Erika Aparicio",
frame = TRUE,
north = TRUE,
tabtitle = TRUE,
theme = "taupe.pal"
)
crops2018 <- read_excel("C:/Users/oaparicio15/Desktop/QGIS/ColombiaDatos/META_EVA2.xlsx")
head(crops2018)
crops2018 %>%
filter(CULTIVO == "MAIZ") -> maiz2018
head(maiz2018)
maiz2018$TEMP <- as.character(maiz2018$COD_MUN)
head(maiz2018)
maiz2018$MPIO_CCDGO <- as.factor(maiz2018$TEMP)
maiz_munic = left_join(munic, maiz2018, by="MPIO_CCDGO")
## Warning: Column `MPIO_CCDGO` joining factors with different levels, coercing to
## character vector
head(maiz_munic)
rep_maiz <- st_transform(maiz_munic, crs = 3116)
# establecer margenes
opar <- par(mar = c(0,0,1.2,0))
# plot municipalities (only the backgroung color is plotted)
plot(st_geometry(rep_maiz), col = NA, border = "black", bg = "grey75")
# plot isopleth map
smoothLayer(
x = rep_maiz,
var = 'PRODUCCION',
typefct = "exponential",
span = 25000,
beta = 2,
nclass = 10,
col = carto.pal(pal1 = 'green.pal', n1 = 10),
border = "grey",
lwd = 0.1,
mask = rep_maiz,
legend.values.rnd = -3,
legend.title.txt = "PRODUCCION",
legend.pos = "topright",
add=TRUE
)
# Anotaciones en el mapa
text(x = 1360000, y = 800000, cex = 0.6, adj = 0, font = 3, labels =
"Función distancia:\n- type = exponencial\n- beta = 2\n- span = 25 km")
# Diseño
layoutLayer(title = "Distribucción en Meta de la producción de maíz",
sources = "Sources: DANE and MADR, 2018",
author = "Erika Aparicio",
frame = FALSE, north = FALSE, tabtitle = TRUE, theme = "green.pal")
# Flecha del norte
north(pos = "topleft")
#
par(opar)
### open the plot
png("C:/Users/oaparicio15/Desktop/QGIS/ColombiaDatos/maiz_2018.png", width = 2048, height = 1526)
opar <- par(mar = c(0,0,5,5))
# Plot the municipalities
plot(st_geometry(rep_maiz), col="darkseagreen3", border="darkseagreen4",
bg = "white", lwd = 0.7)
# Unir simbolos con la coloracion de coropletas
propSymbolsChoroLayer(x = rep_maiz, var = "PRODUCCION", var2 = "RENDIMIENTO",
col = carto.pal(pal1 = "green.pal", n1 = 3,
pal2 = "red.pal", n2 = 3),
inches = 0.9, method = "q6",
border = "grey50", lwd = 1,
legend.title.cex = 1.7,
legend.values.cex = 1.0,
legend.var.pos = "bottomright",
legend.var2.pos = "left",
legend.var2.values.rnd = 2,
legend.var2.title.txt = "Rendimiento (Ton/Ha)",
legend.var.title.txt = "Producción de maíz en 2018",
legend.var.style = "e")
# Diseño del plot
labelLayer(
x = rep_maiz,
txt = "MPIO_CNMBR",
col= "white",
cex = 1.0,
font = 4,
halo = FALSE,
bg = "white",
r = 0.1,
overlap = FALSE,
show.lines = FALSE
)
# Diseño
layoutLayer(title="Producción y rendimiento del cultivo de maíz en Meta, 2018",
author = "Erika Aparicio",
sources = "Sources: MADR & DANE, 2018",
scale = , tabtitle = FALSE, frame = TRUE)
# north arrow
north(pos = "topleft")
#
title(main="Producción y rendimiento de maíz en Meta, 2018", cex.main=3,
sub= "Source: MADR & DANE, 2018", cex.sub=2)
#
graticule = TRUE
#
par(opar)
### close the plot
dev.off()
## png
## 2
## png
## 2
Producción de maiz en el departamento de Meta,2018
crops2018 %>%
filter(CULTIVO == "PALMA DE ACEITE") -> palma2018
palma2018$TEMP <- as.character(palma2018$COD_MUN)
head(palma2018)
palma2018$MPIO_CCDGO <- as.factor(palma2018$TEMP)
palma_munic = left_join(munic, palma2018, by="MPIO_CCDGO")
## Warning: Column `MPIO_CCDGO` joining factors with different levels, coercing to
## character vector
head(palma_munic)
rep_palma <- st_transform(palma_munic, crs = 3116)
### open the plot
png("C:/Users/oaparicio15/Desktop/QGIS/ColombiaDatos/palma_2018.png", width = 2048, height = 1526)
opar <- par(mar = c(0,0,5,5))
# Plot the municipalities
plot(st_geometry(rep_palma), col="darkseagreen3", border="darkseagreen4",
bg = "white", lwd = 0.7)
# Unir simbolos con la coloracion de coropletas
propSymbolsChoroLayer(x = rep_palma, var = "PRODUCCION", var2 = "RENDIMIENTO",
col = carto.pal(pal1 = "green.pal", n1 = 3,
pal2 = "red.pal", n2 = 3),
inches = 0.9, method = "q6",
border = "grey50", lwd = 1,
legend.title.cex = 1.7,
legend.values.cex = 1.0,
legend.var.pos = "bottomright",
legend.var2.pos = "left",
legend.var2.values.rnd = 2,
legend.var2.title.txt = "Rendimiento (Ton/Ha)",
legend.var.title.txt = "Producción de palma en 2018",
legend.var.style = "e")
# Diseño del plot
labelLayer(
x = rep_palma,
txt = "MPIO_CNMBR",
col= "white",
cex = 1.0,
font = 4,
halo = FALSE,
bg = "white",
r = 0.1,
overlap = FALSE,
show.lines = FALSE
)
# Diseño
layoutLayer(title="Producción y rendimiento del cultivo de palma de aceite en Meta, 2018",
author = "Erika Aparicio",
sources = "Sources: MADR & DANE, 2018",
scale = , tabtitle = FALSE, frame = TRUE)
# Flecha del norte
north(pos = "topleft")
#
title(main="Producción y rendimiento de palma de aceite en Meta, 2018", cex.main=3,
sub= "Source: MADR & DANE, 2018", cex.sub=2)
#
graticule = TRUE
#
par(opar)
### close the plot
dev.off()
## png
## 2
Producción de maiz en el departamento de Meta,2018
sessionInfo()
## R version 3.6.3 (2020-02-29)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Spanish_Colombia.1252 LC_CTYPE=Spanish_Colombia.1252
## [3] LC_MONETARY=Spanish_Colombia.1252 LC_NUMERIC=C
## [5] LC_TIME=Spanish_Colombia.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] SpatialPosition_1.2.0 cartography_2.4.1 raster_3.0-12
## [4] sf_0.8-1 rgeos_0.5-2 sp_1.4-1
## [7] readxl_1.3.1 forcats_0.5.0 stringr_1.4.0
## [10] dplyr_0.8.5 purrr_0.3.3 readr_1.3.1
## [13] tidyr_1.0.2 tibble_2.1.3 ggplot2_3.3.0
## [16] tidyverse_1.3.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.4 lubridate_1.7.8 lattice_0.20-38 png_0.1-7
## [5] class_7.3-15 assertthat_0.2.1 digest_0.6.25 R6_2.4.1
## [9] cellranger_1.1.0 backports_1.1.5 reprex_0.3.0 evaluate_0.14
## [13] e1071_1.7-3 httr_1.4.1 pillar_1.4.3 rlang_0.4.5
## [17] rstudioapi_0.11 rmarkdown_2.1 rgdal_1.4-8 munsell_0.5.0
## [21] broom_0.5.5 compiler_3.6.3 modelr_0.1.6 xfun_0.12
## [25] pkgconfig_2.0.3 htmltools_0.4.0 tidyselect_1.0.0 codetools_0.2-16
## [29] fansi_0.4.1 crayon_1.3.4 dbplyr_1.4.2 withr_2.1.2
## [33] grid_3.6.3 nlme_3.1-144 jsonlite_1.6.1 gtable_0.3.0
## [37] lifecycle_0.2.0 DBI_1.1.0 magrittr_1.5 units_0.6-6
## [41] scales_1.1.0 KernSmooth_2.23-16 cli_2.0.2 stringi_1.4.6
## [45] fs_1.4.1 xml2_1.3.1 generics_0.0.2 vctrs_0.2.4
## [49] tools_3.6.3 glue_1.3.2 hms_0.5.3 slippymath_0.3.1
## [53] yaml_2.2.1 colorspace_1.4-1 classInt_0.4-2 rvest_0.3.5
## [57] knitr_1.28 haven_2.2.0