communes_CH_indicators: Swiss communes socio-demographic indicators

loadCommunesCHportraitsR Documentation

Swiss communes socio-demographic indicators

Description

Load socio-demographic data by the Swiss Statistical Office.

Useful to update the source data of commune socio-economic indicators loaded by loadCommunesCHportraits Warning of possible break if the OFS changes the data structure of the xls file!!

Usage

loadCommunesCHportraits()

processPortraitsRegionauxCommune(
  file = "je-f-21.03.01.xlsx",
  output = "communesCH_2021_indicators_je-f-21.03.01.csv"
)

Arguments

file

the raw excel file name from the Swiss Statistical office with indicators by commune

output

the output file name to be saved in the package inst/extdata

Details

Run processPortraitsRegionauxCommune() to generate a readable csv file for loadCommunesCHportraits

The matrix has the commune BFS code as rowname and 3 attributes:

Value

a matrix

See Also

Office fédéral de la statistique > Les Régions > Communes > Données et explications (portraits): https://www.bfs.admin.ch/bfs/fr/home/statistiques/statistique-regions/portraits-regionaux-chiffres-cles/communes.assetdetail.328133.html
The cleaning up of raw xls file:

  • Remove all the empty lines (header has 3 lines: Indicator type, indicator, year)

  • Reorder the header for: indicator type, year, indicator

  • Remove in the data table, the line for Switzerland

  • Remove the text at the bottom

  • Save as csv

http://opendata.admin.ch/de/dataset/ch-swisstopo-vd-ortschaftenverzeichnis_plz/resource/35001b61-e7c1-4124-89fa-17fac7b1139e from: http://opendata.admin.ch/de/dataset/ch-swisstopo-vd-ortschaftenverzeichnis_plz

Examples

communeData <- loadCommunesCHportraits()
colnames(communeData)
head(rownames(communeData))
attr(communeData, "indicatorYear")
attr(communeData, "indicatorGroup")

# Select only "surface" indicators
colIdx <- which(attr(communeData, "indicatorGroup") == "Surface")
head(communeData[,colIdx])

zipcode <- loadCHzipcode()
# get the zipcode of each commune
zp <- zipcode[match( attr(communeData, "communeName"), zipcode$Gemeindename),]

## Not run: 
library(tidyverse)

## 1. Show the %foreigners vs % vote UDC
  g1 <- ggplot(data = as.data.frame(communeData), aes(x = `Etrangers en %`, y = UDC)) + 
  geom_point(aes(size = Habitants), alpha = 0.5)
  g1

## 2. Barcode chart: Densité de la population par km²" 
## ------------- TO DO add some text what is highlighted,  better colour use, ..
communesList <- communes_list() %>% 
  select(-`Numéro d'historisation`, -`Numéro du district`, -`Date de l'inscription`)

dd <- communeData %>% as.data.frame() %>% 
  rownames_to_column(var = "Numéro de la commune") %>% 
  select(`Numéro de la commune`, `Densité de la population par km²`, 
  `Surface agricole en %`, `PS`) %>% 
  mutate(`Numéro de la commune` = as.integer(`Numéro de la commune`))
dd <- left_join(dd, communesList) %>% 
  gather(feature, value, -`Numéro de la commune`, 
  -Canton, -`Nom du district`, -`Nom de la commune`)
## colour group
## 6617 Cologny < GE < remaining
ddd <- dd %>% mutate(
  cgroup = as.factor(ifelse(`Numéro de la commune` == 6617, 1,
    ifelse(Canton == "GE", 2, 3))),
  alpha = case_when(cgroup == 1 ~ 1, cgroup == 2 ~ 0.7, cgroup == 3 ~ 0.1),
  size  = case_when(cgroup == 1 ~ 1, cgroup == 2 ~ 0.5, cgroup == 3 ~ 0.3),
  )

barcode <- ggplot(ddd) +
  geom_segment(aes(x = value, xend = value, colour = cgroup, alpha = alpha, size = size), 
  y = -0.5, yend = 0.5) +
  scale_y_continuous(expand = c(0,0), limits=c(-0.5, 0.5), labels = NULL) +
  theme_minimal() +
  facet_wrap(~feature, ncol = 1, scales = "free_x") +
  scale_alpha_identity() +
  scale_size_identity() +
  scale_colour_manual(values = c('#003333', '#008080', '#00e6e6'))

barcode + theme(strip.text = element_text(hjust = 0), panel.grid = element_blank())

## End(Not run)
## Not run: 
processPortraitsRegionauxCommune()

## End(Not run)

d-qn/tamMap documentation built on Oct. 20, 2023, 7:41 a.m.