list.files("C:/Users/Lenovo/Documents/CuadernosR", pattern=c('csv'))
[1] "co.csv"
[2] "Evaluaciones_Agropecuarias_Municipales_EVA.csv"
[3] "narino_platanos_2020.csv"
[4] "narino_tuberculosyplatanos_2020.csv"
list.files('C:/Users/Lenovo/Documents/CuadernosR/')
[1] "co.csv"
[2] "drive-download-20220409T154620Z-001.zip"
[3] "drive-download-20220409T162205Z-001.zip"
[4] "Evaluaciones_Agropecuarias_Municipales_EVA.csv"
[5] "MunicipiosNarino.cpg"
[6] "MunicipiosNarino.dbf"
[7] "MunicipiosNarino.prj"
[8] "MunicipiosNarino.qpj"
[9] "MunicipiosNarino.shp"
[10] "MunicipiosNarino.shp.BE500P01SG07AGR.13708.5600.sr.lock"
[11] "MunicipiosNarino.shx"
[12] "narino_platanos_2020.csv"
[13] "narino_tuberculosyplatanos_2020.csv"
(platanos = read.csv("C:/Users/Lenovo/Documents/CuadernosR/narino_platanos_2020.csv"))
munnar <- st_read("C:/Users/Lenovo/Documents/CuadernosR/MunicipiosNarino.shp")
Reading layer `MunicipiosNarino' from data source
`C:\Users\Lenovo\Documents\CuadernosR\MunicipiosNarino.shp'
using driver `ESRI Shapefile'
Simple feature collection with 66 features and 11 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -79.01021 ymin: 0.3613481 xmax: -76.83368 ymax: 2.683898
Geodetic CRS: WGS 84
(mun.tmp = st_read('C:/Users/Lenovo/Documents/CuadernosR/MunicipiosNarino.shp'))
Reading layer `MunicipiosNarino' from data source
`C:\Users\Lenovo\Documents\CuadernosR\MunicipiosNarino.shp'
using driver `ESRI Shapefile'
Simple feature collection with 66 features and 11 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -79.01021 ymin: 0.3613481 xmax: -76.83368 ymax: 2.683898
Geodetic CRS: WGS 84
Simple feature collection with 66 features and 11 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -79.01021 ymin: 0.3613481 xmax: -76.83368 ymax: 2.683898
Geodetic CRS: WGS 84
First 10 features:
DPTO_CCDGO MPIO_CCDGO MPIO_CNMBR
1 52 083 BELÉN
2 52 110 BUESACO
3 52 203 COLÓN
4 52 480 NARIÑO
5 52 506 OSPINA
6 52 720 SAPUYES
7 52 786 TAMINANGO
8 52 788 TANGUA
9 52 240 CHACHAGÜÍ
10 52 254 EL PEÑOL
MPIO_CRSLC
1 Ordenanza 53 Noviembre 29 de 1985
2 1899
3 Ordenanza 37 de 1921
4 Ordenanza 027 de Noviembre. 29 de 1999. Decreto 0312 del 24
5 Ordenanza 50 de 1865
6 1849
7 1834
8 Ordenanza 103 de 1874
9 Ordenanza 20 Noviembre 24 de 1992
10 Ordenanza 036 de Diciembre 7 de 1998
MPIO_NAREA MPIO_CCNCT MPIO_NANO DPTO_CNMBR SHAPE_AREA
1 41.84541 52083 2020 NARIÑO 0.003391678
2 635.96083 52110 2020 NARIÑO 0.051533090
3 61.75053 52203 2020 NARIÑO 0.005005108
4 25.31281 52480 2020 NARIÑO 0.002050175
5 64.84321 52506 2020 NARIÑO 0.005249269
6 115.54851 52720 2020 NARIÑO 0.009351438
7 234.65783 52786 2020 NARIÑO 0.019009395
8 217.95977 52788 2020 NARIÑO 0.017652117
9 146.27176 52240 2020 NARIÑO 0.011849554
10 119.85744 52254 2020 NARIÑO 0.009707107
SHAPE_LEN ORIG_FID geometry
1 0.3732840 0 POLYGON ((-77.07227 1.63422...
2 1.2292312 1 POLYGON ((-77.23516 1.45240...
3 0.4592866 2 POLYGON ((-77.04473 1.67173...
4 0.2642048 3 POLYGON ((-77.34282 1.31465...
5 0.3371496 4 POLYGON ((-77.55776 1.07006...
6 0.6599792 5 POLYGON ((-77.71499 1.0915,...
7 0.6601636 6 POLYGON ((-77.32644 1.67981...
8 0.7808421 7 POLYGON ((-77.36152 1.19568...
9 0.7373282 8 POLYGON ((-77.30295 1.51777...
10 0.5550189 9 POLYGON ((-77.39239 1.60127...
mun.tmp %>% select(MPIO_CCNCT, MPIO_CNMBR, MPIO_NAREA) -> municipios
municipios
Simple feature collection with 66 features and 3 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -79.01021 ymin: 0.3613481 xmax: -76.83368 ymax: 2.683898
Geodetic CRS: WGS 84
First 10 features:
MPIO_CCNCT MPIO_CNMBR MPIO_NAREA
1 52083 BELÉN 41.84541
2 52110 BUESACO 635.96083
3 52203 COLÓN 61.75053
4 52480 NARIÑO 25.31281
5 52506 OSPINA 64.84321
6 52720 SAPUYES 115.54851
7 52786 TAMINANGO 234.65783
8 52788 TANGUA 217.95977
9 52240 CHACHAGÜÍ 146.27176
10 52254 EL PEÑOL 119.85744
geometry
1 POLYGON ((-77.07227 1.63422...
2 POLYGON ((-77.23516 1.45240...
3 POLYGON ((-77.04473 1.67173...
4 POLYGON ((-77.34282 1.31465...
5 POLYGON ((-77.55776 1.07006...
6 POLYGON ((-77.71499 1.0915,...
7 POLYGON ((-77.32644 1.67981...
8 POLYGON ((-77.36152 1.19568...
9 POLYGON ((-77.30295 1.51777...
10 POLYGON ((-77.39239 1.60127...
(cities = read_csv("C:/Users/Lenovo/Documents/CuadernosR/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
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
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 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
narino.cities = dplyr::filter(sf.cities.joined, admin_name=='Nariño')
narino.cities
Simple feature collection with 64 features and 10 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -78.7647 ymin: 0.8081 xmax: -76.9706 ymax: 2.5078
Geodetic CRS: WGS 84
class(platanos$Cod_Mun)
[1] "integer"
class(municipios$MPIO_CCNCT)
[1] "character"
platanos$Cod_Mun = as.character(platanos$Cod_Mun)
munic_platanos = left_join(municipios, platanos, by = c("MPIO_CCNCT" = "Cod_Mun"))
munic_platanos
Simple feature collection with 66 features and 7 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -79.01021 ymin: 0.3613481 xmax: -76.83368 ymax: 2.683898
Geodetic CRS: WGS 84
First 10 features:
MPIO_CCNCT MPIO_CNMBR MPIO_NAREA X Municipio Grupo
1 52083 BELÉN 41.84541 NA <NA> <NA>
2 52110 BUESACO 635.96083 NA <NA> <NA>
3 52203 COLÓN 61.75053 5 Colón (Génova Plátanos
4 52480 NARIÑO 25.31281 NA <NA> <NA>
5 52506 OSPINA 64.84321 NA <NA> <NA>
6 52720 SAPUYES 115.54851 NA <NA> <NA>
7 52786 TAMINANGO 234.65783 11 Taminango Plátanos
8 52788 TANGUA 217.95977 NA <NA> <NA>
9 52240 CHACHAGÜÍ 146.27176 23 Chachaguí Plátanos
10 52254 EL PEÑOL 119.85744 NA <NA> <NA>
max_prod geometry
1 NA POLYGON ((-77.07227 1.63422...
2 NA POLYGON ((-77.23516 1.45240...
3 6000 POLYGON ((-77.04473 1.67173...
4 NA POLYGON ((-77.34282 1.31465...
5 NA POLYGON ((-77.55776 1.07006...
6 NA POLYGON ((-77.71499 1.0915,...
7 2655 POLYGON ((-77.32644 1.67981...
8 NA POLYGON ((-77.36152 1.19568...
9 450 POLYGON ((-77.30295 1.51777...
10 NA POLYGON ((-77.39239 1.60127...
breaks <- classIntervals(munic_platanos$max_prod, n = 6, style = 'fisher')
Warning in classIntervals(munic_platanos$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_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)
Warning: ggrepel: 48 unlabeled data points (too many overlaps). Consider increasing max.overlaps
(tuberculosyplatanos = read.csv("C:/Users/Lenovo/Documents/CuadernosR/narino_tuberculosyplatanos_2020.csv"))
tuberculosyplatanos$Cod_Mun = as.character(tuberculosyplatanos$Cod_Mun)
munic_tuberculosyplatanos = left_join(municipios, tuberculosyplatanos, by = c("MPIO_CCNCT" = "Cod_Mun"))
munic_tuberculosyplatanos
Simple feature collection with 66 features and 7 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -79.01021 ymin: 0.3613481 xmax: -76.83368 ymax: 2.683898
Geodetic CRS: WGS 84
First 10 features:
MPIO_CCNCT MPIO_CNMBR MPIO_NAREA X Municipio
1 52083 BELÉN 41.84541 58 Belén
2 52110 BUESACO 635.96083 29 Buesaco
3 52203 COLÓN 61.75053 44 Colón (Génova
4 52480 NARIÑO 25.31281 39 Nariño
5 52506 OSPINA 64.84321 5 Ospina
6 52720 SAPUYES 115.54851 7 Sapuyes
7 52786 TAMINANGO 234.65783 42 Taminango
8 52788 TANGUA 217.95977 14 Tangua
9 52240 CHACHAGÜÍ 146.27176 55 Chachaguí
10 52254 EL PEÑOL 119.85744 37 El Peñol
Grupo max_prod geometry
1 Tubérculos Y Plátanos 33.0 POLYGON ((-77.07227 1.63422...
2 Tubérculos Y Plátanos 544.0 POLYGON ((-77.23516 1.45240...
3 Tubérculos Y Plátanos 160.0 POLYGON ((-77.04473 1.67173...
4 Tubérculos Y Plátanos 208.0 POLYGON ((-77.34282 1.31465...
5 Tubérculos Y Plátanos 41137.5 POLYGON ((-77.55776 1.07006...
6 Tubérculos Y Plátanos 16270.0 POLYGON ((-77.71499 1.0915,...
7 Tubérculos Y Plátanos 180.0 POLYGON ((-77.32644 1.67981...
8 Tubérculos Y Plátanos 7200.0 POLYGON ((-77.36152 1.19568...
9 Tubérculos Y Plátanos 40.0 POLYGON ((-77.30295 1.51777...
10 Tubérculos Y Plátanos 218.0 POLYGON ((-77.39239 1.60127...
facet = "max_prod"
tuberculosyplatanos_map =
tm_shape(munic_tuberculosyplatanos) + tm_polygons(facet) + tm_text(text = "MPIO_CNMBR", size = 0.7, fontfamily = "sans") +
tm_shape(narino.cities) + tm_symbols(shape = 2, col = "red", size = 0.20) +
tm_credits("Data source: BaseSIPRA2020", fontface = "bold") +
tm_layout(main.title = "Produccion de tuberculos y platanos 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
tuberculosyplatanos_map
Credits not supported in view mode.
Symbol shapes other than circles or icons are not supported in view mode.
#Lizarazo, I., 2022. Getting started with thematic maps. Available at https://rpubs.com/ials2un/thematic_maps_v2.