#Sobre los datos Este análisis es parte del proyecto #datosdemiércoles, de @r4ds_es. La fuente de datos es el Human Freedom Index: https://www.cato.org/human-freedom-index-new

El Índice de Libertad Humana es calculado en función de distintas variables que miden dos dimensiones: libertad económica y libertad personal. Algunas de las áreas que se miden son las siguientes: -Rule of Law -Security and Safety -Movement -Religion -Association, Assembly, and Civil Society -Expression and Information -Identity and Relationships -Size of Government -Legal System and Property Rights -Access to Sound Money -Freedom to Trade Internationally -Regulation of Credit, Labor, and Business

Abajo del todo se encuentran dos mapas interactivos que observan cómo le fue a la región en las mediciones de 2008 y 2016. Salvo excepciones (como Uruguay, Chile, Costa Rica), en América Latina se ve un empeoramiento del índice, liderado por la preocupante situación de Venezuela, pero visible también en países que antes puntuaban bien (en varios casos, donde los gobiernos giraron a la derecha -véase Brasil, Argentina).

Código para procesar la base de @r4ds_es

library(tidyverse)
## -- Attaching packages -------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.0     v purrr   0.3.2
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   0.8.3     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts ----------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(foreign)
library(gganimate)
library(devtools)
## Loading required package: usethis
#install_github("mtennekes/tmaptools")
#install_github("mtennekes/tmap")
library(tmap)
library(tmaptools)
library(RColorBrewer)

libertad <- readr::read_csv("https://raw.githubusercontent.com/cienciadedatos/datos-de-miercoles/master/datos/2019/2019-08-14/libertad.csv")
## Parsed with column specification:
## cols(
##   anio = col_double(),
##   codigo_iso = col_character(),
##   pais = col_character(),
##   region = col_character(),
##   libertad_humana_puntaje = col_double(),
##   libertad_humana_ranking = col_double(),
##   libertad_personal_puntaje = col_double(),
##   libertad_personal_ranking = col_double(),
##   libertad_economica_puntaje = col_double(),
##   libertad_economica_ranking = col_double()
## )
colnames(libertad)[3] <- "PAIS"
libertad_tidy <- libertad %>% spread(key=anio, value=libertad_humana_puntaje) %>%
  select(PAIS, region, `2008`,`2016`) 

lib_2016 <- aggregate(`2016`~PAIS+region, libertad_tidy, max)
lib_2008 <- aggregate(`2008`~PAIS+region, libertad_tidy, max)

lib_join <- lib_2016 %>% left_join(lib_2008, by="PAIS")

rm(list=setdiff(ls(), "lib_join"))

lib_join <- lib_join %>% select(-region.x) %>%
  mutate(PAIS=ifelse(PAIS=="México", "Méjico", PAIS))

Trabajo con shapefiles

#Uno tabla a shapefile
#shp mapa paises mundo https://www.arcgis.com/home/item.html?id=ad61dcd7dd244d4096c22a49cc97011f

dbf.mapa <- read.dbf("Mapa_paises_mundo.dbf")
dbf.MAPA <- dbf.mapa %>% 
  left_join(lib_join, by= "PAIS") 
## Warning: Column `PAIS` joining factor and character vector, coercing into
## character vector
write.dbf(dbf.MAPA, "Mapa_paises_mundo.dbf")

para_mapa <- st_read("Mapa_paises_mundo.shp", quiet = T)%>%
  gather(X2016,X2008, key="anio", value="indice") %>%
  mutate(anio_ok=ifelse(anio=="X2016", "2016", "2008"))


pm <- para_mapa%>% filter(region_y=="Latinoamérica y el Caribe" | region_y=="Norteamérica")

Mapa interactivo

tmap_mode("view")
## tmap mode set to interactive viewing
m <- tm_shape(pm)  + tm_polygons("indice",
                                   style="pretty",
                                   palette="RdYlGn",
                                   title="Índice de Libertad Humana")
m_facets <- m + tm_facets(by="anio_ok", nrow=1, sync=TRUE)

m_facets