loadCommunesCHFederalBallot | R Documentation |
Load any federal ballots resuts by communes: https://www.pxweb.bfs.admin.ch/DownloadFile.aspx?file=px-x-1703030000_101.
Run processFederalBallotByCommunes()
to generate a readable csv file for loadCommunesCHFederalBallot
This will download the px file from https://www.bfs.admin.ch/bfs/en/home/statistics/politics/popular-votes.assetdetail.1363949.html,
process it and save it as a Rdata file. Useful to update the ballot results loaded by loadCommunesCHFederalBallot
Warning of possible break if the OFS changes the data structure of the px file!!
loadCommunesCHFederalBallot(file = "federalBallot_communes.RData")
processFederalBallotByCommunes(
url = "https://www.pxweb.bfs.admin.ch/DownloadFile.aspx?file=px-x-1703030000_101",
output = "federalBallot_communes.RData"
)
file |
the name of the csv file processed by processFederalBallotByCommunes to load |
url |
the URL to the px file with all federal ballots |
output |
the output file name to be saved in the package inst/extdata |
a matrix, rownames are communes and colnames are federal ballot IDs. Check the attributes ballotName and date (same length as ncol) and communeName (same lenghth as nrow)
https://www.bfs.admin.ch/bfs/en/home/statistics/politics/popular-votes.assetdetail.1363949.html
## Not run:
require(tidyverse)
fBallot <- loadCommunesCHFederalBallot()
attr(fBallot, "ballotName")
# get only naturalisation facilitée ballots
cidx <- match(c("3150", "4110", "5100", "5110"), colnames(fBallot))
ballot_nat <- fBallot[,cidx]
colnames(ballot_nat) <- attr(fBallot, "ballotName")[cidx]
ballot_nat <- ballot_nat %>%
data.frame() %>%
rownames_to_column(var = "GMDNR") %>%
mutate(GMDNR = as.integer(GMDNR))
# Plot maps
require(sf)
# Get municipalities geodata
shp_ch_paths_2016 <- shp_path(2016, features=c('municipalities'))
shp_ch_geodata <- shp_ch_paths_2016 %>% map(function(x) {
layerName <- st_layers(x)
st_read(x, layer = layerName[[1]]) %>%
select(ends_with("NR"), ends_with("NAME"))
})
municipalities <- shp_ch_geodata[[1]]
# Bind ballot data and make it long (duplicate)
municipalities <- left_join(municipalities, ballot_nat) %>%
select(-BZNR, -GRNR) %>%
gather(ballot, percYes, -GMDNR, -KTNR, -GMDNAME, -geometry)
# discretize data with breaks
brks <- seq(from = 0, to = 1, length.out = 11) * 100
municipalities <- municipalities %>%
mutate(bins = cut(percYes, breaks = brks, right = F))
ggplot() +
geom_sf(data = municipalities, aes(fill = bins), lwd = 0) +
facet_wrap(~ ballot) +
scale_fill_brewer(palette = "BrBG" , drop = F) +
coord_sf(datum = NA, expand = F) +
theme_map()
## End(Not run)
## Not run:
processFederalBallotByCommunes()
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.