Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/robsalasco/sinimr

Chilean Municipalities Information System (SINIM) Wrapper 📈🏛🇨🇱
https://github.com/robsalasco/sinimr

accountability chile municipalidades sinim

Last synced: 3 months ago
JSON representation

Chilean Municipalities Information System (SINIM) Wrapper 📈🏛🇨🇱

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/"
)
```

[![saythanks](https://img.shields.io/badge/say-thanks-ff69b4.svg)](https://saythanks.io/to/robsalasco)
[![Donate](https://img.shields.io/badge/donate-paypal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=WDDLRUVD344XL&currency_code=USD&source=url)
[![Rbuildstatus](https://github.com/robsalasco/sinimr/workflows/R-CMD-check/badge.svg)](https://github.com/robsalasco/sinimr/actions)
[![CoverageStatus](https://img.shields.io/codecov/c/github/robsalasco/sinimr/master.svg)](https://codecov.io/github/robsalasco/sinimr?branch=master)

# sinimR

Chilean Municipalities Information System Wrapper

### What can I do with this?

This R package allows easy SINIM (http://sinim.gov.cl) data retrieval what have advantages over the site:

- When you work with multiple variables or years it will be very useful for rapid analyses.
- Fast ploting directly from data source using the included geometries.
- Data download with or without monetary correction using a switch.

```{r, message=F, fig.height=6, fig.width=6, fig.retina=2}
library(dplyr)
library(sinimr)
library(sf)
library(tmap)

varcode <- 882
var <- get_sinim(varcode, 2018,
region = 13,
truevalue = T,
geometry = T,
auc = T,
unit = "limites")

gran_santiago_plot <- tm_shape(var) +
tm_fill(col = "value",
palette = "BuPu",
border.col = "white",
border.alpha = 0.5,
lwd=1,
style = "jenks",
title = get_sinim_var_name(varcode))+
tm_text("municipality", size = 0.4, style="jenks") +
tm_legend(legend.position = c("left", "top"), legend.title.size = 1, legend.text.size = 0.6) +
tm_compass(type = "8star", position = c(.85, .80)) +
tm_scale_bar(breaks = c(0, 10), text.size = 0.75, position = c("right", "bottom")) +
tm_credits("Fuente: Sistema Nacional de Información Municipal (SINIM), SUBDERE, Ministerio del Interior.", position=c("left", "bottom"), size=0.55)+
tm_layout(legend.width=1,
inner.margins = c(0.1, 0.1, 0.10, 0.1),
legend.format = list(text.separator = "a",
fun = mm)) +
tm_borders(col = 'black')

gran_santiago_plot

```

### Support

FONDECYT Regular 2016 Nº 1161417, ¿Quién es responsable del desarrollo local? Una geografía política del neoestructuralismo en "comunas de exportación" (Comisión Nacional de Investigación Científica y Tecnológica).

### A note on usage

When querying the API, please be respectful of the resources required to provide this data. Please retain the results for each request to avoid repeated requests for duplicate information.

### Installation

```R
install.packages("devtools")
devtools::install_github("robsalasco/sinimr")
```

### How do I use it?

sinimR comes with a small set of functions to deliver the content of SINIM's webpage. To get a first glance of the categories of information what are available please use the ```get_sinim_cats()``` command.

```{r}
library(sinimr)
get_sinim_cats()
```

Every category have a bunch of variables associated. Use the CODE number and the ```get_sinim_vars()``` function to get them.

```{r}
get_sinim_vars(517)
```

Finally, to obtain the data across municipalities use the code column and specify a year.

```{r}
head(get_sinim(c(4210, 4211), 2015))
```

By default the values are in **miles de millones** but it can be disabled using the ```truevalue = T``` switch.

```{r}
head(get_sinim(c(4210, 4211), 2015, truevalue = T))
```

You can get multiple years too! use the command ```get_sinim()``` and add more years as in the example.

```{r}
head(get_sinim(880, 2015:2017))
```

The geometries are available in long format using the ```geometry=T``` argument. By default it uses the **comunal** geographies but the **limite urbano censal** is also available. The switches are ```unit="comunas"``` and ```unit="limites"```. Note: Using **limites** not all features are available because some comunas are not related to urban zones. As shown in the example below you can obtain multiple years and variables in long format.

```{r}
head(get_sinim(882, 2015:2017, geometry=T))
```

Another interesting feature is the possibility to subset by different contexts. e.g if you want the comunas of Antofagasta region this command is available. The command works with or without the presence of the geometry switch and other switches are avaiblable too ```region```, ```provincia``` and ```comuna``` all working with codes.

```{r}
head(get_sinim(882, 2015:2017, geometry=T, region=2))
```

You can get a subset too

```{r}
head(get_sinim(882, 2015:2017, geometry=T, region=c(2,3)))
```

But where obtain the codes? a database is provided and you can filter it using the standard R functions.

```{r}
head(id_geo_census)
```

Related to variables if you don't know what are you looking for use ```search_sinim_vars()```to get search results based on variable descriptions, names and groups.

```{r}
search_sinim_vars("cementerio")
```

## Advanced usage

SINIM (Sistema Nacional de Información Municipal) by default applies a monetary correction to show current values of variables. The original values provided by municipalities are available using the ```moncorr = F``` switch. And if you want geographical identifiers like region or provincia you can apply them using ```idgeo = T``` switch.

### Other example plots

#### Multiple variable faceted plot

```{r, message=F, fig.height=8, fig.width=15, fig.retina=2}
library(tmap)
library(dplyr)
library(stringr)
library(sinimr)
library(sf)

data_sinim <- get_sinim(var = c(3954,4174,880,1226,4251,4173),
year = 2018,
region = 13,
geometry = T,
truevalue = T,
auc = T,
unit = "limites")

gran_santiago_plot <- tm_shape(data_sinim) +
tm_fill(col = "value",
palette = "BuPu",
border.col = "white",
border.alpha = 0.5,
lwd=1,
style = "jenks",
title = "variable")+
tm_text("municipality", size = 0.4) +
tm_style("white", frame = T, legend.title.size = 1, legend.width=1) +
tm_layout(inner.margins = c(0.01, 0.1, 0.1, 0.01),
outer.margins = c(0.01, 0.01, 0.01, 0.01),
design.mode=F,
legend.format = list(text.separator = "a",
fun = mm))+
tm_borders(col = 'black') +
tm_facets(by="variable", ncol = 2)

gran_santiago_plot

```

#### A variable in multiple years using facets

```{r, message=F, fig.height=10, fig.width=18, fig.retina=2}
library(dplyr)
library(sinimr)
library(sf)
library(tmap)

var <- get_sinim(c(880, 882, 1226),
2016:2018,
region = 13,
truevalue = T,
geometry = T,
auc = T,
unit = "limites")

gran_santiago_plot <- tm_shape(var) +
tm_fill("value",
palette="BuPu",
border.col = "white",
style = "jenks",
border.alpha = 0.5,
lwd=1) +
tm_text("municipality", size = 0.4) +
tm_legend(legend.position = c("left", "top")) +
tm_layout(legend.width=0.09,
inner.margins = c(0.01, 0.1, 0.1, 0.01),
outer.margins = c(0.01, 0.01, 0.01, 0.01),
legend.format = list(text.separator = "a",
fun = mm)) +
tm_facets(by=c("year","variable"),) +
tm_borders(col = 'black')

gran_santiago_plot

```

#### Multiple variables and years using geofacet

```{r, message=F, fig.height=12, fig.width=24, fig.retina=2}
library(sf)
library(dplyr)
library(geofacet)
library(sinimr)
library(ggplot2)
library(zoo)
library(scales)
library(ggpubr)

data <- get_sinim(882, 2002:2018,
region = 13,
moncorr = F,
truevalue = T,
auc = T)

data$year <- as.numeric(as.character(data$year))
data$year <- as.Date(as.yearmon(data$year, "1-%y"))

reg13 <- geogrid::read_polygons("https://raw.githubusercontent.com/robsalasco/precenso_2016_geojson_chile/master/Extras/GRAN_SANTIAGO.geojson")
grd <- grid_auto(reg13, seed = 1, names = "NOM_COMUNA", codes = "COMUNA")

#grid_preview(grd, label = "name_NOM_COMUNA")
#grid_design(grd, label = "name_NOM_COMUNA")

ggplot(data, aes(year, value, group=1)) +
geom_line(color = "steelblue") +
facet_geo(~ municipality, grid = grd, scales = "free_y")+
scale_x_date() +
scale_y_continuous(labels = dollar_format(suffix = "", prefix = "$", big.mark = ".", decimal.mark=","))+
theme_bw()
```

### Citation

```{r}
citation("sinimr")
```

### References

- Sistema Nacional de Información Municipal (SINIM), SUBDERE, Ministerio del Interior.