Load necessary libraries

#Load libraries
library(tidyverse)
library(ggthemepark)
library(swissdd)
library(swissgd)
library(sf)
library(glm.predict)
library(readxl)

Import zmer and geo data

#Import data
mentees <- read_excel("Mentees_Frauenzentrale.xls")

zmer <- read_csv(file.choose())


zmer <- zmer %>%
  mutate(
    party2= 
      case_when(
        party== "JUSO" ~ "SP",
        party== "Junge Mitte" ~ "Die Mitte",
        party== "JGLP" ~ "GLP",
        party %in% c("SVP", "SP", "EVP", "GLP", "FDP", "Grüne", "parteilos", "Die Mitte") ~ party,
        T ~ "other"
      ),
    age= 2022-birthyear,
    
    mun_name= ifelse(mun_id== 21, "Andelfingen", mun_name),
    mun_id= ifelse(mun_id== 21, 30, mun_id)
  ) %>%
  group_by(type, municipality, round) %>%
  mutate(candidates= n()) %>%
  ungroup() %>%
  mutate(candidates=ifelse(tacit_election == 0, candidates-1,
                           candidates),
         can_per_seat = candidates / mandates)




geo_data <-  get_geodata(geolevel = "municipality")%>% 
  mutate(mun_id= as.numeric(mun_id)) %>%
  filter(mun_id <=298)

geo_data2 <- st_read( "C:\\Users\\nickg\\OneDrive - Universität Zürich UZH\\Documents\\Statistics\\Datenjournalismus\\SHAPEFILE_LV95_LN02\\swissBOUNDARIES3D_1_3_TLM_HOHEITSGEBIET.shp") %>%
  filter(BFS_NUMMER <=298) %>%
  rename(mun_id= BFS_NUMMER)
geo_data2 <- st_read(file.choose()) %>%
  filter(BFS_NUMMER <=298) %>%
  rename(mun_id= BFS_NUMMER)

geo_data2 %>%
  select(mun_id, NAME)

mun_name <- unique(zmer$mun_name)
mun_id <- unique(zmer$mun_id)
info <- data.frame(name= mun_name, mun_id=mun_id)
geo_data3 <- left_join(geo_data2, info) %>%
  select(name, NAME, mun_id)

Descriptive analysis

zmer %>%
  filter(type=="GE" | type2== "SchP" )%>%
  #filter(!(type== "PS" & unified != "unified: opt 1")) %>%
  mutate( elected_ge=
            case_when(
              type2== "GE" & elected==1 ~ 1,
              type2== "SchP" & elected_pres==1 & unified == "unified: opt 1" ~1,
              T~0
            )) %>%
  filter(elected_ge ==1)%>%
  group_by(mun_name, mun_id)%>%
  mutate(sex2 = ifelse(sex=="m", 0, 1)) %>%
  summarise(n=sum(elected_ge),fem_quote = mean(sex2))%>%
  arrange(fem_quote)%>% 
  filter(fem_quote== 0) 


only_school <- zmer %>%
  filter(type=="GE" | type2== "SchP" )%>%
  #filter(!(type== "PS" & unified != "unified: opt 1")) %>%
  mutate( elected_ge=
            case_when(
              type2== "GE" & elected==1 ~ 1,
              type2== "SchP" & elected_pres==1 & unified == "unified: opt 1" ~1,
              T~0
            )) %>%
  filter(elected_ge ==1, type2== "SchP") %>%
  mutate(sex2= ifelse(sex=="m",0,1))%>%
  summarise(fem_quote= mean(sex2))

zmer %>%
  filter(lname!= "Vereinzelte") %>%
  ggplot(aes(x=sex))+
  geom_bar()+
  theme_bw()+
  facet_wrap(~type2)

zmer %>%
  filter(elected==1) %>%
  group_by(sex) %>%
  summarise(n=n())%>%
  mutate(perc= n/sum(n))

Plots

Map

zmer %>%
  filter(type=="GE" | type2== "SchP" )%>%
  #filter(!(type== "PS" & unified != "unified: opt 1")) %>%
  mutate( elected_ge=
            case_when(
              type2== "GE" & elected==1 ~ 1,
              type2== "SchP" & elected_pres==1 & unified == "unified: opt 1" ~1,
              T~0
            )) %>%
  filter(elected_ge ==1)%>%
  group_by(mun_name, mun_id)%>%
  mutate(sex2 = ifelse(sex=="m", 0, 1)) %>%
  summarise(n=sum(elected_ge),fem_quote = mean(sex2)) %>%
  mutate(fem_quote_factor=
           factor(case_when(
             fem_quote < 0.20 ~ "",
             fem_quote >= 0.20 & fem_quote < 0.25 ~ "20", 
             fem_quote >= 0.25 & fem_quote < 0.30 ~ "25",
             fem_quote >= 0.30 & fem_quote < 0.35 ~ "30",
             fem_quote >= 0.35 & fem_quote < 0.40 ~ "35",
             fem_quote >= 0.40 & fem_quote < 0.50 ~ "40",
             fem_quote >= 0.50 & fem_quote < 0.55 ~ "50",
             fem_quote >= 0.55 & fem_quote < 0.60 ~ "55",
             fem_quote >= 0.60 ~ "60"
           ), levels = c("", "20", "25", "30", "35", "40", "50", "55", "60")
           ))%>%
  left_join(., geo_data3) %>%
  ggplot()+
  geom_sf(aes(fill=fem_quote_factor, geometry = geometry), color="white", linewidth=0.5)+
  scale_fill_manual(
    values = c(
      "#680d25", "#8d0613", "#BD2031", "#cf352e", "#f1434a", "#ff9193",
      

      "#91cdff", "#42a2f1", "#105182"
    ),
    drop = F,
    name = "Frauenanteil",
    guide = guide_legend(
      direction = "horizontal",
      keyheight = unit(2, units = "mm"),
      keywidth = unit(c(25, rep(7, 7), 25), units = "mm"),
      title.position = "top",
      title.hjust = 0,
      label.hjust = 1,
      nrow = 1,
      byrow = T,
      reverse = T,
      label.position = "bottom"))+  
  theme_void()+
  theme(legend.position = "bottom")+
  annotate(
    geom = "curve", x = 2700000, y = 1229000 , xend = 2696900, yend = 1245000,
    curvature = -0.15, size=1,
    arrow = arrow(length = unit(2, "mm"), type = "closed"))+
  annotate(
    geom = "text", x = 2700900, y = 1229000, label= "Uster, die Stadt mit dem \nhöchsten Frauenanteil in \nder Exekutive",
    size=3, hjust = "bottom")+
  annotate(
    geom = "curve", x = 2670000, y = 1276000 , xend = 2685300, yend = 1265000,
    curvature = 0.15, size=1,
    arrow = arrow(length = unit(2, "mm"), type = "closed"))+
  annotate(
    geom = "text", x = 2670000, y = 1280900, label= "Rorbas, eine von \n5 Gemeinden ohne \nFrauen in der \nExekutive",
    size=3, hjust = "bottom")+
  labs(title= "Wenige Gemeinden mit Frauenmehrheit im Gemeindevorstand",
       subtitle = "Karte der Zürcher Gemeinden und ihrem Frauenanteil")
ggsave("plots/map.jpeg")

Barplot

zmer %>%
  filter(elected== 1) %>%
  group_by(type2, sex) %>%
  tally() %>%
  group_by(type2) %>%
  mutate(perc= n/ sum(n)) %>%
  ggplot(aes(x= type2, y= perc, fill= sex))+
  geom_col(position = "dodge")+
  geom_text(aes(label= paste0(round(perc,3)*100,"%")), vjust = -0.5, 
            position = position_dodge(width = 0.9))+
  scale_y_continuous(labels = scales::percent, breaks = seq(0,1,0.25))+
  scale_x_discrete(labels = c("GE"= "Gemeindeexekutive", "RPK"= "Rechnungsprüfungskommission",
                              "SchP"= "Schulpflege"))+
  scale_fill_manual(values= c("#53A2BE","#357266"),
                    labels= c("Frauen","Männer"))+
  labs(x="", y="",
       fill= "Geschlecht",
       title = "Massiver Männerüberschuss in Exekutive und RPK, \nkleiner Frauenüberschuss in den Schulpflegen",
       subtitle = "Prozent gewählte Personen nach Geschlecht und Behörde")+
  theme_minimal()+
  theme(legend.position = "right",
        legend.title= element_blank(),
        panel.grid.major.x = element_blank(),
        axis.line.x = element_line())
ggsave("plots/perc.jpeg")

Statistical Analysis

model1 <- glm(elected ~ sex*incumbent +type2 + party2+
                round + age +
                incumbent_pres + can_per_seat,
              data = zmer)
summary(model1)

predicts(model1, "F;0;mode;mode;mode;median;0;1") %>%
  ggplot(aes(x=sex, y=mean, ymin= lower, ymax=upper))+
  geom_errorbar(width=0.1)+
  geom_point(size=2)+
  scale_x_discrete(labels= c("Frauen", "Männer"))+
  scale_y_continuous(labels = scales::percent, breaks = seq(0,1,0.05))+
  labs(x="", y="",
       title = "Frauen mit hohen Wahlwahrscheinlichkeiten",
       subtitle = "Vorhergesagte Wahlwahrscheinlichkeit nach Geschlecht",
       caption = "Alle weiteren Prädiktoren wurden auf den Median oder Modus gesetzt")+
  theme_bw()+
  theme(panel.grid.major.x = element_blank())
ggsave("plots/predicts.jpeg")