#1. mapa tematico partipacion municipal de los dos grupos de cultivos mas importantes en el departamento del meta.
##.2 Instalacion de paquetes y librerias.
#install.packages("tmap")
#install.packages("ggplot2")
#install.packages("classInt")
library(readxl)
library(readr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(s2)
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
###.3 lectura de datos.
list.files("C:\\Users\\PERSONAL\\Documents\\cuaderno1y2", pattern=c('csv'))
## [1] "co.csv"
## [2] "Evaluaciones_Agropecuarias_Municipales_EVA.csv"
## [3] "meta_Cereales_2020.csv"
## [4] "meta_Platanos_2020.csv"
## [5] "meta_Plátanos_2020.csv"
list.files("C:\\Users\\PERSONAL\\Documents\\cuaderno1y2")
## [1] "20210624_BaseSIPRA2020.xlsx"
## [2] "co.csv"
## [3] "Cuaderno1.html"
## [4] "Cuaderno1.Rmd"
## [5] "cuaderno2.html"
## [6] "cuaderno2.Rmd"
## [7] "Evaluaciones_Agropecuarias_Municipales_EVA.csv"
## [8] "meta_Cereales_2020.csv"
## [9] "meta_Platanos_2020.csv"
## [10] "meta_Plátanos_2020.csv"
## [11] "META3116.shp"
## [12] "MGN_MPIO_POLITICO.CPG"
## [13] "MGN_MPIO_POLITICO.dbf"
## [14] "MGN_MPIO_POLITICO.prj"
## [15] "MGN_MPIO_POLITICO.sbn"
## [16] "MGN_MPIO_POLITICO.sbx"
## [17] "MGN_MPIO_POLITICO.shp"
## [18] "MGN_MPIO_POLITICO.shp.xml"
## [19] "MGN_MPIO_POLITICO.shx"
## [20] "MUNICIPIOSMETA1.cpg"
## [21] "MUNICIPIOSMETA1.dbf"
## [22] "MUNICIPIOSMETA1.prj"
## [23] "MUNICIPIOSMETA1.qmd"
## [24] "MUNICIPIOSMETA1.shp"
## [25] "MUNICIPIOSMETA1.shx"
## [26] "rsconnect"
(Platanos = read_csv("C:\\Users\\PERSONAL\\Documents\\cuaderno1y2\\meta_Platanos_2020.csv",show_col_types = FALSE))
## New names:
## * `` -> ...1
(mun.tmp = st_read("C:\\Users\\PERSONAL\\Documents\\cuaderno1y2\\MUNICIPIOSMETA1.shp"))
## Reading layer `MUNICIPIOSMETA1' from data source
## `C:\Users\PERSONAL\Documents\cuaderno1y2\MUNICIPIOSMETA1.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 29 features and 11 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -74.89921 ymin: 1.604238 xmax: -71.07753 ymax: 4.899101
## CRS: NA
mun.tmp %>% select(MPIO_NAREA,MPIO_CCNCT,) -> municipios
municipios
(cities = read_csv("C:\\Users\\PERSONAL\\Documents\\cuaderno1y2\\co.csv"))
## Rows: 1102 Columns: 9
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (5): city, country, iso2, admin_name, capital
## dbl (4): lat, lng, population, population_proper
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
sf.cities <- st_as_sf(x =cities,
coords = c("lng", "lat"))
sf.cities
st_crs(sf.cities) <- 4326
st_crs(municipios) <- 4326
st_crs(sf.cities)
## Coordinate Reference System:
## User input: EPSG:4326
## wkt:
## GEOGCRS["WGS 84",
## DATUM["World Geodetic System 1984",
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["geodetic latitude (Lat)",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["geodetic longitude (Lon)",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## USAGE[
## SCOPE["Horizontal component of 3D system."],
## AREA["World."],
## BBOX[-90,-180,90,180]],
## ID["EPSG",4326]]
st_crs(municipios)
## Coordinate Reference System:
## User input: EPSG:4326
## wkt:
## GEOGCRS["WGS 84",
## DATUM["World Geodetic System 1984",
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["geodetic latitude (Lat)",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["geodetic longitude (Lon)",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## USAGE[
## SCOPE["Horizontal component of 3D system."],
## AREA["World."],
## BBOX[-90,-180,90,180]],
## ID["EPSG",4326]]
####.4 limpiamos los datos con datos relevantes de nuestro departamento.
sf.cities.joined <- st_join(sf.cities, municipios, join = st_within)
sf.cities.joined
meta.cities = dplyr::filter(sf.cities.joined, admin_name=='Meta')
meta.cities
######5.el primer cultivo de mayo importancia en el departamento del meta.
library(tmap)
library(ggplot2)
library(ggrepel)
library(classInt)
class(Platanos$Cod_Mun)
## [1] "numeric"
class(municipios$MPIO_CCNCT)
## [1] "character"
municipios$MPIO_CCNCT = as.numeric(municipios$MPIO_CCNCT)
munic_Platanos = left_join(municipios,Platanos, by = c( "MPIO_CCNCT"="Cod_Mun"))
munic_Platanos
breaks <- classIntervals(na.omit(munic_Platanos$max_prod), n = 6, style = 'fisher')
lab_vec <- vector(length = length(breaks$brks)-1)
rounded_breaks <- round(breaks$brks,2)
lab_vec[1] <- paste0('[', rounded_breaks[1],' - ', rounded_breaks[2],']')
for(i in 2:(length(breaks$brks) - 1)){
lab_vec[i] <- paste0('(',rounded_breaks[i], ' - ', rounded_breaks[i+1], ']')
}
munic_Platanos <- munic_Platanos %>%
mutate(faktor_class = factor(cut(max_prod, breaks$brks, include.lowest = T), labels = lab_vec))
munic_Platanos$Produccion = munic_Platanos$faktor_class
munic_Platanos$mid <- sf::st_centroid(munic_Platanos$geometry)
munic_Platanos
LONG = st_coordinates( munic_Platanos$mid)[,1]
LAT = st_coordinates(munic_Platanos$mid)[,2]
ggplot(data = munic_Platanos) +
geom_sf(aes(fill = Produccion)) +
geom_label_repel(aes(x = LONG, y = LAT, label = MPIO_CCNCT),
label.padding = unit(0.05,"lines"),
label.r = unit(0.025, "lines"),
label.size = 0.05)
######6.representamos el segundo cultivo de mayor importancia en el departamento del meta.
(cereales = read_csv("C:\\Users\\PERSONAL\\Documents\\cuaderno1y2\\meta_Cereales_2020.csv",show_col_types = FALSE))
## New names:
## * `` -> ...1
cereales$Cod_Mun = as.character(cereales$Cod_Mun)
municipios$MPIO_CCNCT = as.character(municipios$MPIO_CCNCT)
munic_cereales = left_join(municipios, cereales, by = c("MPIO_CCNCT" = "Cod_Mun"))
munic_cereales
facet = "max_prod"
cereales_map =
tm_shape(munic_cereales) + tm_polygons(facet) + tm_text(text = "MPIO_CCNCT", size = 0.7, fontfamily = "sans") +
tm_shape(meta.cities) + tm_symbols(shape = 2, col = "red", size = 0.20) +
tm_credits("Data source: UPRA (2020)", fontface = "bold") +
tm_layout(main.title = "Produccion de cereales en 2020",
main.title.fontface = "bold.italic",
legend.title.fontfamily = "monospace") +
tm_scale_bar(position = c("left", "bottom"))
tmap_mode("view")
## tmap mode set to interactive viewing
cereales_map
## Credits not supported in view mode.
## Symbol shapes other than circles or icons are not supported in view mode.