Sotkanet API R tools

This is the sotkanet R package to access data from the Sotkanet portal that provides over 2000 demographic indicators across Finland and Europe, maintained by the National Institute for Health and Welfare (THL). For more information, see Sotkanet indicator database and API description. This package is part of rOpenGov.

Installation

Release version:

install.packages("sotkanet")
library(sotkanet)

Development version (for the adventurous ones):

library(devtools)
install_github("ropengov/sotkanet")

Listing available indicators

List available Sotkanet indicators and print as a table:

library(sotkanet) 
sotkanet.indicators <- SotkanetIndicators(type = "table")

library(knitr)
kable(head(sotkanet.indicators))

List geographical regions with available indicators:

sotkanet.regions <- SotkanetRegions(type = "table")
kable(head(sotkanet.regions))

Querying SOTKAnet indicators

Get the indicator 10013 from Finland (Suomi) for 1990-2012 (Eurostat employment statistics youth unemployment), and plot a graph:

# Get indicator data
dat <- GetDataSotkanet(indicators = 10013, years = 1990:2012, 
                   genders = c('female', 'male', 'total'), 
               region.category = "EUROOPPA", regions = "Suomi")

# Investigate the first lines in the data
kable(head(dat))

Fetch all SOTKAnet indicators

This takes for a long time and is not recommended for regular use. Save the data on your local disk for further work.

# These indicators have problems with R routines:
probematic.indicators <- c(1575, 1743, 1826, 1861, 1882, 1924, 1952, 2000, 2001, 2033, 2050, 3386, 3443)

# Get data for all indicators
datlist <- list()
for (ind in setdiff(sotkanet.indicators$indicator, probematic.indicators)) {
  datlist[[as.character(ind)]] <- GetDataSotkanet(indicators = ind, 
        years = 1990:2013, genders = c('female', 'male', 'total'))
}

# Combine tables (this may require considerable time and memory 
# for the full data set)
dat <- do.call("rbind", datlist)

Visualization

Download and visualize time series:

# Pick indicator name
indicator.name <- as.character(unique(dat$indicator.title.fi))
indicator.source <- as.character(unique(dat$indicator.organization.title.fi))

# Visualize
library(ggplot2)
theme_set(theme_bw(20)); 
p <- ggplot(dat, aes(x = year, y = primary.value, group = gender, color = gender)) 
p <- p + geom_line() + ggtitle(paste(indicator.name, indicator.source, sep = " / ")) 
p <- p + xlab("Year") + ylab("Value") 
p <- p + theme(title = element_text(size = 10))
p <- p + theme(axis.title.x = element_text(size = 20))
p <- p + theme(axis.title.y = element_text(size = 20))
p <- p + theme(legend.title = element_text(size = 15))
print(p)

Investigate the effect of municipality size on demographic variation. Smaller municipalities show more random variation as expected by statistical arguments:

selected.inds <- c(127, 178)
dat <- GetDataSotkanet(indicators = selected.inds, 
                years = 2011, genders = c('total'))
# Pick necessary fields and remove duplicates
datf <- dat[, c("region.title.fi", "indicator.title.fi", "primary.value")]
datf <- datf[!duplicated(datf),]
dw <- reshape(datf, idvar = "region.title.fi", 
                timevar = "indicator.title.fi", direction = "wide")
names(dw) <- c("Municipality", "Population", "Migration")
p <- ggplot(dw, aes(x = log10(Population), y = Migration)) 
p <- p + geom_point(size = 3)
p <- p + ggtitle("Migration vs. population size") 
p <- p + theme(title = element_text(size = 15))
p <- p + theme(axis.title.x = element_text(size = 20))
p <- p + theme(axis.title.y = element_text(size = 20))
p <- p + theme(legend.title = element_text(size = 15))
print(p)

Further examples

For further usage examples, see Louhos-blog, and takomo. In particular the following blog posts are using the package:

Licensing and Citations

SOTKAnet data

Cite SOTKAnet and link to http://www.sotkanet.fi. Also mention indicator provider.

Central points:

SOTKAnet R package

This work can be freely used, modified and distributed under the Two-clause BSD license.

citation("sotkanet")
## 
## Kindly cite the sotkanet R package as follows:
## 
##   (C) Leo Lahti, Einari Happonen, Juuso Parkkinen and Joona
##   Lehtomaki 2013-2015. sotkanet: Sotkanet R Tools
## 
## A BibTeX entry for LaTeX users is
## 
##   @Misc{,
##     title = {sotkanet R package},
##     author = {Leo Lahti and Einari Happonen and Juuso Parkkinen and Joona Lehtomaki},
##     year = {2013},
##   }
## 
## For more info, see: https://github.com/ropengov/sotkanet/

Session info

This vignette was created with

sessionInfo()
## R version 3.2.1 (2015-06-18)
## Platform: x86_64-unknown-linux-gnu (64-bit)
## Running under: Ubuntu 15.04
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
## [1] magrittr_1.5  formatR_1.2   tools_3.2.1   stringi_0.5-2 knitr_1.10.5 
## [6] stringr_1.0.0 evaluate_0.7