Giter Club home page Giter Club logo

siane's Introduction

Siane

This is an R package to plot maps based on CartoSiane, the official Spanis maps provided by the Instituto Geográfico Nacional (IGN). These maps are compatible with the Spanish Instituto Nacional de Estadística (INE) georreferenced data; this makes it simple to combine geographical and statistical data to produce maps.

This document explains how to install the package, how to download the base maps, and, finally, how to produce a number of simple statistical maps.

Package installation

To install the package, you need to download Siane maps first.

Obtain Siane maps

The package does not include the map layers. These need to be downloaded from the IGN first from this website.

You need to scroll down to the bottom of the page and click in the highlighted button:

Image

You will get into in a new page where you will have to click on "Buscar por polígono"

Image

Once you get here, draw a triangle in the surface of the map. It just has to be a closed polygon. A simple triangle will do it. Then, you need to unlist all the products by clicking on the "+" button.

Image

You will need to download the SIANE_CARTO_BASE_S_3M and SIANE_CARTO_BASE_S_6M5 maps.

Image

You need to unzip the folders in the same directory. Siane should have access to both folders in order to plot the maps. For instance, my folders (SIANE_CARTO_BASE_S_3M and SIANE_CARTO_BASE_S_6M5) are stored at /Users/nunoc/Desktop/siane. You shouldnt' rename neither the files nor the directories.

Install the package

To install, the package, all you need to do is:

library(devtools)
install_github("rOpenSpain/Siane")
library(Siane)

Then you can use it in your session:

library(Siane)

However, the package is not usable until you register the maps, i.e., you indicate where to find them.

Register and test the package

To register the maps, you need to indicate the full path to the directory storing the SIANE_CARTO_BASE_S_3M and SIANE_CARTO_BASE_S_6M5 folders. In my case:

obj <- register_siane("/Users/Nuniemsis/Desktop/siane/")

Then you can select the map,

shp <- siane_map(obj = obj, level = "Municipios", canarias = TRUE, peninsula = "close")

and plot it:

raster::plot(shp)

Map options

This section describes some of the parameters to select the map features.

  • year: Maps change over time and Siane keeps a historic version of them. You can select the year corresponding to the base map. This is particularly useful for municipalities.
  • canarias: It indicates whether we want to plot the Canary islands.
  • level: It is the administrative level. For this set of maps there are three: Municipios, Provincias and Comunidades.
  • scale: The scale of the maps. The default scale for municipalities is 1:3000000 scale = "3m" . For provinces and regions the default scale is 1:6500000 scale = "6m".
  • peninsula: It's the relative position of the Canarias island to the peninsula in case you want it to be shifted closer to it.
shp <- siane_map(obj, level = "Municipios", canarias = TRUE, peninsula = "close")
plot(shp)

Plotting INE data

One of the advantages of Siane maps is that their entities are indexed using official INE codes. This facilitates the creation of maps representing statistical variables. This section illustrates the procedure. In particular, we will plot the population of the municipalities of La Rioja.

Data download

To download the data from the INE, you need to click on the button on the right side of the webpage and choose the Pc-Axis format.

Image

You will need the pxR package to read this file.

library(pxR)
library(RColorBrewer)

ine_path <- "/Users/nunoc/Downloads/2879.px"
df <- as.data.frame(read.px(ine_path))

names(df) # List the column names  of the data frame

Of course, you will have to provide the path where you have downloaded the 2879.px file.

Data preparation

First we need to understand the data frame. It has the following columns:

  • Periodo is the time column in year's format.
  • value is a column with the numeric value of the population.
  • Sexo is the sex of that population.
  • Municipios is a character array with the municipality name and the municipality code

In this dataset there is only one value per territory.

Split the Municipios column to get the municipality's codes. We are storing the codes in the column codes. This column is really important in order to plot the polygons with the correct colours.

We create a single column in the data frame with those codes.

df$codes <- sapply(df$Municipios, function(x) strsplit(as.character(x), split = " ")[[1]][1])
df$Periodo <- as.numeric(as.character(df$Periodo))

Plotting polygons by colour intensity requires one unique value per territory. Therefore, we have to filter the data frame. Keep this Golden rule: One value per territory. In this example I want to plot the total population in the year 2016.

df <- df[df$Sex == "Total" & df$Periodo == 2016, ]

Next, we need to include our population data into the map object, shp

by <- "codes"
value <- "value"
level <- "Municipios"

shp_merged <- siane_merge(shp = shp, df = df, by = by, level = level, value = value)

Then, we can plot the map with the plot function. The RColorBrewer package provides lots of colour scales. We can use this colour scales to visualize data over the map. The following function displays all the colour scales from that package. In case we want to plot the population of certain municipalities in a province, we must use a sequential pallete.

pallete_colour <- "OrRd" # Scale of oranges and reds

Let's say that n is the number of colour intervals.

n <- 5

The brewer.pal function builds a pallete with n intervals and the pallete_colour colour.

values_ine <- shp_merged@data[[value]] # Values we want to plot are stored in the shape@data data frame
colors <- brewer.pal(n, pallete_colour) # A pallete from RColorBrewer

The style is the distribution of colour within a numerical range. The classIntervals function generates numerical intervals. The upper and lower limits of these intervals are named breaks.

style <- "quantile"
brks <- classIntervals(values_ine, n = n, style = style)
my_pallete <- brks$brks # my_pallete is a vector of breaks

Match each number with its corresponding interval to be able to decide its colour.

col <- colors[findInterval(values_ine, my_pallete,
                           all.inside=TRUE)] # Setting the final colors

Plot the map and set title and legends.

raster::plot(shp_merged,col = col) # Plot the map
title_plot <- "Población total por municipios en La Rioja"

title(main = title_plot)
legend(legend = leglabs(round(my_pallete)), fill = colors,x = "bottomright")

The resulting map is:

Image

Other examples

Let's try with a different dataset. This dataset has the spanish population for all the provinces.You can find the link in the README.MD file as well. Now we are going to go deeper in some options.

ine_path <- "/Users/nunoc/Downloads/2852.px"
df <- as.data.frame(read.px(ine_path))
names(df) # List the column names  of the data frame

Data preparation

The same steps as before.

df[[by]] <- sapply(df$Provincias,
                   function(x) strsplit(x = as.character(x), split = " ")[[1]][1])
df$Periodo <- as.numeric(as.character(df$Periodo))

year <- 2016 # year of the maps

df <- df[df$Sex == "Total" & df$Periodo == year,]

The options now will be slightly different:level <- "Provincias". Remember that first we have to create the shapefile. We can't use the previous shapefile provided that the level has changed. These maps are provincial maps.

level <- "Provincias"
canarias <- FALSE
scale <- "6m" # "3m" also accepted

Read the map

shp <- siane_map(obj = obj, canarias = canarias, year = year, level = level, scale = scale)
Map options

The values are in the values column and the codes are in the codes column.

value <- "value"
by <- "codes"

shp_merged <- siane_merge(shp = shp, df = df, by = by, value = value)

Plot the map.

pallete_colour <- "BuPu"
n <- 7
style <- "kmeans"

values_ine <- shp_merged@data[[value]] # Values we want to plot are stored in the shape@data data frame
colors <- brewer.pal(n, pallete_colour) # A pallete from RColorBrewer

brks <- classIntervals(values_ine, n = n, style = style)
my_pallete <- brks$brks

col <- colors[findInterval(values_ine, my_pallete,
                           all.inside=TRUE)] # Setting the final colors

raster::plot(shp_merged,col = col) # Plot the map

title_plot <- "Población de España a nivel de provincias"

title(main = title_plot)
legend(legend = leglabs(round(my_pallete)), fill = colors,x = "bottomright")

This produces the map:

Image

siane's People

Contributors

cjgb avatar nuniemsis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

siane's Issues

Documentation problems

From R CMD check --as-cran:

* checking Rd line widths ... NOTE
Rd file 'siane_map.Rd':
  \examples lines wider than 100 characters:
     shp <- siane_map(obj = obj, level = "Municipios", canarias = FALSE, peninsula = "close") # Loading the municipality's map of Spain

Rd file 'siane_merge.Rd':
  \examples lines wider than 100 characters:
     shp <- siane_map(obj = obj, level = "Municipios", canarias = FALSE) # Loading the municipality's map of Spain
     df <- as.data.frame(read.px("/home/ncarvalho/Downloads/2879.px"))# Get this file in INE's website. Check the README to learn how to do  ... [TRUNCATED]

These lines will be truncated in the PDF manual.

Please, place the comments over the line (and in multiple lines if a comment is longer than 100 characters).

Malformed DESCRIPTION

From R CMD check --as-cran:

* checking CRAN incoming feasibility ... NOTE

The Title field is just the package name: provide a real title.

The Title field should be a short description of the package.

* checking DESCRIPTION meta-information ... NOTE
Malformed Description field: should contain one or more complete sentences. 

Current Description should be in Title, and Description should provide a comprehensive description of the package.

License components which are templates and need '+ file LICENSE':
  MIT

Should be MIT + LICENSE file, and an appropriate LICENSE file should be present.

Author field differs from that derived from Authors@R
  Author:    ‘Carlos J. Gil Bellosta[aut, cre] Nuno Carvalho[aut]’
  Authors@R: ‘Carlos J. Gil Bellosta [aut, cre], Nuno Carvalho [aut]’

Maintainer field differs from that derived from Authors@R
  Maintainer: ‘Nuno Carvalho <[email protected]>’
  Authors@R:  ‘Carlos J. Gil Bellosta <[email protected]>’

If Authors@R is present, then Author and Maintainer are automatically derived from there, so both fields should be removed. Also there's a discrepancy: if you are the maintainer, you should be marked as [aut, cre], not Carlos.

Finally, packages cannot depend on a patch release. Depends: R (>= 3.3.1) should be Depends: R (>= 3.3.0) instead.

Top-level files problems

From R CMD check --as-cran:

* checking top-level files ... NOTE
Non-standard file/directory found at top level:
  ‘Images’

Please, create a .Rbuildignore file and add Images and README.md to it.

Siane - rostemplate project

https://github.com/orgs/rOpenSpain/projects

Dear developers,

I have created a fork including the deploy of your package on your gh-pages branch using the rostemplate (see Slack channel and https://ropenspain.github.io/rostemplate/). See a live preview of your pkgdown site here:
https://dieghernan.github.io/Siane/

An additional GitHub Action check-standard.yaml has been included. That action would check your package (AS CRAN) on several platforms on every push, pull request and every first day of the month. The goal is to provide CI on your package.

Actions on your package:

  • Check locally and rebuild documentation (upgrade Roxygen version)

  • Fix a small typo on docs (extra '#)

  • Vignette: Fix mismatch on %\VignetteIndexEntry()

  • Implementation of github-actions and related .gitgnore and .Rbuildignore

Next steps

You should receive a Pull Request with these modifications.

After your review and in case you consider merging, consider to deploy Github Pages on your settings (gh-pages branch).

Once it is done, I would appreciate if you move this issue to the Deployed tab on the Project panel (see also Projects tab on this website).


LOG

@cjgb, @Nuniemsis

TODO: Open issue and pull request. Fina deploy up to the owners of the repo

Code problems

From R CMD check --as-cran:

* checking package dependencies ... NOTE 
Depends: includes the non-default packages: 
  ‘RColorBrewer’ ‘classInt’ ‘maptools’ ‘plyr’ ‘raster’ ‘rgdal’ ‘sp’  
Adding so many packages to the search path is excessive and importing 
selectively is preferable.
* checking dependencies in R code ... NOTE
Packages in Depends field not imported from:
  ‘RColorBrewer’ ‘classInt’ ‘maptools’ ‘plyr’ ‘raster’ ‘sp’
  These packages need to be imported from (in the NAMESPACE file)
  for when this namespace is loaded but not attached.

These packages should be Imports, not Depends.

* checking R code for possible problems ... NOTE
brks_color: no visible global function definition for ‘classIntervals’
get_dir : <anonymous>: no visible global function definition for ‘tail’
siane_map: no visible global function definition for ‘bind’
siane_map: no visible global function definition for ‘shift’
siane_map: no visible global function definition for ‘as’
siane_map: no visible global function definition for ‘proj4string<-’
siane_merge: no visible global function definition for ‘join’
stop_advices: no visible binding for global variable ‘value’
Undefined global functions or variables:
  as bind classIntervals join proj4string<- shift tail value
Consider adding
  importFrom("methods", "as")
  importFrom("utils", "tail")
to your NAMESPACE file (and ensure that your DESCRIPTION Imports field
contains 'methods').

objeto 'final_dir' no encontrado

Dear Siane creators.

I've followed the steps in your webpage to plot my data on a Spain map (specifically I want to plot over Andalusia region). No problem installing the package, neither with the register_siane function, but whe I try to run the siane_map the following error appears. Any clue to resolve this problem will be welcome!

library(Siane)
obj <- register_siane("/home/angel/gitrepos/covid19/datasets/siane/")
shp <- siane_map(obj = obj, level = "Municipios", canarias = FALSE)

Using default year as the latest year 
Error in get_dir(base_path, dirs_path, last_path, year) : 
  objeto 'final_dir' no encontrado

Error with readOGr

Follow your example I get an error

shp <- get_siane_map(obj = obj, level = level, year = year, canarias = canarias)
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
  Cannot open file

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.