library(cartogram)
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(readxl)
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(ggplot2)
library(tmap)
library(tidyr)
setwd("C:/Users/User/Desktop/MAPA")
mundo <- st_read("countries.geo.json")
## Reading layer `countries.geo' from data source
## `C:\Users\User\Desktop\MAPA\countries.geo.json' using driver `GeoJSON'
## Simple feature collection with 180 features and 2 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -180 ymin: -85.60904 xmax: 180 ymax: 83.64513
## Geodetic CRS: WGS 84
library(htmltab)
IDH = "https://en.wikipedia.org/wiki/List_of_countries_by_Human_Development_Index"
seccion2='//*[@id="mw-content-text"]/div[1]/table[2]'
IDHx = htmltab(doc =IDH,
which = seccion2 ,encoding = "UTF-8")
#Limpiamos data IDHx
IDHx = IDHx[,c(3,4)]
names(IDHx) = c("Country" , "Idh2022") #Renombramos las dimensiones
IDHx$Idh2022 = as.numeric(IDHx$Idh2022) #De character a numerico
IDHx[24,1] = "United States of America"
IDHx$Country=trimws(IDHx$Country,whitespace = "[\\h\\v]")
plot(mundo$geometry)
mundo3857 <- st_transform(mundo, 3857)
plot(mundo3857$geometry, main = "Mapa del mundo (EPSG:3857")
#Eliminamos Antartida
mundo3857 <- mundo3857 %>%
filter(!name %in% 'Antarctica')
#Merge
mundo_idh <- left_join(mundo3857, IDHx, by=c('name'='Country'))
mundo_idh_noNA <- mundo_idh %>% replace_na(list(Idh2022 = 0.1))
#Cartogram
idh_cartogram <- cartogram_cont(mundo_idh_noNA, "Idh2022", itermax = 8)
## Mean size error for iteration 1: 17.7744226134362
## Mean size error for iteration 2: 11.6484692229543
## Mean size error for iteration 3: 7.60904748480816
## Mean size error for iteration 4: 5.15809396622234
## Mean size error for iteration 5: 3.68378024463233
## Mean size error for iteration 6: 2.80599143297307
## Mean size error for iteration 7: 2.3319845515039
## Mean size error for iteration 8: 2.05757710100953
dtemp<-replace(idh_cartogram$Idh2022, idh_cartogram$Idh2022 == 0.1, 0)
mapa_final <- idh_cartogram %>% mutate(Idh2022 = dtemp)
tm_shape(mapa_final) +
tm_style("gray") +
tm_layout(main.title = "Índice de Desarrollo Humano 2022",
main.title.size = 1,
title = "Fuente: PNUD (2022)", title.size = 0.7)+
tm_polygons(col = "Idh2022", style = "cont",
breaks= c(0,0.25, 0.5, 0.75,1), palette = "Purples",
title = "IDH") +
tm_scale_bar(position = c("left", "bottom"), width = 0.1) +
tm_compass(position = c("0.001", "0.09"), size = 1.5)+
tm_credits("Grupo 2 - Teorías del Desarrollo, 2022")