Introducción

En este cuaderno se desea mostrar cómo hacer mapas temáticos que muestren la participación municipal de los dos grupos de cultivos más importantes del departamento de Tolima.

Desarrollo

Se cargan las librerias a utilizar

library(sf)
## Linking to GEOS 3.11.1, GDAL 3.6.2, PROJ 9.1.1; sf_use_s2() is TRUE
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(readr)
library(readxl)

Se muestran los archivos “csv” disponibles en el directorio de trabajo

list.files("./Datos", pattern = c('csv'))
## [1] "co.csv"                  "Tolima_cereales.csv"    
## [3] "Tolima_tuber_&_plat.csv"

Se muestran los archivos “shapefiles” disponibles en el directorio de trabajo

list.files('./Municipios_Tolima')
## [1] "Municipios_Tolima.cpg" "Municipios_Tolima.dbf" "Municipios_Tolima.prj"
## [4] "Municipios_Tolima.shp" "Municipios_Tolima.shx"

Se leen los archivos base EVA que contienen la información de cereales del Tolima

(cereales = read.csv("./Datos/Tolima_cereales.csv"))
##    Cod_Mun         Municipio Gr_cultivo max_prod
## 1    73671           SALDAÑA   CEREALES    69069
## 2    73268           ESPINAL   CEREALES    67005
## 3    73319             GUAMO   CEREALES    60541
## 4    73585      PURIFICACION   CEREALES    58251
## 5    73001            IBAGUE   CEREALES    55200
## 6    73030          AMBALEMA   CEREALES    39830
## 7    73861         VENADILLO   CEREALES    25111
## 8    73408            LERIDA   CEREALES    23154
## 9    73547           PIEDRAS   CEREALES    18523
## 10   73055 ARMERO (GUAYABAL)   CEREALES    18416
## 11   73854 VALLE DE SAN JUAN   CEREALES    17220
## 12   73168         CHAPARRAL   CEREALES    16220
## 13   73563             PRADO   CEREALES    15106
## 14   73483         NATAGAIMA   CEREALES    14559
## 15   73026          ALVARADO   CEREALES    14024
## 16   73275           FLANDES   CEREALES    11354
## 17   73504            ORTEGA   CEREALES     9550
## 18   73217           COYAIMA   CEREALES     8390
## 19   73678          SAN LUIS   CEREALES     7949
## 20   73770            SUAREZ   CEREALES     4320
## 21   73200            COELLO   CEREALES     2800
## 22   73443         MARIQUITA   CEREALES     2722
## 23   73283            FRESNO   CEREALES     2000
## 24   73067             ATACO   CEREALES     1865
## 25   73024         ALPUJARRA   CEREALES     1654
## 26   73555          PLANADAS   CEREALES     1368
## 27   73270             FALAN   CEREALES     1260
## 28   73616         RIOBLANCO   CEREALES     1120
## 29   73624            ROVIRA   CEREALES      992
## 30   73043        ANZOATEGUI   CEREALES      968
## 31   73675       SAN ANTONIO   CEREALES      748
## 32   73520       PALOCABILDO   CEREALES      700
## 33   73411            LIBANO   CEREALES      640
## 34   73236           DOLORES   CEREALES      624
## 35   73349             HONDA   CEREALES      364
## 36   73124         CAJAMARCA   CEREALES      300
## 37   73347            HERVEO   CEREALES      300
## 38   73449            MELGAR   CEREALES      279
## 39   73622      RONCESVALLES   CEREALES      274
## 40   73870      VILLAHERMOSA   CEREALES      240
## 41   73873        VILLARRICA   CEREALES      240
## 42   73226            CUNDAY   CEREALES      175
## 43   73152        CASABIANCA   CEREALES      119
## 44   73352          ICONONZO   CEREALES       83
## 45   73148 CARMEN DE APICALA   CEREALES       43
## 46   73461           MURILLO   CEREALES       38
## 47   73686      SANTA ISABEL   CEREALES       35

Se leen los municipios del departamento Tolima

(munic.tmp = st_read('./Municipios_Tolima/Municipios_Tolima.shp'))
## Reading layer `Municipios_Tolima' from data source 
##   `C:\Users\laura\Desktop\Geomática_2023\Cuaderno_2_KT\Municipios_Tolima\Municipios_Tolima.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 47 features and 90 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -76.10574 ymin: 2.871081 xmax: -74.47482 ymax: 5.319342
## Geodetic CRS:  MAGNA-SIRGAS
## Simple feature collection with 47 features and 90 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -76.10574 ymin: 2.871081 xmax: -74.47482 ymax: 5.319342
## Geodetic CRS:  MAGNA-SIRGAS
## First 10 features:
##    DPTO_CCDGO MPIO_CCDGO                       MPIO_CNMBR MPIO_CDPMP VERSION
## 1          73        555                         PLANADAS      73555    2018
## 2          73        616                        RIOBLANCO      73616    2018
## 3          73        168                        CHAPARRAL      73168    2018
## 4          73        622                     RONCESVALLES      73622    2018
## 5          73        152                       CASABIANCA      73152    2018
## 6          73        347                           HERVEO      73347    2018
## 7          73        870                     VILLAHERMOSA      73870    2018
## 8          73        283                           FRESNO      73283    2018
## 9          73        443 SAN SEBASTIÃ\u0081N DE MARIQUITA      73443    2018
## 10         73        270                            FALAN      73270    2018
##          AREA  LATITUD  LONGITUD STCTNENCUE STP3_1_SI STP3_2_NO STP3A_RI
## 1  1754101831 3.098973 -75.81684       8999       359      8640      359
## 2  2046233792 3.468078 -75.85481      10173        49     10124       49
## 3  2102063236 3.743693 -75.58987      24131         0     24131        0
## 4   775849529 4.097861 -75.59428       2839         0      2839        0
## 5   175296179 5.016751 -75.18880       3575         0      3575        0
## 6   322743433 5.068554 -75.24323       4541         0      4541        0
## 7   278849941 4.965753 -75.15593       5218         0      5218        0
## 8   219661479 5.186695 -75.05229      12903         0     12903        0
## 9   293343154 5.235323 -74.90668      18061         0     18061        0
## 10  181322213 5.079257 -74.95703       4155         0      4155        0
##    STP3B_TCN STP4_1_SI STP4_2_NO STP9_1_USO STP9_2_USO STP9_3_USO STP9_4_USO
## 1          0       375      8624       7358        199       1438          4
## 2          0        48     10125       6682        602       2886          3
## 3          0        40     24091      18494        521       5096         20
## 4          0         4      2835       2065         64        708          2
## 5          0         0      3575       2341         74       1160          0
## 6          0         0      4541       2921         76       1543          1
## 7          0        14      5204       3626         90       1501          1
## 8          0         0     12903      10540        299       2062          2
## 9          0       387     17674      13328       1275       3451          7
## 10         0         0      4155       2926         67       1162          0
##    STP9_2_1_M STP9_2_2_M STP9_2_3_M STP9_2_4_M STP9_2_9_M STP9_3_1_N STP9_3_2_N
## 1           2        165         30          1          1          1        525
## 2           4        192         29        377          0          2        315
## 3           9        441         64          5          2         13       1017
## 4           2         53          8          1          0          6         80
## 5           0         59         15          0          0          0         83
## 6           0         65         11          0          0          0        130
## 7           0         82          8          0          0          4        207
## 8           6        235         46          9          3         14        512
## 9          41        498        233        503          0         52        568
## 10          1         61          5          0          0          2        107
##    STP9_3_3_N STP9_3_4_N STP9_3_5_N STP9_3_6_N STP9_3_7_N STP9_3_8_N STP9_3_9_N
## 1          81        262        102        334         17          0          4
## 2         229        592         89       1529          6          0          5
## 3         307        232        124       3107         76          5          3
## 4          45        188         48        312          2          1          1
## 5          49          8         53        917          3          0          1
## 6          55        264         50        994         14          0          0
## 7         130          8         68        982         17          0         11
## 8         202        300         99        852         18          4          1
## 9         393        410        109       1577         75          0         20
## 10         36        843         59         98          6          0          0
##    STP9_3_10 STP9_3_99 STVIVIENDA STP14_1_TI STP14_2_TI STP14_3_TI STP14_4_TI
## 1        109         3       7557       6748        404        332          9
## 2        115         4       7284       6570        427        272          2
## 3        203         9      19015      17323       1118        507         16
## 4         25         0       2129       1951         74        103          0
## 5         44         2       2415       2326         48         37          0
## 6         36         0       2997       2873         85         37          0
## 7         73         1       3716       3465        159         90          1
## 8         57         3      10839       8894       1533        404          1
## 9        245         2      14603      12150       1951        398          9
## 10        11         0       2993       2805        114         72          0
##    STP14_5_TI STP14_6_TI STP15_1_OC STP15_2_OC STP15_3_OC STP15_4_OC TSP16_HOG
## 1           1         63       6159         31       1077        290      6329
## 2           3         10       5546         11       1187        540      5670
## 3          10         41      14669         16       2637       1693     15012
## 4           0          1       1650          1         83        395      1737
## 5           1          3       1802          9        152        452      1930
## 6           2          0       2099          5        534        359      2163
## 7           1          0       2869          2         89        756      2950
## 8           1          6       9803         31        246        759     10150
## 9           0         95      11493        727       1023       1360     11790
## 10          0          2       2174        298        142        379      2230
##    STP19_EC_1 STP19_ES_2 STP19_EE_1 STP19_EE_2 STP19_EE_3 STP19_EE_4 STP19_EE_5
## 1        5595        564       3426       1890        106          6          2
## 2        4885        661       3504       1211         12          3          0
## 3       14204        465       8685       4327       1027         16          3
## 4        1520        130        850        647          8          1          0
## 5        1751         51        852        875          8          0          0
## 6        2041         58        560       1435         33          0          0
## 7        2688        181        802       1789         83          1          0
## 8        9627        176       4043       4435       1099          8          0
## 9       11388        105       5670       4819        714         61          5
## 10       2100         74       1139        919         23          0          3
##    STP19_EE_6 STP19_EE_9 STP19_ACU1 STP19_ACU2 STP19_ALC1 STP19_ALC2 STP19_GAS1
## 1           2        163       3259       2900       2600       3559       1418
## 2           1        154       2915       2631       2004       3542       1629
## 3           1        145      10726       3943       8888       5781       7355
## 4           0         14        762        888        718        932         15
## 5           1         15       1243        559        629       1173        541
## 6           0         13       1161        938       1065       1034       1128
## 7           1         12       1409       1460       1045       1824        819
## 8           2         40       6841       2962       5615       4188       5268
## 9           5        114       9910       1583       8874       2619       8354
## 10          1         15       1720        454        652       1522        799
##    STP19_GAS2 STP19_GAS9 STP19_REC1 STP19_REC2 STP19_INT1 STP19_INT2 STP19_INT9
## 1        4711         30       2473       3686        409       5715         35
## 2        3886         31       1399       4147        265       5250         31
## 3        7189        125       8957       5712       2333      12202        134
## 4        1619         16        673        977         41       1593         16
## 5        1247         14        659       1143        109       1679         14
## 6         968          3       1114        985        297       1799          3
## 7        2033         17       1128       1741        180       2672         17
## 8        4484         51       5465       4338        937       8812         54
## 9        3007        132       8956       2537       2241       9120        132
## 10       1359         16        721       1453        158       2000         16
##    STP27_PERS STPERSON_L STPERSON_S STP32_1_SE STP32_2_SE STP34_1_ED STP34_2_ED
## 1       21557         54      21503      11246      10311       4241       4765
## 2       19090         25      19065      10099       8991       3951       4144
## 3       43795        574      43221      22203      21592       7305       8686
## 4        5161         68       5093       2741       2420        862       1085
## 5        5496          0       5496       2958       2538        832       1032
## 6        6368          7       6361       3307       3061        863       1153
## 7        8530         10       8520       4492       4038       1332       1562
## 8       28920        144      28776      14873      14047       4054       5204
## 9       34505        107      34398      16884      17621       4717       5699
## 10       6612          0       6612       3445       3167        882       1166
##    STP34_3_ED STP34_4_ED STP34_5_ED STP34_6_ED STP34_7_ED STP34_8_ED STP34_9_ED
## 1        3407       2868       2307       1972       1065        648        284
## 2        2989       2425       2017       1738        996        583        247
## 3        6483       5607       4800       4557       3196       2039       1122
## 4         768        671        639        559        325        192         60
## 5         713        708        675        700        470        250        116
## 6         841        787        807        878        590        310        139
## 7        1058       1059       1095       1081        815        402        126
## 8        4110       3951       3516       3567       2574       1327        617
## 9        4990       4652       4395       4201       3114       1789        948
## 10        816        727        783        928        701        408        201
##    STP51_PRIM STP51_SECU STP51_SUPE STP51_POST STP51_13_E STP51_99_E Shape_Leng
## 1        9976       6847        854        119       1470        215  2.0804727
## 2        9030       5547        751        116       1611        148  2.3105836
## 3       17953      14958       3167        501       3026        770  2.5792724
## 4        2409       1725        294         24        273         50  1.4122546
## 5        2726       1688        196         35        440         36  1.0707227
## 6        3136       1989        323         53        467         37  1.0242400
## 7        4498       2480        196         63        623         60  0.9470417
## 8       12567      10196       1879        276       1921        234  0.8488067
## 9       12327      14020       3717        351       1516        399  0.8394114
## 10       3022       2176        321         94        563         39  0.8617616
##    Shape_Area                       geometry
## 1  0.14257548 POLYGON ((-75.72356 3.31827...
## 2  0.16637385 POLYGON ((-75.83841 3.77412...
## 3  0.17100865 POLYGON ((-75.74412 4.04007...
## 4  0.06314353 POLYGON ((-75.59634 4.27247...
## 5  0.01428912 POLYGON ((-75.07491 5.12314...
## 6  0.02630925 POLYGON ((-75.15427 5.17053...
## 7  0.02272901 POLYGON ((-75.11157 5.06709...
## 8  0.01791172 POLYGON ((-75.00927 5.29282...
## 9  0.02392361 POLYGON ((-74.93615 5.31237...
## 10 0.01478382 POLYGON ((-74.94871 5.17345...

Se seleccionan algunos atributos para limpiar el proyecto

munic.tmp %>% select(MPIO_CCDGO, MPIO_CNMBR, AREA) -> municipios
municipios
## Simple feature collection with 47 features and 3 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -76.10574 ymin: 2.871081 xmax: -74.47482 ymax: 5.319342
## Geodetic CRS:  MAGNA-SIRGAS
## First 10 features:
##    MPIO_CCDGO                       MPIO_CNMBR       AREA
## 1         555                         PLANADAS 1754101831
## 2         616                        RIOBLANCO 2046233792
## 3         168                        CHAPARRAL 2102063236
## 4         622                     RONCESVALLES  775849529
## 5         152                       CASABIANCA  175296179
## 6         347                           HERVEO  322743433
## 7         870                     VILLAHERMOSA  278849941
## 8         283                           FRESNO  219661479
## 9         443 SAN SEBASTIÃ\u0081N DE MARIQUITA  293343154
## 10        270                            FALAN  181322213
##                          geometry
## 1  POLYGON ((-75.72356 3.31827...
## 2  POLYGON ((-75.83841 3.77412...
## 3  POLYGON ((-75.74412 4.04007...
## 4  POLYGON ((-75.59634 4.27247...
## 5  POLYGON ((-75.07491 5.12314...
## 6  POLYGON ((-75.15427 5.17053...
## 7  POLYGON ((-75.11157 5.06709...
## 8  POLYGON ((-75.00927 5.29282...
## 9  POLYGON ((-74.93615 5.31237...
## 10 POLYGON ((-74.94871 5.17345...

Se leen las ciudades del archivo csv

(ciudades = read_csv("./Datos/co.csv"))
## Rows: 1102 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): city, country, iso2, admin_name, capital
## dbl (4): lat, lng, population, population_proper
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## # A tibble: 1,102 × 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
## # ℹ 1,092 more rows
## # ℹ 1 more variable: population_proper <dbl>

Se convierte el objeto ciudades a un objeto espacial de característica simple (simple feature object)

sf.ciudades <- st_as_sf(x = ciudades, coords = c("lng", "lat"))
sf.ciudades
## 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 × 8
##    city         country  iso2  admin_name   capital population population_proper
##    <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 C… 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 Sa… admin       750000            750000
##  9 Soledad      Colombia CO    Atlántico    minor       698852            342556
## 10 Pereira      Colombia CO    Risaralda    admin       590554            590554
## # ℹ 1,092 more rows
## # ℹ 1 more variable: geometry <POINT>

Se añaden coordenadas a través del código EPSG

st_crs(sf.ciudades) <- 4326
st_crs(municipios) <- 4326
## Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
## that
st_transform(municipios, 4326)
## Simple feature collection with 47 features and 3 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -76.10574 ymin: 2.871081 xmax: -74.47482 ymax: 5.319342
## Geodetic CRS:  WGS 84
## First 10 features:
##    MPIO_CCDGO                       MPIO_CNMBR       AREA
## 1         555                         PLANADAS 1754101831
## 2         616                        RIOBLANCO 2046233792
## 3         168                        CHAPARRAL 2102063236
## 4         622                     RONCESVALLES  775849529
## 5         152                       CASABIANCA  175296179
## 6         347                           HERVEO  322743433
## 7         870                     VILLAHERMOSA  278849941
## 8         283                           FRESNO  219661479
## 9         443 SAN SEBASTIÃ\u0081N DE MARIQUITA  293343154
## 10        270                            FALAN  181322213
##                          geometry
## 1  POLYGON ((-75.72356 3.31827...
## 2  POLYGON ((-75.83841 3.77412...
## 3  POLYGON ((-75.74412 4.04007...
## 4  POLYGON ((-75.59634 4.27247...
## 5  POLYGON ((-75.07491 5.12314...
## 6  POLYGON ((-75.15427 5.17053...
## 7  POLYGON ((-75.11157 5.06709...
## 8  POLYGON ((-75.00927 5.29282...
## 9  POLYGON ((-74.93615 5.31237...
## 10 POLYGON ((-74.94871 5.17345...
st_transform(sf.ciudades, 4326)
## 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
## Geodetic CRS:  WGS 84
## # A tibble: 1,102 × 8
##    city         country  iso2  admin_name   capital population population_proper
##  * <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 C… 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 Sa… admin       750000            750000
##  9 Soledad      Colombia CO    Atlántico    minor       698852            342556
## 10 Pereira      Colombia CO    Risaralda    admin       590554            590554
## # ℹ 1,092 more rows
## # ℹ 1 more variable: geometry <POINT [°]>

Se crea una unión espacial para obtener datos relevantes de Tolima

sf.ciudades.unidas <- st_join(sf.ciudades, municipios, join = st_within)
sf.ciudades.unidas
## 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 × 11
##    city         country  iso2  admin_name   capital population population_proper
##  * <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 C… 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 Sa… admin       750000            750000
##  9 Soledad      Colombia CO    Atlántico    minor       698852            342556
## 10 Pereira      Colombia CO    Risaralda    admin       590554            590554
## # ℹ 1,092 more rows
## # ℹ 4 more variables: geometry <POINT [°]>, MPIO_CCDGO <chr>, MPIO_CNMBR <chr>,
## #   AREA <dbl>

Se filtra la información correspondiente a Tolima

tolima.ciudades = dplyr::filter(sf.ciudades.unidas, admin_name == 'Tolima')
tolima.ciudades
## Simple feature collection with 47 features and 10 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -75.6447 ymin: 3.1964 xmax: -74.5328 ymax: 5.2069
## Geodetic CRS:  WGS 84
## # A tibble: 47 × 11
##    city         country  iso2  admin_name capital population population_proper
##  * <chr>        <chr>    <chr> <chr>      <chr>        <dbl>             <dbl>
##  1 Ibagué       Colombia CO    Tolima     admin       541101            541101
##  2 Espinal      Colombia CO    Tolima     minor        75828             75828
##  3 Chaparral    Colombia CO    Tolima     minor        47397             47397
##  4 Líbano       Colombia CO    Tolima     minor        39665             39665
##  5 Melgar       Colombia CO    Tolima     minor        37224             37224
##  6 Guamo        Colombia CO    Tolima     minor        31350             31350
##  7 Planadas     Colombia CO    Tolima     minor        30117             30117
##  8 Fresno       Colombia CO    Tolima     minor        29794             29794
##  9 Purificación Colombia CO    Tolima     minor        29777             29777
## 10 Flandes      Colombia CO    Tolima     minor        29478             29478
## # ℹ 37 more rows
## # ℹ 4 more variables: geometry <POINT [°]>, MPIO_CCDGO <chr>, MPIO_CNMBR <chr>,
## #   AREA <dbl>

Se obtuvieron 46 municipios y su capital Ibagué.

Con los datos obtenidos, se realiza un mapa.

Para ello, se necesitan las librerias tmap, ggplot2, ggrepel y classInt.

library(tmap)
library(ggplot2)
library(ggrepel)
library(classInt)
library(tidyr)

Para poder realizar la unión entre el objeto municipios y el objeto cereales, se necesita hallar un atributo en común. Por lo que se usará el código municipal. Pero su nombre y su tipo en cada objeto, es diferente

class(cereales$Cod_Mun)
## [1] "integer"
class(municipios$MPIO_CCDGO)
## [1] "character"

Por lo anterior, se cambia el tipo que tiene el código municipal en el objeto cereales

cereales$Cod_Mun = as.character(cereales$Cod_Mun)
class(cereales$Cod_Mun)
## [1] "character"
class(municipios$MPIO_CCDGO)
## [1] "character"

Se observan los encabezados de los objetos “cereales” y “municipios”

head(cereales)
##   Cod_Mun    Municipio Gr_cultivo max_prod
## 1   73671      SALDAÑA   CEREALES    69069
## 2   73268      ESPINAL   CEREALES    67005
## 3   73319        GUAMO   CEREALES    60541
## 4   73585 PURIFICACION   CEREALES    58251
## 5   73001       IBAGUE   CEREALES    55200
## 6   73030     AMBALEMA   CEREALES    39830
head(municipios)
## Simple feature collection with 6 features and 3 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -76.10574 ymin: 2.871081 xmax: -75.04211 ymax: 5.171479
## Geodetic CRS:  WGS 84
##   MPIO_CCDGO   MPIO_CNMBR       AREA                       geometry
## 1        555     PLANADAS 1754101831 POLYGON ((-75.72356 3.31827...
## 2        616    RIOBLANCO 2046233792 POLYGON ((-75.83841 3.77412...
## 3        168    CHAPARRAL 2102063236 POLYGON ((-75.74412 4.04007...
## 4        622 RONCESVALLES  775849529 POLYGON ((-75.59634 4.27247...
## 5        152   CASABIANCA  175296179 POLYGON ((-75.07491 5.12314...
## 6        347       HERVEO  322743433 POLYGON ((-75.15427 5.17053...

Como los datos de la columna “Cod_Mun” de cereales no coinciden con los de la columna “MPIO_CCDGO”, se añade el prefijo “73” que le falta a municipios

municipios$prefijo = "73"

Por medio de “unite” se cambia el nombre de la columna “MPIO_CCDGO” por “Codigo”, se añade el prefijo 73 a los datos de “Codigo”, dejando esta interaccción sin espacios (sep = ““). Este proceso se guarda en”municipios2”

municipios %>% unite(Codigo, c("prefijo","MPIO_CCDGO"), sep = "") -> municipios2
head(municipios2)
## Simple feature collection with 6 features and 3 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -76.10574 ymin: 2.871081 xmax: -75.04211 ymax: 5.171479
## Geodetic CRS:  WGS 84
##   Codigo   MPIO_CNMBR       AREA                       geometry
## 1  73555     PLANADAS 1754101831 POLYGON ((-75.72356 3.31827...
## 2  73616    RIOBLANCO 2046233792 POLYGON ((-75.83841 3.77412...
## 3  73168    CHAPARRAL 2102063236 POLYGON ((-75.74412 4.04007...
## 4  73622 RONCESVALLES  775849529 POLYGON ((-75.59634 4.27247...
## 5  73152   CASABIANCA  175296179 POLYGON ((-75.07491 5.12314...
## 6  73347       HERVEO  322743433 POLYGON ((-75.15427 5.17053...

Ahora ya se puede realizar la unión de los objetos municipios2 y cereales por medio del código municipal

municipios_cereales = left_join(municipios2, cereales, by = c("Codigo" = "Cod_Mun"))
municipios_cereales
## Simple feature collection with 47 features and 6 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -76.10574 ymin: 2.871081 xmax: -74.47482 ymax: 5.319342
## Geodetic CRS:  WGS 84
## First 10 features:
##    Codigo                       MPIO_CNMBR       AREA    Municipio Gr_cultivo
## 1   73555                         PLANADAS 1754101831     PLANADAS   CEREALES
## 2   73616                        RIOBLANCO 2046233792    RIOBLANCO   CEREALES
## 3   73168                        CHAPARRAL 2102063236    CHAPARRAL   CEREALES
## 4   73622                     RONCESVALLES  775849529 RONCESVALLES   CEREALES
## 5   73152                       CASABIANCA  175296179   CASABIANCA   CEREALES
## 6   73347                           HERVEO  322743433       HERVEO   CEREALES
## 7   73870                     VILLAHERMOSA  278849941 VILLAHERMOSA   CEREALES
## 8   73283                           FRESNO  219661479       FRESNO   CEREALES
## 9   73443 SAN SEBASTIÃ\u0081N DE MARIQUITA  293343154    MARIQUITA   CEREALES
## 10  73270                            FALAN  181322213        FALAN   CEREALES
##    max_prod                       geometry
## 1      1368 POLYGON ((-75.72356 3.31827...
## 2      1120 POLYGON ((-75.83841 3.77412...
## 3     16220 POLYGON ((-75.74412 4.04007...
## 4       274 POLYGON ((-75.59634 4.27247...
## 5       119 POLYGON ((-75.07491 5.12314...
## 6       300 POLYGON ((-75.15427 5.17053...
## 7       240 POLYGON ((-75.11157 5.06709...
## 8      2000 POLYGON ((-75.00927 5.29282...
## 9      2722 POLYGON ((-74.93615 5.31237...
## 10     1260 POLYGON ((-74.94871 5.17345...

Se le dan las características finales al mapa de producción de cereales en el departamento del Tolima

facet = "max_prod"
cereales_map =  
  tm_shape(municipios_cereales) + tm_polygons(facet) + tm_text(text = "Municipio", size = 0.7, fontfamily = "sans") +
  tm_shape(tolima.ciudades) + tm_symbols(shape = 2, col = "red", size = 0.20) +
  tm_credits("Data source: UPRA (2022)", fontface = "bold") +
  tm_layout(main.title = "Produccion cereales en 2022",
            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.

Ahora se quiere realizar el mapa de la segunda producción más alta en el Tolima: Tuberculos y platanos.

tuber_plat = read_csv("./Datos/Tolima_tuber_&_plat.csv", show_col_types = FALSE)
tuber_plat
## # A tibble: 47 × 4
##    Cod_Mun Municipio         Gr_cultivo            max_prod
##      <dbl> <chr>             <chr>                    <dbl>
##  1   73124 CAJAMARCA         TUBERCULOS Y PLATANOS    60000
##  2   73555 PLANADAS          TUBERCULOS Y PLATANOS    41700
##  3   73067 ATACO             TUBERCULOS Y PLATANOS    25603
##  4   73001 IBAGUE            TUBERCULOS Y PLATANOS    20295
##  5   73217 COYAIMA           TUBERCULOS Y PLATANOS    18408
##  6   73283 FRESNO            TUBERCULOS Y PLATANOS    16881
##  7   73411 LIBANO            TUBERCULOS Y PLATANOS    16800
##  8   73624 ROVIRA            TUBERCULOS Y PLATANOS    15100
##  9   73504 ORTEGA            TUBERCULOS Y PLATANOS    15050
## 10   73854 VALLE DE SAN JUAN TUBERCULOS Y PLATANOS    15000
## # ℹ 37 more rows

Se comprueba si se puede hacer la unión de los objetos “tuber_plat” y “municipios” por medio del código municipal

class(tuber_plat$Cod_Mun)
## [1] "numeric"
class(municipios$MPIO_CCDGO)
## [1] "character"

Al ser de diferente tipo, se cambia el del la columna “Cod_Mun”

tuber_plat$Cod_Mun = as.character(tuber_plat$Cod_Mun)
class(tuber_plat$Cod_Mun)
## [1] "character"
class(municipios$MPIO_CCDGO)
## [1] "character"

Se comprueba los datos de las columnas “Cod_Mun”(objeto “tuber_plat”) y “MPIO_CCDGO” (objeto “municipios”)

head(tuber_plat)
## # A tibble: 6 × 4
##   Cod_Mun Municipio Gr_cultivo            max_prod
##   <chr>   <chr>     <chr>                    <dbl>
## 1 73124   CAJAMARCA TUBERCULOS Y PLATANOS    60000
## 2 73555   PLANADAS  TUBERCULOS Y PLATANOS    41700
## 3 73067   ATACO     TUBERCULOS Y PLATANOS    25603
## 4 73001   IBAGUE    TUBERCULOS Y PLATANOS    20295
## 5 73217   COYAIMA   TUBERCULOS Y PLATANOS    18408
## 6 73283   FRESNO    TUBERCULOS Y PLATANOS    16881
head(municipios)
## Simple feature collection with 6 features and 4 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -76.10574 ymin: 2.871081 xmax: -75.04211 ymax: 5.171479
## Geodetic CRS:  WGS 84
##   MPIO_CCDGO   MPIO_CNMBR       AREA                       geometry prefijo
## 1        555     PLANADAS 1754101831 POLYGON ((-75.72356 3.31827...      73
## 2        616    RIOBLANCO 2046233792 POLYGON ((-75.83841 3.77412...      73
## 3        168    CHAPARRAL 2102063236 POLYGON ((-75.74412 4.04007...      73
## 4        622 RONCESVALLES  775849529 POLYGON ((-75.59634 4.27247...      73
## 5        152   CASABIANCA  175296179 POLYGON ((-75.07491 5.12314...      73
## 6        347       HERVEO  322743433 POLYGON ((-75.15427 5.17053...      73

Como los datos de las columnas del código municipal no coniciden, se agrega el prefijo faltante: “73”

municipios$prefi = "73"

Por medio de “unite” se cambia el nombre de la columna “MPIO_CCDGO” por “Codigo”, se añade el prefijo 73 a los datos de “Codigo”, dejando esta interaccción sin espacios (sep = ““). Este proceso se guarda en”municipios3”

municipios%>% unite(Codigo, c("prefi","MPIO_CCDGO"), sep="") -> municipios3
head(municipios3)
## Simple feature collection with 6 features and 4 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -76.10574 ymin: 2.871081 xmax: -75.04211 ymax: 5.171479
## Geodetic CRS:  WGS 84
##   Codigo   MPIO_CNMBR       AREA prefijo                       geometry
## 1  73555     PLANADAS 1754101831      73 POLYGON ((-75.72356 3.31827...
## 2  73616    RIOBLANCO 2046233792      73 POLYGON ((-75.83841 3.77412...
## 3  73168    CHAPARRAL 2102063236      73 POLYGON ((-75.74412 4.04007...
## 4  73622 RONCESVALLES  775849529      73 POLYGON ((-75.59634 4.27247...
## 5  73152   CASABIANCA  175296179      73 POLYGON ((-75.07491 5.12314...
## 6  73347       HERVEO  322743433      73 POLYGON ((-75.15427 5.17053...

Ahora ya se puede realizar la unión de los objetos “municipios3” y “tuber_plat” por medio del código municipal

municipios_tuber_plat = left_join(municipios3, tuber_plat, by = c("Codigo" = "Cod_Mun"))
municipios_tuber_plat
## Simple feature collection with 47 features and 7 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -76.10574 ymin: 2.871081 xmax: -74.47482 ymax: 5.319342
## Geodetic CRS:  WGS 84
## First 10 features:
##    Codigo                       MPIO_CNMBR       AREA prefijo    Municipio
## 1   73555                         PLANADAS 1754101831      73     PLANADAS
## 2   73616                        RIOBLANCO 2046233792      73    RIOBLANCO
## 3   73168                        CHAPARRAL 2102063236      73    CHAPARRAL
## 4   73622                     RONCESVALLES  775849529      73 RONCESVALLES
## 5   73152                       CASABIANCA  175296179      73   CASABIANCA
## 6   73347                           HERVEO  322743433      73       HERVEO
## 7   73870                     VILLAHERMOSA  278849941      73 VILLAHERMOSA
## 8   73283                           FRESNO  219661479      73       FRESNO
## 9   73443 SAN SEBASTIÃ\u0081N DE MARIQUITA  293343154      73    MARIQUITA
## 10  73270                            FALAN  181322213      73        FALAN
##               Gr_cultivo max_prod                       geometry
## 1  TUBERCULOS Y PLATANOS    41700 POLYGON ((-75.72356 3.31827...
## 2  TUBERCULOS Y PLATANOS     6400 POLYGON ((-75.83841 3.77412...
## 3  TUBERCULOS Y PLATANOS     4500 POLYGON ((-75.74412 4.04007...
## 4  TUBERCULOS Y PLATANOS    10540 POLYGON ((-75.59634 4.27247...
## 5  TUBERCULOS Y PLATANOS    10250 POLYGON ((-75.07491 5.12314...
## 6  TUBERCULOS Y PLATANOS     8139 POLYGON ((-75.15427 5.17053...
## 7  TUBERCULOS Y PLATANOS    12570 POLYGON ((-75.11157 5.06709...
## 8  TUBERCULOS Y PLATANOS    16881 POLYGON ((-75.00927 5.29282...
## 9  TUBERCULOS Y PLATANOS     5600 POLYGON ((-74.93615 5.31237...
## 10 TUBERCULOS Y PLATANOS    14800 POLYGON ((-74.94871 5.17345...

Finalmente, se le dan las características al mapa de producción de turberculos y platanos en el departamento del Tolima

facet = "max_prod"
tuber_plat_map =  
  tm_shape(municipios_tuber_plat) + tm_polygons(facet) + tm_text(text = "Municipio", size = 0.7, fontfamily = "sans") +
  tm_shape(tolima.ciudades) + tm_symbols(shape = 2, col = "red", size = 0.20) +
  tm_credits("Data source: UPRA (2022)", fontface = "bold") +
  tm_layout(main.title = "Produccion de tuberculos y platanos en 2022",
            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
tuber_plat_map
## Credits not supported in view mode.
## Symbol shapes other than circles or icons are not supported in view mode.

Este cuaderno se logró gracias a la guía de:

Lizarazo, I., 2022. Getting started with thematic maps. Disponible en: https://rpubs.com/ials2un/thematic_maps_v2.

sessionInfo()
## R version 4.3.0 (2023-04-21 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 11 x64 (build 22000)
## 
## Matrix products: default
## 
## 
## locale:
## [1] LC_COLLATE=Spanish_Colombia.utf8  LC_CTYPE=Spanish_Colombia.utf8   
## [3] LC_MONETARY=Spanish_Colombia.utf8 LC_NUMERIC=C                     
## [5] LC_TIME=Spanish_Colombia.utf8    
## 
## time zone: America/Bogota
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] tidyr_1.3.0    classInt_0.4-9 ggrepel_0.9.3  ggplot2_3.4.2  tmap_3.3-3    
## [6] readxl_1.4.2   readr_2.1.4    dplyr_1.1.2    sf_1.0-12     
## 
## loaded via a namespace (and not attached):
##  [1] gtable_0.3.3            xfun_0.39               bslib_0.4.2            
##  [4] raster_3.6-20           htmlwidgets_1.6.2       lattice_0.21-8         
##  [7] tzdb_0.3.0              leaflet.providers_1.9.0 vctrs_0.6.2            
## [10] tools_4.3.0             crosstalk_1.2.0         generics_0.1.3         
## [13] parallel_4.3.0          tibble_3.2.1            proxy_0.4-27           
## [16] fansi_1.0.4             pkgconfig_2.0.3         KernSmooth_2.23-20     
## [19] RColorBrewer_1.1-3      leaflet_2.1.2           lifecycle_1.0.3        
## [22] compiler_4.3.0          munsell_0.5.0           terra_1.7-29           
## [25] codetools_0.2-19        leafsync_0.1.0          stars_0.6-1            
## [28] htmltools_0.5.5         class_7.3-21            sass_0.4.5             
## [31] yaml_2.3.7              pillar_1.9.0            crayon_1.5.2           
## [34] jquerylib_0.1.4         ellipsis_0.3.2          cachem_1.0.7           
## [37] lwgeom_0.2-11           wk_0.7.2                abind_1.4-5            
## [40] mime_0.12               tidyselect_1.2.0        digest_0.6.31          
## [43] purrr_1.0.1             fastmap_1.1.1           grid_4.3.0             
## [46] colorspace_2.1-0        cli_3.6.1               magrittr_2.0.3         
## [49] base64enc_0.1-3         dichromat_2.0-0.1       XML_3.99-0.14          
## [52] utf8_1.2.3              leafem_0.2.0            e1071_1.7-13           
## [55] withr_2.5.0             scales_1.2.1            sp_1.6-0               
## [58] bit64_4.0.5             rmarkdown_2.21          bit_4.0.5              
## [61] cellranger_1.1.0        png_0.1-8               hms_1.1.3              
## [64] evaluate_0.20           knitr_1.42              tmaptools_3.1-1        
## [67] viridisLite_0.4.1       markdown_1.6            s2_1.1.3               
## [70] rlang_1.1.1             Rcpp_1.0.10             glue_1.6.2             
## [73] DBI_1.1.3               rstudioapi_0.14         vroom_1.6.3            
## [76] jsonlite_1.8.4          R6_2.5.1                units_0.8-2