Read, clean, manipulate data

read data; all files at once; include file name as file-id; locale ‘latin1’ for Umlaute (ü etc).

extract year from filename.

take only those rows pertaining to Vienna and district levels; sort (leader and runners-up)

get leaders and runners-up into one row; calculate vote difference;

identify those district in which leader changed between 2017 and 2019

Plot

plot_wien <- Wien_x %>% 
  ungroup() %>% 
  mutate(jahr=as_factor(jahr)) %>% 
  ggplot()+
  geom_rect(data=spo_loss %>% filter(spo_loss=="loss"), 
            aes(xmin=-Inf, xmax=Inf,
                ymin=-Inf, ymax=Inf),
            fill="grey30",
            alpha=0.2)+
  geom_segment(aes(x=jahr, xend=jahr,
                   y=votes_rel, yend=votes_rel_runnersup))+
  geom_text(aes(x=jahr,
                y=votes_rel_runnersup+votes_diff/2,
                label=round(votes_diff, 1)),
            nudge_x=0.2,
            size=3)+
  geom_point(aes(y=votes_rel_runnersup,
                 x=jahr,
                 color=party_runnersup),
             size=3) +
    geom_point(aes(x=jahr, 
                 y=votes_rel,
                 color=party),
             size=3)+
  facet_rep_wrap(vars(gebietsname), repeat.tick.labels = T)+
  scale_y_continuous(limits=c(0, 50), 
                     breaks=seq(0, 50, 20),
                     labels=scales::percent_format(scale=1))+
  theme_minimal()+
  scale_color_manual(values=c("grune"="#A3C630", "neos"="#EA5290", "fpo"="#005DA8",
                              "spo"="#FC0204", "ovp"="#5DC2CC"),
                     labels=c("grune"="Grüne", "neos"="Neos", "fpo"="FPÖ",
                              "ovp"="ÖVP", "spo"="SPÖ"))+
  theme(panel.grid.minor.y = element_blank(),
        panel.grid.major.x = element_blank(),
        plot.caption = element_markdown(color="grey30"),
        legend.position = "bottom",
        axis.title.x = element_blank(),
        axis.title.y = element_text(size=rel(0.8), hjust=1, color="grey30"),
        #plot.title = element_markdown(),
        legend.title = element_blank(),
        strip.text = element_text(face="bold", hjust=0))+
  labs(title="Nationalratswahl 2019 - Wien: Abstand zwischen stärkster und zweitstärkster Partei je Bezirk",
       subtitle="Graue Unterlegung: SPÖ verliert Position als stärkste Partei. Vergleich zu NRW 2017.",
       y="% Stimmen",
       caption="Daten: data.gv.at, Stand: 2019-09-30, 21:34:06<br>Roland Schmidt | @zoowalk | <span style='color:black'>**werk.statt.codes**</span>")

move legend to bottom-right empty space.

#Wienliebe