Description Usage Arguments Details Value See Also Examples
Load any federal ballots resuts by cantons: https://www.pxweb.bfs.admin.ch/Selection.aspx?px_language=fr&px_db=px-x-1703030000_100&px_tableid=px-x-1703030000_100\px-x-1703030000_100.px&px_type=PX
Process Portraits régionaux de la Suisse canton 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 loadCantonsCHFederalBallot
1 2 3 4 5 | loadCantonsCHFederalBallot(file = "federalBallot_cantons.RData")
processFederalBallotByCantons(url = "https://www.pxweb.bfs.admin.ch/DownloadFile.aspx?file=px-x-1703030000_100",
output = "federalBallot_cantons.RData")
|
file |
the name of the csv file processed by processFederalBallotByCantons 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 |
Run processFederalBallotByCantons() to generate a readable csv file for loadCantonsCHFederalBallot
a matrix, rownames are cantons (2 letters name) and colnames are federal ballot IDs. Check the attributes ballotName and date
https://www.bfs.admin.ch/bfs/en/home/statistics/politics/popular-votes.assetdetail.1363949.html
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 45 46 47 | ## Not run:
fBallot <- loadCantonsCHFederalBallot()
attr(fBallot, "ballotName")
cidx <- grep("naturalisation", attr(fBallot, "ballotName"), ignore.case = T)
attr(fBallot, "ballotName")[cidx]
attr(fBallot, "date")[cidx]
# 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)
ca <- readOGR(path.ch, layer = 'cantons')
ca.df <- formatShp(ca) %>% select(long, lat, group, KANTONSNUM)
# duplicte canton data.frame for each ballot
df <- do.call(rbind, lapply(cidx, function(idx) {
value <- fBallot[,idx]
names(value) <- canton_CH[match(names(value), canton_CH[,1]), "order"]
res <- ca.df
res$value <- value[match(res$KANTONSNUM, names(value))]
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:
processPortraitsRegionauxCantons()
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.