library(sf)
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.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)
list.files("C:/Users/EPI_m/OneDrive/Escritorio/Geomatica/Rstudio", pattern=c('csv'))
## [1] "Evaluaciones_Agropecuarias_Municipales_EVA.csv"
## [2] "Vichada_Cereales_2020.csv"                     
## [3] "vichada_platanos_2020.csv"                     
## [4] "vichada_Tuberculos_2020.csv"
list.files('C:/Users/EPI_m/OneDrive/Escritorio/Geomatica/Rstudio')
##  [1] "20210624_BaseSIPRA2020.xlsx"                   
##  [2] "Evaluaciones_Agropecuarias_Municipales_EVA.csv"
##  [3] "Mi-primer-cuaderno2.html"                      
##  [4] "Mi-primer-cuaderno2.tex"                       
##  [5] "Mi-segundo-cuaderno.html"                      
##  [6] "Mi-segundo-cuaderno.Rmd"                       
##  [7] "Mi-tercer.cuaderno.html"                       
##  [8] "Mi-tercer.cuaderno.rmd"                        
##  [9] "Mi primer cuaderno2.nb.html"                   
## [10] "Mi primer cuaderno2.Rmd"                       
## [11] "rsconnect"                                     
## [12] "Vichada_Cereales_2020.csv"                     
## [13] "vichada_platanos_2020.csv"                     
## [14] "vichada_Tuberculos_2020.csv"
(Platanos = read_csv("C:/Users/EPI_m/OneDrive/Escritorio/Geomatica/Rstudio/vichada_platanos_2020.csv",show_col_types = FALSE))
## # A tibble: 4 x 4
##   Cod_Mun Municipio      Grupo    max_prod
##     <dbl> <chr>          <chr>       <dbl>
## 1   99773 Cumaribo       Plátanos    10250
## 2   99524 La Primavera   Plátanos      657
## 3   99624 Santa Rosalía  Plátanos      320
## 4   99001 Puerto Carreño Plátanos      180
(mun.tmp =  st_read('C:/Users/EPI_m/OneDrive/Escritorio/Geomatica/Qgis/Datos/Municipios_Vichada.shp'))
## Reading layer `Municipios_Vichada' from data source 
##   `C:\Users\EPI_m\OneDrive\Escritorio\Geomatica\Qgis\Datos\Municipios_Vichada.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 4 features and 11 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -71.07793 ymin: 2.737109 xmax: -67.4098 ymax: 6.324317
## Geodetic CRS:  WGS 84
## Simple feature collection with 4 features and 11 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -71.07793 ymin: 2.737109 xmax: -67.4098 ymax: 6.324317
## Geodetic CRS:  WGS 84
##   DPTO_CCDGO MPIO_CCDGO     MPIO_CNMBR
## 1         99        624  SANTA ROSALÍA
## 2         99        001 PUERTO CARREÑO
## 3         99        524   LA PRIMAVERA
## 4         99        773       CUMARIBO
##                                                   MPIO_CRSLC MPIO_NAREA
## 1 Modificado por Ordenanza No. 21 de 30 de Noviembre de 2019   3691.869
## 2                             Decreto 1594 de Ago 5  de 1974  12204.951
## 3 Modificado por Ordenanza No. 21 de 30 de Noviembre de 2019  18569.338
## 4                       Ordenanza 66 de Noviembre 22 de 1996  65597.212
##   MPIO_CCNCT MPIO_NANO DPTO_CNMBR SHAPE_AREA SHAPE_LEN ORIG_FID
## 1      99624      2020    VICHADA  0.2999597  3.805847       51
## 2      99001      2020    VICHADA  0.9859169  5.474851       52
## 3      99524      2020    VICHADA  1.5062235  8.080061       53
## 4      99773      2020    VICHADA  5.3085803 18.794383       54
##                         geometry
## 1 POLYGON ((-70.65603 5.37316...
## 2 POLYGON ((-67.80972 6.32431...
## 3 POLYGON ((-69.0331 6.218449...
## 4 POLYGON ((-68.47074 5.55046...
mun.tmp %>% select(MPIO_CCDGO, MPIO_CCNCT, MPIO_NAREA,MPIO_CNMBR) -> municipios
municipios
## Simple feature collection with 4 features and 4 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -71.07793 ymin: 2.737109 xmax: -67.4098 ymax: 6.324317
## Geodetic CRS:  WGS 84
##   MPIO_CCDGO MPIO_CCNCT MPIO_NAREA     MPIO_CNMBR
## 1        624      99624   3691.869  SANTA ROSALÍA
## 2        001      99001  12204.951 PUERTO CARREÑO
## 3        524      99524  18569.338   LA PRIMAVERA
## 4        773      99773  65597.212       CUMARIBO
##                         geometry
## 1 POLYGON ((-70.65603 5.37316...
## 2 POLYGON ((-67.80972 6.32431...
## 3 POLYGON ((-69.0331 6.218449...
## 4 POLYGON ((-68.47074 5.55046...
(cities = read_csv("C:/Users/EPI_m/OneDrive/Escritorio/Geomatica/Qgis/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
## 
## 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>
## Rows: 1102 Columns: 9
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>
st_crs(sf.cities) <- 4326
sf.cities.joined <- st_join(sf.cities, municipios, join = st_within)
sf.cities.joined
## Simple feature collection with 1102 features and 11 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 12
##    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 5 more variables: geometry <POINT [°]>,
## #   MPIO_CCDGO <chr>, MPIO_CCNCT <chr>, MPIO_NAREA <dbl>, MPIO_CNMBR <chr>
Vichada.cities = dplyr::filter(sf.cities.joined, admin_name=='Vichada')
Vichada.cities
## Simple feature collection with 4 features and 11 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -70.8756 ymin: 4.4461 xmax: -67.4858 ymax: 6.1889
## Geodetic CRS:  WGS 84
## # A tibble: 4 x 12
##   city           country  iso2  admin_name capital population population_proper
## * <chr>          <chr>    <chr> <chr>      <chr>        <dbl>             <dbl>
## 1 Cumaribo       Colombia CO    Vichada    minor        43138             43138
## 2 La Primavera   Colombia CO    Vichada    minor        17626             17626
## 3 Puerto Carreño Colombia CO    Vichada    admin        16763             16763
## 4 Santa Rosalía  Colombia CO    Vichada    minor         4255              4255
## # ... with 5 more variables: geometry <POINT [°]>, MPIO_CCDGO <chr>,
## #   MPIO_CCNCT <chr>, MPIO_NAREA <dbl>, MPIO_CNMBR <chr>
library(tmap)
library(ggplot2)
library(ggrepel)
library(classInt)
class(Platanos$Cod_Mun)
## [1] "numeric"
class(municipios$MPIO_CCNCT)
## [1] "character"
Platanos$Cod_Mun = as.character(Platanos$Cod_Mun)
class(Platanos$Cod_Mun)
## [1] "character"
munic_platanos = left_join(municipios,Platanos, by = c("MPIO_CCNCT" = "Cod_Mun"))
munic_platanos
## Simple feature collection with 4 features and 7 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -71.07793 ymin: 2.737109 xmax: -67.4098 ymax: 6.324317
## Geodetic CRS:  WGS 84
##   MPIO_CCDGO MPIO_CCNCT MPIO_NAREA     MPIO_CNMBR      Municipio    Grupo
## 1        624      99624   3691.869  SANTA ROSALÍA  Santa Rosalía Plátanos
## 2        001      99001  12204.951 PUERTO CARREÑO Puerto Carreño Plátanos
## 3        524      99524  18569.338   LA PRIMAVERA   La Primavera Plátanos
## 4        773      99773  65597.212       CUMARIBO       Cumaribo Plátanos
##   max_prod                       geometry
## 1      320 POLYGON ((-70.65603 5.37316...
## 2      180 POLYGON ((-67.80972 6.32431...
## 3      657 POLYGON ((-69.0331 6.218449...
## 4    10250 POLYGON ((-68.47074 5.55046...
breaks <- classIntervals(munic_platanos$max_prod, n = 3, 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)
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_CNMBR), 
                    label.padding =     unit(0.05,"lines"),  
                    label.r = unit(0.025, "lines"),
                    label.size = 0.05)

(tuberculos = read_csv("C:/Users/EPI_m/OneDrive/Escritorio/Geomatica/Rstudio/vichada_Tuberculos_2020.csv",show_col_types = FALSE))
## # A tibble: 4 x 4
##   Cod_Mun Municipio      Grupo                 max_prod
##     <dbl> <chr>          <chr>                    <dbl>
## 1   99773 Cumaribo       Tubérculos Y Plátanos     4000
## 2   99524 La Primavera   Tubérculos Y Plátanos     1470
## 3   99624 Santa Rosalía  Tubérculos Y Plátanos      432
## 4   99001 Puerto Carreño Tubérculos Y Plátanos      300
tuberculos$Cod_Mun = as.character(tuberculos$Cod_Mun)
#municipios$MPIO_CCDGO = as.character(municipios$MPIO_CCNCT)
munic_tuberculos = left_join(municipios, tuberculos, by = c("MPIO_CCNCT" = "Cod_Mun"))
munic_tuberculos
## Simple feature collection with 4 features and 7 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -71.07793 ymin: 2.737109 xmax: -67.4098 ymax: 6.324317
## Geodetic CRS:  WGS 84
##   MPIO_CCDGO MPIO_CCNCT MPIO_NAREA     MPIO_CNMBR      Municipio
## 1        624      99624   3691.869  SANTA ROSALÍA  Santa Rosalía
## 2        001      99001  12204.951 PUERTO CARREÑO Puerto Carreño
## 3        524      99524  18569.338   LA PRIMAVERA   La Primavera
## 4        773      99773  65597.212       CUMARIBO       Cumaribo
##                   Grupo max_prod                       geometry
## 1 Tubérculos Y Plátanos      432 POLYGON ((-70.65603 5.37316...
## 2 Tubérculos Y Plátanos      300 POLYGON ((-67.80972 6.32431...
## 3 Tubérculos Y Plátanos     1470 POLYGON ((-69.0331 6.218449...
## 4 Tubérculos Y Plátanos     4000 POLYGON ((-68.47074 5.55046...
facet = "max_prod"
tuberculos_map =  
  tm_shape(munic_tuberculos) + tm_polygons(facet) + tm_text(text = "Municipio", size = 0.7, fontfamily = "sans") +
  tm_shape(Vichada.cities) + tm_symbols(shape = 2, col = "red", size = 0.20) +
  tm_credits("Data source: UPRA (2020)", fontface = "bold") +
  tm_layout(main.title = "Produccion de 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
tuberculos_map
## Credits not supported in view mode.
## Symbol shapes other than circles or icons are not supported in view mode.