INTRODUCCIÓN

En el presente cuaderno, se ilustrará como hacer mapas temáticos de los dos grupos de cultivos más importantes en el departamento de Norte de santander. para tal fin, se usarán los archivos ‘csv’ generados del cuaderno uno y los archivos ‘shapefile’ generados del mapa de los municipios del departamento en QGIS.

Leyendo los archivos relacionados a los municipios, cultivos y ciudades

Archivos ‘csv’ guardados en el directorio, generados en el primer cuaderno de la base de datos de las evaluaciones agropecuarios municipales del año 2020, en donde se muestran los dos grupos de cultivos màs importantes de Norte de Santander, adicionalmente se muestra el archivo ‘csv’ de las ciudades de Colombia:

list.files("C:/Users/andre/OneDrive/Escritorio/Documentos U/12 matricula/Geomatica/Proyectos", pattern=c('csv'))
## [1] "co.csv"                                        
## [2] "Evaluaciones_Agropecuarias_Municipales_EVA.csv"
## [3] "nortestder_cereales_2020.csv"                  
## [4] "nortestder_LegumiYOleagi_2020.csv"

Archivos ‘shp’ de los municipios de Norte de Santander guardados en el directorio:

list.files("C:/Users/andre/OneDrive/Escritorio/Documentos U/12 matricula/Geomatica/Datos")
##  [1] "20210624_BaseSIPRA2020.xlsx"      "departamentoscolombia.gpkg"      
##  [3] "departamentosnortesantander.gpkg" "MGN_MPIO_POLITICO.CPG"           
##  [5] "MGN_MPIO_POLITICO.dbf"            "MGN_MPIO_POLITICO.prj"           
##  [7] "MGN_MPIO_POLITICO.sbn"            "MGN_MPIO_POLITICO.sbx"           
##  [9] "MGN_MPIO_POLITICO.shp"            "MGN_MPIO_POLITICO.shp.xml"       
## [11] "MGN_MPIO_POLITICO.shx"            "MGN2018_MPIO_POLITICO.rar"       
## [13] "municipiosNS.cpg"                 "municipiosNS.dbf"                
## [15] "municipiosNS.prj"                 "municipiosNS.qmd"                
## [17] "municipiosNS.shp"                 "municipiosNS.shx"                
## [19] "nortesantander.gpkg"

Archivo del grupo de cultivo más importante de Norte de Santander

(leguminosas = read_csv("C:/Users/andre/OneDrive/Escritorio/Documentos U/12 matricula/Geomatica/Proyectos/nortestder_LegumiYOleagi_2020.csv"))
## Rows: 38 Columns: 4
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (2): Municipio, Grupo
## dbl (2): Cod_Mun, max_prod
## 
## 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.
## # A tibble: 38 x 4
##    Cod_Mun Municipio    Grupo                     max_prod
##      <dbl> <chr>        <chr>                        <dbl>
##  1   54810 Tibú         Leguminosas Y Oleaginosas  105860.
##  2   54385 La Esperanza Leguminosas Y Oleaginosas   21651.
##  3   54720 Sardinata    Leguminosas Y Oleaginosas   16692.
##  4   54001 Cúcuta       Leguminosas Y Oleaginosas    6982.
##  5   54261 El Zulia     Leguminosas Y Oleaginosas    3055.
##  6   54003 Ábrego       Leguminosas Y Oleaginosas    1728 
##  7   54498 Ocaña        Leguminosas Y Oleaginosas    1500 
##  8   54670 San Calixto  Leguminosas Y Oleaginosas    1116 
##  9   54398 La Playa     Leguminosas Y Oleaginosas     880 
## 10   54174 Chitagá      Leguminosas Y Oleaginosas     680.
## # ... with 28 more rows

Archivo shapefile con los municipios de Norte de Santander

(mun.tmp =  st_read("C:/Users/andre/OneDrive/Escritorio/Documentos U/12 matricula/Geomatica/Datos/municipiosNS.shp"))
## Reading layer `municipiosNS' from data source 
##   `C:\Users\andre\OneDrive\Escritorio\Documentos U\12 matricula\Geomatica\Datos\municipiosNS.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 41 features and 11 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.63379 ymin: 6.872201 xmax: -72.04761 ymax: 9.290847
## Geodetic CRS:  WGS 84
## Simple feature collection with 41 features and 11 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.63379 ymin: 6.872201 xmax: -72.04761 ymax: 9.290847
## Geodetic CRS:  WGS 84
## First 10 features:
##    DPTO_CCDGO MPIO_CCDGO         MPIO_CNMBR
## 1          54        001 SAN JOSÉ DE CÚCUTA
## 2          54        003             ÁBREGO
## 3          54        051          ARBOLEDAS
## 4          54        099          BOCHALEMA
## 5          54        109         BUCARASICA
## 6          54        125             CÁCOTA
## 7          54        128            CÁCHIRA
## 8          54        172          CHINÁCOTA
## 9          54        223          CUCUTILLA
## 10         54        239            DURANIA
##                              MPIO_CRSLC MPIO_NAREA MPIO_CCNCT MPIO_NANO
## 1                                  1972  1133.5259      54001      2018
## 2                                  1806  1382.4498      54003      2018
## 3                                  1835   456.1490      54051      2018
## 4                                  1826   170.2670      54099      2018
## 5                                  1838   270.7909      54109      2018
## 6                                  1630   138.9222      54125      2018
## 7                                  1911   615.8199      54128      2018
## 8                                  1775   166.2541      54172      2018
## 9                                  1834   374.9105      54223      2018
## 10 Ordenanza 12 del 27 de Marzo de 1911   175.0128      54239      2018
##            DPTO_CNMBR Shape_Leng Shape_Area ORIG_FID
## 1  NORTE DE SANTANDER  2.8770204 0.09292433      868
## 2  NORTE DE SANTANDER  2.2393759 0.11336240      869
## 3  NORTE DE SANTANDER  1.1611936 0.03736204      870
## 4  NORTE DE SANTANDER  0.7565850 0.01394456      871
## 5  NORTE DE SANTANDER  0.8281364 0.02220518      872
## 6  NORTE DE SANTANDER  0.5561253 0.01136823      873
## 7  NORTE DE SANTANDER  1.6560289 0.05045724      874
## 8  NORTE DE SANTANDER  0.7573386 0.01361348      875
## 9  NORTE DE SANTANDER  0.8858476 0.03069885      876
## 10 NORTE DE SANTANDER  0.6020091 0.01433714      877
##                          geometry
## 1  POLYGON ((-72.4778 8.430477...
## 2  POLYGON ((-73.01687 8.28517...
## 3  POLYGON ((-72.73134 7.71823...
## 4  POLYGON ((-72.60265 7.7578,...
## 5  POLYGON ((-72.95019 8.17889...
## 6  POLYGON ((-72.62101 7.32978...
## 7  POLYGON ((-73.04222 7.86361...
## 8  POLYGON ((-72.58771 7.69713...
## 9  POLYGON ((-72.79776 7.64021...
## 10 POLYGON ((-72.63625 7.83530...

Identificar y limpiar los atributos que posee el archivo

Para realizar de manera exitosa el join, es importante tener claros los atributos en común que tiene el archivo “csv” y el “shp”.

mun.tmp %>% select(MPIO_CCNCT, MPIO_CNMBR, MPIO_NAREA) -> municipios

municipios
## Simple feature collection with 41 features and 3 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.63379 ymin: 6.872201 xmax: -72.04761 ymax: 9.290847
## Geodetic CRS:  WGS 84
## First 10 features:
##    MPIO_CCNCT         MPIO_CNMBR MPIO_NAREA                       geometry
## 1       54001 SAN JOSÉ DE CÚCUTA  1133.5259 POLYGON ((-72.4778 8.430477...
## 2       54003             ÁBREGO  1382.4498 POLYGON ((-73.01687 8.28517...
## 3       54051          ARBOLEDAS   456.1490 POLYGON ((-72.73134 7.71823...
## 4       54099          BOCHALEMA   170.2670 POLYGON ((-72.60265 7.7578,...
## 5       54109         BUCARASICA   270.7909 POLYGON ((-72.95019 8.17889...
## 6       54125             CÁCOTA   138.9222 POLYGON ((-72.62101 7.32978...
## 7       54128            CÁCHIRA   615.8199 POLYGON ((-73.04222 7.86361...
## 8       54172          CHINÁCOTA   166.2541 POLYGON ((-72.58771 7.69713...
## 9       54223          CUCUTILLA   374.9105 POLYGON ((-72.79776 7.64021...
## 10      54239            DURANIA   175.0128 POLYGON ((-72.63625 7.83530...

Archivo con las ciudades de Colombia

Para éste paso, se usa la base de datos mundial de ciudades.

(cities = read_csv("C:/Users/andre/OneDrive/Escritorio/Documentos U/12 matricula/Geomatica/Proyectos/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.
## # A tibble: 1,102 x 9
##    city           lat   lng country  iso2  admin_name         capital population
##    <chr>        <dbl> <dbl> <chr>    <chr> <chr>              <chr>        <dbl>
##  1 Bogotá        4.61 -74.1 Colombia CO    Bogotá             primary    9464000
##  2 Medellín      6.24 -75.6 Colombia CO    Antioquia          admin      2529403
##  3 Cali          3.44 -76.5 Colombia CO    Valle del Cauca    admin      2471474
##  4 Barranquilla 11.0  -74.8 Colombia CO    Atlántico          admin      1274250
##  5 Cartagena    10.4  -75.5 Colombia CO    Bolívar            admin      1036412
##  6 Soacha        4.58 -74.2 Colombia CO    Cundinamarca       minor       995268
##  7 Palermo       2.89 -75.4 Colombia CO    Huila              minor       800000
##  8 Cúcuta        7.91 -72.5 Colombia CO    Norte de Santander admin       750000
##  9 Soledad      10.9  -74.8 Colombia CO    Atlántico          minor       698852
## 10 Pereira       4.81 -75.7 Colombia CO    Risaralda          admin       590554
## # ... with 1,092 more rows, and 1 more variable: population_proper <dbl>

Ahora se debe Convertir el archivo de las ciudades a un objeto espacial:

sf.cities <-  st_as_sf(x = cities, coords = c("lng", "lat"))

sf.cities
## Simple feature collection with 1102 features and 7 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -81.7006 ymin: -4.215 xmax: -67.4858 ymax: 13.3817
## CRS:           NA
## # A tibble: 1,102 x 8
##    city         country  iso2  admin_name    capital population population_prop~
##    <chr>        <chr>    <chr> <chr>         <chr>        <dbl>            <dbl>
##  1 Bogotá       Colombia CO    Bogotá        primary    9464000          7963000
##  2 Medellín     Colombia CO    Antioquia     admin      2529403          2529403
##  3 Cali         Colombia CO    Valle del Ca~ admin      2471474          2471474
##  4 Barranquilla Colombia CO    Atlántico     admin      1274250          1274250
##  5 Cartagena    Colombia CO    Bolívar       admin      1036412          1036412
##  6 Soacha       Colombia CO    Cundinamarca  minor       995268           995268
##  7 Palermo      Colombia CO    Huila         minor       800000           800000
##  8 Cúcuta       Colombia CO    Norte de San~ admin       750000           750000
##  9 Soledad      Colombia CO    Atlántico     minor       698852           342556
## 10 Pereira      Colombia CO    Risaralda     admin       590554           590554
## # ... with 1,092 more rows, and 1 more variable: geometry <POINT>

Como se puede observar, el archivo a pesar de haberse convertido no posee CRS (CRS=NA), se usa el código global EPSG 4326

st_crs(sf.cities) <- 4326

Subconjunto de datos relevantes del departamento

unión del archivo de las ciudades de Colombia con los municipios de Norte de Santander

sf.cities.joined <- st_join(sf.cities, municipios, join = st_within)

sf.cities.joined
## Simple feature collection with 1102 features and 10 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -81.7006 ymin: -4.215 xmax: -67.4858 ymax: 13.3817
## Geodetic CRS:  WGS 84
## # A tibble: 1,102 x 11
##    city         country  iso2  admin_name    capital population population_prop~
##  * <chr>        <chr>    <chr> <chr>         <chr>        <dbl>            <dbl>
##  1 Bogotá       Colombia CO    Bogotá        primary    9464000          7963000
##  2 Medellín     Colombia CO    Antioquia     admin      2529403          2529403
##  3 Cali         Colombia CO    Valle del Ca~ admin      2471474          2471474
##  4 Barranquilla Colombia CO    Atlántico     admin      1274250          1274250
##  5 Cartagena    Colombia CO    Bolívar       admin      1036412          1036412
##  6 Soacha       Colombia CO    Cundinamarca  minor       995268           995268
##  7 Palermo      Colombia CO    Huila         minor       800000           800000
##  8 Cúcuta       Colombia CO    Norte de San~ admin       750000           750000
##  9 Soledad      Colombia CO    Atlántico     minor       698852           342556
## 10 Pereira      Colombia CO    Risaralda     admin       590554           590554
## # ... with 1,092 more rows, and 4 more variables: geometry <POINT [°]>,
## #   MPIO_CCNCT <chr>, MPIO_CNMBR <chr>, MPIO_NAREA <dbl>

Se obtiene una tabla con todas las ciudades de cColombia y los municipios del departamento Norte de Santander, Para otras ciudades de otros departamentos, la columna de municipios aparece en blanco. Para organizar los datos se filtra la información correspondiente a Norte de Santander

nortestder.cities = dplyr::filter(sf.cities.joined, admin_name=='Norte de Santander')

nortestder.cities
## Simple feature collection with 39 features and 10 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -73.4478 ymin: 7.1378 xmax: -72.4075 ymax: 8.64
## Geodetic CRS:  WGS 84
## # A tibble: 39 x 11
##    city             country iso2  admin_name capital population population_prop~
##  * <chr>            <chr>   <chr> <chr>      <chr>        <dbl>            <dbl>
##  1 Cúcuta           Colomb~ CO    Norte de ~ admin       750000           750000
##  2 Ocaña            Colomb~ CO    Norte de ~ minor        90037            90037
##  3 Los Patios       Colomb~ CO    Norte de ~ minor        80235            80235
##  4 Villa del Rosar~ Colomb~ CO    Norte de ~ minor        69833            69833
##  5 Pamplona         Colomb~ CO    Norte de ~ minor        58975            58975
##  6 Tibú             Colomb~ CO    Norte de ~ minor        37286            37286
##  7 El Zulia         Colomb~ CO    Norte de ~ minor        23921            23921
##  8 Teorama          Colomb~ CO    Norte de ~ minor        23381            23381
##  9 Sardinata        Colomb~ CO    Norte de ~ minor        22583            22583
## 10 El Tarra         Colomb~ CO    Norte de ~ minor        10957            10957
## # ... with 29 more rows, and 4 more variables: geometry <POINT [°]>,
## #   MPIO_CCNCT <chr>, MPIO_CNMBR <chr>, MPIO_NAREA <dbl>

Mapa del grupo de cultivo más importante de Norte de Santander

Antes de unir un objeto estadisctico con uno espacial, se debe entender que los dos deben tener un atributo e común, en este caso el código del municipio. No obstante deben tener el mismo tipo o clase de dato “chr”.

library(tmap)
library(ggplot2)
library(ggrepel)
library(classInt)
class(leguminosas$Cod_Mun)
## [1] "numeric"
class(municipios$MPIO_CCNCT)
## [1] "character"
leguminosas$Cod_Mun = as.character(leguminosas$Cod_Mun)
class(leguminosas$Cod_Mun)
## [1] "character"

Ahora es posible hacer la unión, tendiendo siempre presente el atributo que se comparte en los dos tipos de archivos.

munic_leguminosas = left_join(municipios, leguminosas, by = c("MPIO_CCNCT" = "Cod_Mun"))

munic_leguminosas
## Simple feature collection with 41 features and 6 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.63379 ymin: 6.872201 xmax: -72.04761 ymax: 9.290847
## Geodetic CRS:  WGS 84
## First 10 features:
##    MPIO_CCNCT         MPIO_CNMBR MPIO_NAREA  Municipio
## 1       54001 SAN JOSÉ DE CÚCUTA  1133.5259     Cúcuta
## 2       54003             ÁBREGO  1382.4498     Ábrego
## 3       54051          ARBOLEDAS   456.1490  Arboledas
## 4       54099          BOCHALEMA   170.2670  Bochalema
## 5       54109         BUCARASICA   270.7909 Bucarasica
## 6       54125             CÁCOTA   138.9222     Cácota
## 7       54128            CÁCHIRA   615.8199    Cáchira
## 8       54172          CHINÁCOTA   166.2541  Chinácota
## 9       54223          CUCUTILLA   374.9105  Cucutilla
## 10      54239            DURANIA   175.0128    Durania
##                        Grupo max_prod                       geometry
## 1  Leguminosas Y Oleaginosas  6982.40 POLYGON ((-72.4778 8.430477...
## 2  Leguminosas Y Oleaginosas  1728.00 POLYGON ((-73.01687 8.28517...
## 3  Leguminosas Y Oleaginosas    15.00 POLYGON ((-72.73134 7.71823...
## 4  Leguminosas Y Oleaginosas   225.00 POLYGON ((-72.60265 7.7578,...
## 5  Leguminosas Y Oleaginosas    16.25 POLYGON ((-72.95019 8.17889...
## 6  Leguminosas Y Oleaginosas   126.17 POLYGON ((-72.62101 7.32978...
## 7  Leguminosas Y Oleaginosas    58.50 POLYGON ((-73.04222 7.86361...
## 8  Leguminosas Y Oleaginosas   180.00 POLYGON ((-72.58771 7.69713...
## 9  Leguminosas Y Oleaginosas     8.40 POLYGON ((-72.79776 7.64021...
## 10 Leguminosas Y Oleaginosas    54.00 POLYGON ((-72.63625 7.83530...
breaks <- classIntervals(munic_leguminosas$max_prod, n = 6, style = 'fisher')
## Warning in classIntervals(munic_leguminosas$max_prod, n = 6, style = "fisher"):
## var has missing values, omitted in finding classes
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_leguminosas <-  munic_leguminosas %>%
  mutate(faktor_class = factor(cut(max_prod, breaks$brks, include.lowest = T), labels = lab_vec))

munic_leguminosas$Produccion = munic_leguminosas$faktor_class
munic_leguminosas$mid <- sf::st_centroid(munic_leguminosas$geometry)

LONG = st_coordinates(munic_leguminosas$mid)[,1]

LAT = st_coordinates(munic_leguminosas$mid)[,2]
ggplot(data = munic_leguminosas) +
   geom_sf(aes(fill = Produccion)) +
   geom_label_repel(aes(x = LONG, y = LAT, label = MPIO_CNMBR), 
                    label.padding =     unit(0.05,"lines"),  
                    label.r = unit(0.025, "lines"),
                    label.size = 0.05)
## Warning: ggrepel: 8 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

facet = "max_prod"

leguminosas_map =  
  tm_shape(munic_leguminosas) + tm_polygons(facet) + tm_text(text = "MPIO_CNMBR", size = 0.7, fontfamily = "sans") +
  tm_shape(nortestder.cities) + tm_symbols(shape = 2, col = "red", size = 0.20) +
  tm_credits("Data source: UPRA (2020)", fontface = "bold") +
  tm_layout(main.title = "Produccion de leguminosas y oleaginosas 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
leguminosas_map
## Credits not supported in view mode.
## Symbol shapes other than circles or icons are not supported in view mode.

Mapa del grupo del segundo cultivo más importante de Norte de Santander

(cereales = read_csv("C:/Users/andre/OneDrive/Escritorio/Documentos U/12 matricula/Geomatica/Proyectos/nortestder_cereales_2020.csv",show_col_types = FALSE))
## # A tibble: 38 x 4
##    Cod_Mun Municipio         Grupo    max_prod
##      <dbl> <chr>             <chr>       <dbl>
##  1   54001 Cúcuta            Cereales   81791 
##  2   54261 El Zulia          Cereales   17947 
##  3   54553 Puerto Santander  Cereales    5497 
##  4   54810 Tibú              Cereales    4301 
##  5   54673 San Cayetano      Cereales    3894 
##  6   54385 La Esperanza      Cereales    3667 
##  7   54498 Ocaña             Cereales    1828.
##  8   54003 Ábrego            Cereales    1541.
##  9   54874 Villa del Rosario Cereales    1395 
## 10   54405 Los Patios        Cereales     810 
## # ... with 28 more rows
cereales$Cod_Mun = as.character(cereales$Cod_Mun)
munic_cereales = left_join(municipios, cereales, by = c("MPIO_CCNCT" = "Cod_Mun"))

munic_cereales
## Simple feature collection with 41 features and 6 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.63379 ymin: 6.872201 xmax: -72.04761 ymax: 9.290847
## Geodetic CRS:  WGS 84
## First 10 features:
##    MPIO_CCNCT         MPIO_CNMBR MPIO_NAREA  Municipio    Grupo max_prod
## 1       54001 SAN JOSÉ DE CÚCUTA  1133.5259     Cúcuta Cereales 81791.00
## 2       54003             ÁBREGO  1382.4498     Ábrego Cereales  1540.93
## 3       54051          ARBOLEDAS   456.1490  Arboledas Cereales    52.50
## 4       54099          BOCHALEMA   170.2670  Bochalema Cereales    29.00
## 5       54109         BUCARASICA   270.7909 Bucarasica Cereales    14.25
## 6       54125             CÁCOTA   138.9222     Cácota Cereales    40.00
## 7       54128            CÁCHIRA   615.8199       <NA>     <NA>       NA
## 8       54172          CHINÁCOTA   166.2541  Chinácota Cereales    10.00
## 9       54223          CUCUTILLA   374.9105  Cucutilla Cereales    69.44
## 10      54239            DURANIA   175.0128    Durania Cereales    60.00
##                          geometry
## 1  POLYGON ((-72.4778 8.430477...
## 2  POLYGON ((-73.01687 8.28517...
## 3  POLYGON ((-72.73134 7.71823...
## 4  POLYGON ((-72.60265 7.7578,...
## 5  POLYGON ((-72.95019 8.17889...
## 6  POLYGON ((-72.62101 7.32978...
## 7  POLYGON ((-73.04222 7.86361...
## 8  POLYGON ((-72.58771 7.69713...
## 9  POLYGON ((-72.79776 7.64021...
## 10 POLYGON ((-72.63625 7.83530...
breaks <- classIntervals(munic_cereales$max_prod, n = 6, style = 'fisher')
## Warning in classIntervals(munic_cereales$max_prod, n = 6, style = "fisher"): var
## has missing values, omitted in finding classes
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_cereales <- munic_cereales %>%
  mutate(faktor_class = factor(cut(max_prod, breaks$brks, include.lowest = T), labels = lab_vec))

munic_cereales$Produccion = munic_cereales$faktor_class

munic_cereales$mid <- sf::st_centroid(munic_cereales$geometry)

LONG = st_coordinates(munic_cereales$mid)[,1]

LAT = st_coordinates(munic_cereales$mid)[,2]
ggplot(data = munic_cereales) +
   geom_sf(aes(fill = Produccion)) +
   geom_label_repel(aes(x = LONG, y = LAT, label = MPIO_CNMBR), 
                    label.padding =     unit(0.05,"lines"),  
                    label.r = unit(0.025, "lines"),
                    label.size = 0.05)
## Warning: ggrepel: 8 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

facet = "max_prod"

cereales_map =  
  tm_shape(munic_cereales) + tm_polygons(facet) + tm_text(text = "MPIO_CNMBR", size = 0.7, fontfamily = "sans") +
  tm_shape(nortestder.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.