Sotkanet API R tools

Leo Lahti

2022-02-01

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 Information about Sotkanet and API description. This package is part of rOpenGov.

Installation

Release version from CRAN:

install.packages("sotkanet")

Development version from GitHub:

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

Usage

Listing available indicators

Load sotkanet and other libraries used in this vignette

List available Sotkanet indicators:

# Using a pre-defined list of indicators for a smaller download
sotkanet.indicators <- SotkanetIndicators(id = c(4,5,6,7,46,74), type = "table")
kable(head(sotkanet.indicators))

List geographical regions with available indicators:

# options(width = 60)
sotkanet.regions <- SotkanetRegions(type = "table")
kable(head(sotkanet.regions))

Querying SOTKAnet indicators

Get the indicator no. 10013 (Unemployment rate of young people aged 16-24 years) from Sotkanet, 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)) %>% 
  kable_styling() %>%
  kableExtra::scroll_box(width = "100%")

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 + labs(x = "Year", y = "Value", caption = "Data source: https://sotkanet.fi")
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)) +
       geom_point(size = 3)
       ggtitle("Migration vs. population size") 
       theme(title = element_text(size = 15))
       theme(axis.title.x = element_text(size = 20))
       theme(axis.title.y = element_text(size = 20))
       theme(legend.title = element_text(size = 15))
print(p)

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:
problematic.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, problematic.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)

Further examples

For further usage examples, see rOpenGov-blog, and takomo, and Helsinki Region Infoshare-blog

Licensing and Citations

SOTKAnet data

Cite SOTKAnet and link to https://sotkanet.fi/sotkanet/fi/index. 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:
#> 
#>   Leo Lahti, Einari Happonen, Juuso Parkkinen, Joona Lehtomaki, Vesa
#>   Saaristo and Pyry Kantanen (rOpenGov 2022). sotkanet: Sotkanet Open
#>   Data Access and Analysis. R package version 0.9.79
#>   https://github.com/rOpenGov/sotkanet
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Misc{,
#>     title = {sotkanet: Sotkanet Open Data Access and Analysis},
#>     author = {Leo Lahti and Einari Happonen and Joona Lehtomäki and Juuso Parkkinen and Joona Lehtomaki and Vesa Saaristo and Pyry Kantanen},
#>     url = {https://github.com/rOpenGov/sotkanet},
#>     year = {2022},
#>     note = {R package version 0.9.79},
#>   }
#> 
#> Many thanks for all contributors!

Session info

This vignette was created with

sessionInfo()