cantonal_ch_federalBallot: Federal ballot results by cantons

loadCantonsCHFederalBallotR Documentation

Federal ballot results by cantons

Description

Load all 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

Usage

loadCantonsCHFederalBallot(file = "federalBallot_cantons.RData")

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

Arguments

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

Details

Run processFederalBallotByCantons() to generate a readable csv file for loadCantonsCHFederalBallot

Value

a matrix, rownames are cantons (2 letters name) and colnames are federal ballot IDs. Check the attributes ballotName and date

See Also

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

Examples

## 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))
ballot_nat <- fBallot[,cidx]
colnames(ballot_nat) <- attr(fBallot, "ballotName")[cidx]
ballot_nat <- ballot_nat %>% 
  data.frame() %>% 
  rownames_to_column(var = "iso2")
# get the cantonal IDs
ballot_nat <- left_join(ballot_nat, canton_CH() %>% select(order, iso2)) %>% select(-iso2)
require(sf)
require(tidyverse)

# get canton shapefiles as a sf data.frame
path.cantons <-  shp_path(2018, features = "cantons")
ca <- st_read(path.cantons, layer = "g2k18") %>%
  select(KTNR, KTNAME)
  
# Bind ballot data and make it long (duplicate for each ballot)
ca <- left_join(ca, ballot_nat, by=c("KTNR" = "order")) %>% 
  gather(ballot, percYes, -KTNR, -KTNAME, -geometry)
  
# Create breaks
brks <- seq(from = 0, to = 1, length.out = 11) * 100
ca <- ca %>% mutate(
  bins = cut(percYes, breaks = brks, right = F)
)

# plot
gp <- ggplot(ca) +
  geom_sf(aes(fill = bins), lwd = 0) +
  facet_wrap(~ ballot) + 
  theme_map()+ 
  scale_fill_brewer(palette = "BrBG" , drop = F)
  
 gp + ggtitle('Votes naturalisation')

## End(Not run)
## Not run: 
processPortraitsRegionauxCantons()

## End(Not run)

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