knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
introductory remarks
library(voteR)
Scrape all available polls.
df <- scrape_wahlrecht_bund()
Explore how many polls are available in total:
nrow(df)
And split up by institute:
df %>% pull(institute) %>% table()
Check out the availbe variables:
colnames(df)
Print the first 10 observations to see an example:
df %>% slice(1:10)
Furthermore, a set of institutes to be included can be selected via a character vector passed to the institutes
parameter. Possible options include allensbach
, emnid
, forsa
, politbarometer
, gms
, dimap
, insa
, and yougov
scrape_wahlrecht_bund(institutes = c("dimap","politbarometer"))
Lastly, for some institutes (namely Politbarometer
and GMS
), it is also possible to include (declared) raw-data which has not been post-treated or weighted. These data points are excluded by default but can be included by using the include_rawdata = TRUE
option.
scrape_wahlrecht_bund(include_rawdata = T) %>% filter(raw_by_institute == T)
library(ggplot2) ggpoll <- df %>% gather(party,pollscore,`cdu/csu`:others) %>% filter(!party %in% c("others")) %>% ggplot(aes(x = as.Date(date,"%d.%m.%Y"), y = pollscore)) + geom_point(alpha = 0.1) + geom_smooth(aes(col = party)) + facet_wrap(~party,scales = "free") ggpoll
Restrict graph to last legislative period only:
ggpoll2 <- ggpoll + scale_x_date(limits = c(as.Date("2017-09-22"),Sys.Date())) ggpoll2
And adjust party colors accordingly:
partycolors_ger <- c(spd = "red", cdu = "black", cdu_csu = "black", `cdu/csu` = "black", csu = "darkblue", gruene = "darkgreen", grĂ¼ne = "darkgreen", fdp = "#999900", afd = "blue", piraten = "orange", linke = "purple", andere = "black", sonstige = "grey") ggpoll2 + scale_color_manual(values = partycolors_ger) + theme_minimal() + labs(x = "Date", y = "Pollscore")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.