communes_ch_federalBallot: Federal ballot results by communes

Description Usage Arguments Details Value See Also Examples

Description

Load any federal ballots resuts by communes: https://www.pxweb.bfs.admin.ch/DownloadFile.aspx?file=px-x-1703030000_101

Process Portraits régionaux de la Suisse commune px file 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 Necessary to run it before using loadCommunesCHFederalBallot

Usage

1
2
3
4
5
loadCommunesCHFederalBallot(file = "federalBallot_communes.RData")


  processFederalBallotByCommunes(url = "https://www.pxweb.bfs.admin.ch/DownloadFile.aspx?file=px-x-1703030000_101",
  output = "federalBallot_communes.RData")

Arguments

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

Details

Run processFederalBallotByCommunes() to generate a readable csv file for loadCommunesCHFederalBallot

Value

a matrix, rownames are cantons (2 letters name) and colnames are federal ballot IDs. Check the attributes ballotName and date (same length as ncol) and communeName (same lenghth as nrow)

See Also

https://www.bfs.admin.ch/bfs/en/home/statistics/politics/popular-votes.assetdetail.1363949.html

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
## Not run: 
fBallot <- loadCommunesCHFederalBallot()
attr(fBallot, "ballotName")
# get only naturalisation facilitée ballots
cidx <- match(c("3150", "4110", "5100", "5110"), colnames(fBallot))
attr(fBallot, "ballotName")[cidx]
fBallot[,cidx]
# Plot maps
require(rgdal)
require(rgeos)
require(ggplot2)
require(maptools)
require(dplyr)

# get canton shapefiles as a data.frame
path.ch <- getPathShp('CH')
layers <-  ogrListLayers(path.ch)
mu <- readOGR(path.ch, layer = 'municipalities')
mu.df <- formatShp(mu) %>% select(long, lat, group, BFS_NUMMER)
r.idx <- match(mu.df$BFS_NUMMER, rownames(fBallot))
# duplicate commune data.frame for each ballot
df <- do.call(rbind, lapply(cidx, function(idx) {
  value <- fBallot[,idx]
  res <- mu.df
  res$value <- value[r.idx]
  res$ballot <- attr(fBallot, "ballotName")[idx]
  res$date <- attr(fBallot, "date")[idx]
  res
}))
# plot maps
brks <- seq(from = 0, to = 1, length.out = 11) * 100
df$bins <- cut(df$value, breaks = brks, right = F)
df$ballot <- factor(df$ballot, levels = attr(fBallot, "ballotName")[cidx])
ggplot(df, aes(x = long, y = lat, group = group)) + geom_polygon(size = 0, aes(fill = bins)) +
theme_minimal() + theme(legend.position = "bottom", panel.grid = element_blank(), 
axis.ticks = element_blank(), axis.title = element_blank(), axis.text = element_blank()) +
facet_wrap(~ ballot) + scale_fill_brewer(palette = "BrBG" , drop = F) + 
coord_quickmap(expand = F)

## End(Not run)
## Not run: 
processFederalBallotByCommunes()

## End(Not run)

d-qn/swiMap documentation built on May 14, 2019, 3:04 p.m.