# Load Data and packages

library(dplyr)
library(readxl)
library(tidyverse)
library(knitr)
library(ggplot2)
library(ggpubr)
library(stringr)
library(reshape2)

# Load Data

file <- "CoronaMonitorAll.csv"
corona <-read.csv(file,
                  sep=",",
                  encoding = "UTF-8",
                  stringsAsFactors = FALSE)

### 1. Inspect data

# Which variables are in the dataset

#colnames(corona)

# How many municipalities are in the dataset?

#unique(corona$gmd)

## Prepare dataset to merge on Gemeindenummer

# Backup and remove all rows with gmd = NA in new dataset

corona.backup <- corona

corona <- corona %>% filter(!is.na(gmd))

# Check

#unique(corona1$gmd)

### 2. Merge data on Gemeindenummer

# import .xlsx from bfs

raum_stadt <- read_excel("raum_stadt.xlsx")
agglo_groesse <- read_excel("agglo_groesse.xlsx")

# Clean up and better variable names

raum_stadt <- na.omit(raum_stadt)
agglo_groesse <- na.omit(agglo_groesse)

# rename colnames

raum_stadt <- raum_stadt %>% rename("gmd" = "Der Raum mit städtischem Charakter am 18.12.2014*", 
                                    "Gemeinde" = "...2",
                                    "stadt" = "17718")
raum_stadt <- select(raum_stadt,-c(2))

agglo_groesse <- agglo_groesse %>% rename("gmd" = "5 Agglomerations-Grössenklassen der Schweiz am 31.12.2012", 
                                    "Gemeinde" = "...2",
                                    "agglo" = "17773")

# Remove gmds in neigbouring countries to get 2352 gmds in both datasets

raum_stadt <- raum_stadt[! grepl("[A-Z]", raum_stadt$gmd),]

#colnames()

# Rename variable values

agglo_groesse <- agglo_groesse %>% 
  mutate(agglo = case_when(agglo == "Keine Agglomerationszugehörigkeit" ~ 0,
                           agglo == "500 000 und mehr Einwohner/innen" ~ 1,
                           agglo == "200 000 - 499 999 Einwohner/innen" ~ 2, 
                           agglo == "100 000 - 199 999 Einwohner/innen" ~ 3, 
                           agglo == "50 000 -   99 999 Einwohner/innen" ~ 4, 
                           agglo == "< 50 000 Einwohner/innen" ~ 5))

raum_stadt <- raum_stadt %>% 
  mutate(stadt = case_when(stadt == "Ländliche Gemeinde ohne städtischen Charakter" ~ 0,
                           stadt == "Agglomerationskerngemeinde (Kernstadt)" ~ 1,
                           stadt == "Agglomerationskerngemeinde (Hauptkern)" ~ 2, 
                           stadt == "Agglomerationskerngemeinde (Nebenkern)" ~ 3, 
                           stadt == "Agglomerationsgürtelgemeinde" ~ 4, 
                           stadt == "Mehrfach orientierte Gemeinde" ~ 5,
                           stadt == "Kerngemeinde ausserhalb Agglomerationen" ~ 6))
#unique(raum_stadt$stadt)

# Merge the dataframes by "gmd"

corona.merged <- merge(x = corona, y = agglo_groesse, by = "gmd" , all.x = TRUE)

corona.merged <- merge(x = corona.merged, y = raum_stadt, by = "gmd" , all.x = TRUE)

# Create the five categories

# Stadt/Land Einteilung

corona.merged <- corona.merged %>% 
  mutate(stadt.land = case_when(agglo %in% c(1,2,3) & stadt %in% c(1,2) ~ "grosse Kernstadt", #condition 1
                         agglo %in% c(4,5,0) & stadt %in% c(1,2) ~ "kleine Kernstadt", #condition 2
                         agglo %in% c(1,2,3) & stadt %in% c(3,4,5) ~ "grosse Agglomeration", #condition 3
                         agglo %in% c(4,5,0) & stadt %in% c(3,4,5) ~ "kleine Agglomeration", #condition 4
                         stadt %in% c(0,6) ~ "ländlicher Raum")) #condition 5

#table(corona.merged$stadt.land)
#sum(is.na(corona.merged.sel$stadt.land))

# Es gibt Gemeinden, die durch Fusionen erst nach 2015 entstanden sind. Diese Gemeinden () sind in den Datensätzen, die der Einteilung in städtische und ländliche Gemeinden zugrunde liegen, noch nicht eingetragen. Sie wurden deshalb in dieser Arbeit nicht berücksichtigt. Es handelt sich im 2048 von insgesamt 142'335 Beobachtungen.  

### 3. Create final Dataset with only potentially relevant data for this project

corona.merged.sel <- corona.merged %>%
  select("gmd", "stadt.land", "agglo", "stadt", "Gemeinde", "kanton", "alltagHeraus", "coronagefahr", "massnBewegung", "massnLockdown", "massnWirtsch", "massnLohn", "reaktionSpeed", "vertrauen", "Welle", "datestamp", "singVerbot", "sportVerbot", "massnQuarantaene", "alltagcontact")

# Remove NAs from variable Stadt.Land

corona.merged.sel <- corona.merged.sel %>% filter(!is.na(stadt.land))

### 4. Get Plots for massnBewegung

## Welle 1 massnBewegung

## remove blanks in variable massnBewegung

corona.merged.bew <- corona.merged.sel %>% filter(!massnBewegung=="")

# Divide dataset into five waves

corona.1.bew <- corona.merged.bew %>%
  filter(Welle == "1")

corona.2.bew <- corona.merged.bew %>%
  filter(Welle == "2")

corona.3.bew <- corona.merged.bew %>%
  filter(Welle == "3")

corona.4.bew <- corona.merged.bew %>%
  filter(Welle == "4")

corona.5.bew <- corona.merged.bew %>%
  filter(Welle == "5")

# Get percentages for massnBewegung 

# Create frequency table

table.1.bew <- corona.1.bew %>%
        group_by(massnBewegung, stadt.land) %>%
        tally()

# get percentages

perc.1.bew <- table.1.bew %>%
  group_by(stadt.land) %>%
  mutate(percentage1 = n/sum(n) * 100)

## Welle 2 massnBewegung

table.2.bew <- corona.2.bew %>%
        group_by(massnBewegung, stadt.land) %>%
        tally()

perc.2.bew <- table.2.bew %>%
  group_by(stadt.land) %>%
  mutate(percentage2 = n/sum(n) * 100)

## Welle 3 massnBewegung

table.3.bew <- corona.3.bew %>%
        group_by(massnBewegung, stadt.land) %>%
        tally()

perc.3.bew <- table.3.bew %>%
  group_by(stadt.land) %>%
  mutate(percentage3 = n/sum(n) * 100)

## Welle 4 massnBewegung

table.4.bew <- corona.4.bew %>%
        group_by(massnBewegung, stadt.land) %>%
        tally()

perc.4.bew <- table.4.bew %>%
  group_by(stadt.land) %>%
  mutate(percentage4 = n/sum(n) * 100)

## Welle 5 massnBewegung

table.5.bew <- corona.5.bew %>%
        group_by(massnBewegung, stadt.land) %>%
        tally()

perc.5.bew <- table.5.bew %>%
  group_by(stadt.land) %>%
  mutate(percentage5 = n/sum(n) * 100)

# create one table for all observations

table.massnBewegung <- perc.1.bew
table.massnBewegung <- table.massnBewegung[ -c(3) ]
table.massnBewegung$percentage2 <- perc.2.bew$percentage2 
table.massnBewegung$percentage3 <- perc.3.bew$percentage3
table.massnBewegung$percentage4 <- perc.4.bew$percentage4 
table.massnBewegung$percentage5 <- perc.5.bew$percentage5 

### 4.1 Show results in stacked barplots

# change order of factors

table.massnBewegung$massnBewegung <- factor(table.massnBewegung$massnBewegung, levels=c("Gehen viel zu weit", "Gehen eher zu weit", "Sind angemessen", "Gehen eher zu wenig weit","Gehen viel zu wenig weit"))

table.massnBewegung$stadt.land <- factor(table.massnBewegung$stadt.land, levels=c("grosse Kernstadt", "kleine Kernstadt", "grosse Agglomeration", "kleine Agglomeration","ländlicher Raum"))

# percentage stacked barplot

data <- table.massnBewegung

plot.1.bew <- ggplot(data, aes(fill=table.massnBewegung$massnBewegung, y=table.massnBewegung$percentage1, x=table.massnBewegung$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "März",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels=function(x) paste0(x,"%")) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 8, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1)) 

plot.2.bew <- ggplot(data, aes(fill=table.massnBewegung$massnBewegung, y=table.massnBewegung$percentage2, x=table.massnBewegung$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "Bewegungsfreiheit \nim April",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels = scales::percent) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 7.5, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1))


plot.3.bew <- ggplot(data, aes(fill=table.massnBewegung$massnBewegung, y=table.massnBewegung$percentage3, x=table.massnBewegung$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "Mai",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels=function(x) paste0(x,"%")) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 8, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1))

plot.4.bew <- ggplot(data, aes(fill=table.massnBewegung$massnBewegung, y=table.massnBewegung$percentage4, x=table.massnBewegung$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "Bewegungsfreiheit \nim Juni",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels = scales::percent) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 7.5, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1))

plot.5.bew <- ggplot(data, aes(fill=table.massnBewegung$massnBewegung, y=table.massnBewegung$percentage5, x=table.massnBewegung$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "Bewegungsfreiheit \nim Oktober",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels=function(x) paste0(x,"%")) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 7.5, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank())

#axis.text.y = element_blank(),
#axis.ticks.y = element_blank()

# Get a nice plot for overview

plot.wrap.bew <- ggarrange(plot.1.bew, plot.2.bew, plot.3.bew, plot.4.bew, plot.5.bew, nrow = 1, common.legend = TRUE, legend = "top")
annotate_figure(plot.wrap.bew, top = text_grob("Einschränkung der Bewegungsfreiheit", size = 12),
               bottom = text_grob("Daten: Sotomo", face = "italic", size = 7))

### 5. Get Plot for massnLohn

## Welle 1 massnLohn

## remove blanks in varaible massnBewegung

corona.merged.lohn <- corona.merged.sel %>% filter(!massnLohn=="")

# Divide dataset into five waves

corona.1.lohn <- corona.merged.lohn %>%
  filter(Welle == "1")

corona.2.lohn <- corona.merged.lohn %>%
  filter(Welle == "2")

corona.3.lohn <- corona.merged.lohn %>%
  filter(Welle == "3")

corona.4.lohn <- corona.merged.lohn %>%
  filter(Welle == "4")

corona.5.lohn <- corona.merged.lohn %>%
  filter(Welle == "5")

# Get percentages for massnLohn

# Create frequency table

table.1.lohn <- corona.1.lohn %>%
        group_by(massnLohn, stadt.land) %>%
        tally()

# get percentages

perc.1.lohn <- table.1.lohn %>%
  group_by(stadt.land) %>%
  mutate(percentage1 = n/sum(n) * 100)

## Welle 2 massnLohn

table.2.lohn <- corona.2.lohn %>%
        group_by(massnLohn, stadt.land) %>%
        tally()

perc.2.lohn <- table.2.lohn %>%
  group_by(stadt.land) %>%
  mutate(percentage2 = n/sum(n) * 100)

## Welle 3 massnLohn

table.3.lohn <- corona.3.lohn %>%
        group_by(massnLohn, stadt.land) %>%
        tally()

perc.3.lohn <- table.3.lohn %>%
  group_by(stadt.land) %>%
  mutate(percentage3 = n/sum(n) * 100)

## Welle 4 massnLohn

table.4.lohn <- corona.4.lohn %>%
        group_by(massnLohn, stadt.land) %>%
        tally()

perc.4.lohn <- table.4.lohn %>%
  group_by(stadt.land) %>%
  mutate(percentage4 = n/sum(n) * 100)

## Welle 5 massnLohn

table.5.lohn <- corona.5.lohn %>%
        group_by(massnLohn, stadt.land) %>%
        tally()

perc.5.lohn <- table.5.lohn %>%
  group_by(stadt.land) %>%
  mutate(percentage5 = n/sum(n) * 100)

# create one table for all observations

table.massnLohn <- perc.1.lohn
table.massnLohn <- table.massnLohn[ -c(3) ]
table.massnLohn$percentage2 <- perc.2.lohn$percentage2 
table.massnLohn$percentage3 <- perc.3.lohn$percentage3
table.massnLohn$percentage4 <- perc.4.lohn$percentage4 
table.massnLohn$percentage5 <- perc.5.lohn$percentage5 

### 5.1 Show results in stacked barplots

# change order of factors

table.massnLohn$massnLohn <- factor(table.massnLohn$massnLohn, levels=c("Gehen viel zu weit", "Gehen eher zu weit", "Sind angemessen", "Gehen eher zu wenig weit","Gehen viel zu wenig weit"))

table.massnLohn$stadt.land <- factor(table.massnLohn$stadt.land, levels=c("grosse Kernstadt", "kleine Kernstadt", "grosse Agglomeration", "kleine Agglomeration","ländlicher Raum"))

# percentage stacked barplot

data1 <- table.massnLohn

plot.1.lohn <- ggplot(data1, aes(fill=table.massnLohn$massnLohn, y=table.massnLohn$percentage1, x=table.massnLohn$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "März",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels=function(x) paste0(x,"%")) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 8, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1)) 

plot.2.lohn <- ggplot(data1, aes(fill=table.massnLohn$massnLohn, y=table.massnLohn$percentage2, x=table.massnLohn$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "Abfederung Lohnausfall \nim April",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels = scales::percent) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 7.5, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1))

plot.3.lohn <- ggplot(data1, aes(fill=table.massnLohn$massnLohn, y=table.massnLohn$percentage3, x=table.massnLohn$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "Mai",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels=function(x) paste0(x,"%")) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 8, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1))

plot.4.lohn <- ggplot(data1, aes(fill=table.massnLohn$massnLohn, y=table.massnLohn$percentage4, x=table.massnLohn$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "Abfederung Lohnausfall \nim Juni",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels = scales::percent) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 7.5, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1))

plot.5.lohn <- ggplot(data1, aes(fill=table.massnLohn$massnLohn, y=table.massnLohn$percentage5, x=table.massnLohn$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "Abfederung Lohnausfall \nim Oktober",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels=function(x) paste0(x,"%")) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 7.5, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank())

# Get a nice plot

plot.wrap.lohn <- ggarrange(plot.1.lohn, plot.2.lohn, plot.3.lohn, plot.4.lohn, plot.5.lohn, nrow = 1, common.legend = TRUE, legend = "top")
annotate_figure(plot.wrap.lohn, top = text_grob("Abfederung von Lohnausfällen", size = 12),
               bottom = text_grob("", face = "italic", size = 7))

### 6. Get Plot for massnLock

## Welle 1 massnLock

## remove blanks in variable massnLockdown

corona.merged.lock <- corona.merged.sel %>% filter(!massnLockdown=="")

# Divide dataset into five waves

corona.1.lock <- corona.merged.lock %>%
  filter(Welle == "1")

corona.2.lock <- corona.merged.lock %>%
  filter(Welle == "2")

corona.3.lock <- corona.merged.lock %>%
  filter(Welle == "3")

corona.4.lock <- corona.merged.lock %>%
  filter(Welle == "4")

corona.5.lock <- corona.merged.lock %>%
  filter(Welle == "5")

# Get percentages for massnLock

# Create frequency table

table.1.lock <- corona.1.lock %>%
        group_by(massnLockdown, stadt.land) %>%
        tally()

# get percentages

perc.1.lock <- table.1.lock %>%
  group_by(stadt.land) %>%
  mutate(percentage1 = n/sum(n) * 100)

## Welle 2

table.2.lock <- corona.2.lock %>%
        group_by(massnLockdown, stadt.land) %>%
        tally()

perc.2.lock <- table.2.lock %>%
  group_by(stadt.land) %>%
  mutate(percentage2 = n/sum(n) * 100)

## Welle 3

table.3.lock <- corona.3.lock %>%
        group_by(massnLockdown, stadt.land) %>%
        tally()

perc.3.lock <- table.3.lock %>%
  group_by(stadt.land) %>%
  mutate(percentage3 = n/sum(n) * 100)

## Welle 4

table.4.lock <- corona.4.lock %>%
        group_by(massnLockdown, stadt.land) %>%
        tally()

perc.4.lock <- table.4.lock %>%
  group_by(stadt.land) %>%
  mutate(percentage4 = n/sum(n) * 100)

## Welle 5

table.5.lock <- corona.5.lock %>%
        group_by(massnLockdown, stadt.land) %>%
        tally()

perc.5.lock <- table.5.lock %>%
  group_by(stadt.land) %>%
  mutate(percentage5 = n/sum(n) * 100)

# create one table for all observations

table.massnlock <- perc.1.lock
table.massnlock <- table.massnlock[ -c(3) ]
table.massnlock$percentage2 <- perc.2.lock$percentage2 
table.massnlock$percentage3 <- perc.3.lock$percentage3
table.massnlock$percentage4 <- perc.4.lock$percentage4 
table.massnlock$percentage5 <- perc.5.lock$percentage5 

### 6.1 Show results in stacked barplots

# change order of factors

table.massnlock$massnLockdown <- factor(table.massnlock$massnLockdown, levels=c("Gehen viel zu weit", "Gehen eher zu weit", "Sind angemessen", "Gehen eher zu wenig weit","Gehen viel zu wenig weit"))

table.massnlock$stadt.land <- factor(table.massnlock$stadt.land, levels=c("grosse Kernstadt", "kleine Kernstadt", "grosse Agglomeration", "kleine Agglomeration","ländlicher Raum"))

# percentage stacked barplot

data2 <- table.massnlock

plot.1.lock <- ggplot(data2, aes(fill=table.massnlock$massnLockdown, y=table.massnlock$percentage1, x=table.massnlock$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "März",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels=function(x) paste0(x,"%")) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 8, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1)) 

plot.2.lock <- ggplot(data2, aes(fill=table.massnlock$massnLockdown, y=table.massnlock$percentage2, x=table.massnlock$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "Schliessung Geschäfte \nim April",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels = scales::percent) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 7.5, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1))

plot.3.lock <- ggplot(data2, aes(fill=table.massnlock$massnLockdown, y=table.massnlock$percentage3, x=table.massnlock$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "Mai",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels=function(x) paste0(x,"%")) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 8, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1))

plot.4.lock <- ggplot(data2, aes(fill=table.massnlock$massnLockdown, y=table.massnlock$percentage4, x=table.massnlock$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "Eingriffe in Wirtschaft \nim Juni",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels = scales::percent) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 7.5, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1))

plot.5.lock <- ggplot(data2, aes(fill=table.massnlock$massnLockdown, y=table.massnlock$percentage5, x=table.massnlock$stadt.land)) + 
    geom_bar(position="fill", stat="identity") +
    labs(title = "Eingriffe in Wirtschaft \nim Oktober",
         fill = "",
       x = "", y = "") +
    scale_fill_brewer(palette = "Set4") +
    scale_y_continuous(labels=function(x) paste0(x,"%")) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 7.5, hjust = 0.5),
    axis.text.x = element_text(angle = 90, vjust = 0.0, hjust=1),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank())

# Get a nice plot

plot.wrap.lock <- ggarrange(plot.1.lock, plot.2.lock, plot.3.lock, plot.4.lock, plot.5.lock, nrow = 1, common.legend = TRUE, legend = "top")
annotate_figure(plot.wrap.lock, top = text_grob("Schliessung von Geschäften und Dienstleistungen", size = 12),
               bottom = text_grob("", face = "italic", size = 7))

# Get the final plot with comparison between June and october for each policy

plot.wrap.final2 <- ggarrange(plot.4.bew, plot.5.bew, plot.4.lock, plot.5.lock, plot.4.lohn, plot.5.lohn, nrow = 1, widths = c(1,0.8,1,0.8,1,0.8), common.legend = TRUE, legend = "top")
plot.wrap.final2 <- annotate_figure(plot.wrap.final2, top = text_grob("Einschätzung politischer Massnahmen zur Eindämmung des Coronavirus", size = 14),
               bottom = text_grob("", face = "italic", size = 7))

plot.wrap.final2

#ggsave(plot.wrap.final2, file = "Akzeptanz.png", width = 9, height = 7)

### 7. Get Plot for Differences

# Create table

table.diff <- perc.4.bew
table.diff <- table.diff[ -c(3) ]
table.diff$percentage.bew.5 <- perc.5.bew$percentage5 
table.diff$percentage.lock.4 <- perc.4.lock$percentage4
table.diff$percentage.lock.5 <- perc.5.lock$percentage5
table.diff$percentage.lohn.4 <- perc.4.lohn$percentage4
table.diff$percentage.lohn.5 <- perc.5.lohn$percentage5

# drop unnecessary rows

table.diff1 <- table.diff %>% filter(!stadt.land=="grosse Agglomeration")
table.diff1 <- table.diff1 %>% filter(!stadt.land=="kleine Agglomeration")
table.diff1 <- table.diff1 %>% filter(!stadt.land=="kleine Kernstadt")

# drop unnecessary answers for lockdown and Bewegung

table.diff1 <- table.diff1 %>% filter(!massnBewegung=="Sind angemessen")
table.diff1 <- table.diff1 %>% filter(!massnBewegung=="Gehen viel zu wenig weit")
table.diff1 <- table.diff1 %>% filter(!massnBewegung=="Gehen eher zu wenig weit")

# Get sums for massnBewegung
sum.bew.4l <-sum(subset(table.diff1, stadt.land == "ländlicher Raum")$percentage4)
sum.bew.4s <-sum(subset(table.diff1, stadt.land == "grosse Kernstadt")$percentage4)
sum.bew.5l <-sum(subset(table.diff1, stadt.land == "ländlicher Raum")$percentage.bew.5)
sum.bew.5s <-sum(subset(table.diff1, stadt.land == "grosse Kernstadt")$percentage.bew.5)

# Get sums for massnLockdwon
sum.lock.4l <-sum(subset(table.diff1, stadt.land == "ländlicher Raum")$percentage.lock.4)
sum.lock.4s <-sum(subset(table.diff1, stadt.land == "grosse Kernstadt")$percentage.lock.4)
sum.lock.5l <-sum(subset(table.diff1, stadt.land == "ländlicher Raum")$percentage.lock.5)
sum.lock.5s <-sum(subset(table.diff1, stadt.land == "grosse Kernstadt")$percentage.lock.5)

# create a dataframe
answer <- c(rep("Einschränkung Bewegungsfreiheit \nim Juni" , 2) , rep("Einschränkung Bewegungsfreiheit \nim Oktober" , 2) , rep("Eingriffe in Wirtschaft \nim Juni" , 2) , rep("Eingriffe in Wirtschaft \nim Oktober" , 2) )
stadtland <- rep(c("grosse Kernstadt" , "ländlicher Raum") , 4)
value <- c(sum.bew.4s, sum.bew.4l, sum.bew.5s, sum.bew.5l, sum.lock.4s, sum.lock.4l, sum.lock.5s, sum.lock.5l)
data4 <- data.frame(answer,stadtland,value)

# Grouped plot

data4$answer <- factor(data4$answer, levels = c("Eingriffe in Wirtschaft \nim Oktober", "Eingriffe in Wirtschaft \nim Juni", "Einschränkung Bewegungsfreiheit \nim Oktober", "Einschränkung Bewegungsfreiheit \nim Juni"))

plot.diff <- ggplot(data4, aes(fill=stadtland, y=value, x=answer)) + 
    geom_bar(position="dodge", stat="identity") +
    scale_y_continuous(labels=function(x) paste0(x,"%")) +
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    legend.title = element_blank(),
    axis.ticks.y = element_blank(),
    axis.line.x = element_line()) +
  scale_fill_brewer(palette = "Set4") +
  geom_text(x= .8, y = 4, label= round(data4[7,3], digits = 0), color="grey34") +
  geom_text(x= 1.2, y= 4, label= round(data4[8,3], digits = 0), color="grey34") +
  geom_text(x= 1.8, y= 4, label= round(data4[5,3], digits = 0), color="grey34") +
  geom_text(x= 2.2, y= 4, label= round(data4[6,3], digits = 0), color="grey34") +
  geom_text(x= 2.8, y= 4, label= round(data4[3,3], digits = 0), color="grey34") +
  geom_text(x= 3.2, y= 4, label= round(data4[4,3], digits = 0), color="grey34") +
  geom_text(x= 3.8, y= 4, label= round(data4[1,3], digits = 0), color="grey34") +
  geom_text(x= 4.2, y= 4, label= round(data4[2,3], digits = 0), color="grey34") +
  ylab("Bevölkerungsanteil, dem die Massnahmen zu weit gehen") +
  xlab("") +
  coord_flip()

plot.diff <- annotate_figure(plot.diff, top = text_grob("Corona-Massnahmen werden im ländlichen Raum kritischer gesehen", size = 14), bottom = text_grob("", face = "italic", size = 7))

plot.diff

#ggsave(plot.diff, file = "Different.png", width = 9, height = 7)


### 8. Get means of "Vertrauen in Bundesrat"

## remove blanks in varaible massnBewegung

corona.merged.br <- corona.merged.sel %>% filter(!vertrauen=="")

corona.merged.br <- corona.merged.br %>%
  mutate(vertrauen = as.numeric(str_extract(vertrauen, "[0-9]+")))

# Divide dataset into five waves

corona.1.br <- corona.merged.br %>%
  filter(Welle == "1")

corona.2.br <- corona.merged.br %>%
  filter(Welle == "2")

corona.3.br <- corona.merged.br %>%
  filter(Welle == "3")

corona.4.br <- corona.merged.br %>%
  filter(Welle == "4")

corona.5.br <- corona.merged.br %>%
  filter(Welle == "5")

## Welle 1 vertrauen

# get means

table.1.br <- corona.1.br %>%
  group_by(stadt.land) %>%
  mutate(mean1 = weighted.mean(vertrauen, weight = weight)) %>% 
  select(stadt.land, mean1) %>%
  distinct()

## Welle 2

table.2.br <- corona.2.br %>%
  group_by(stadt.land) %>%
  mutate(mean2 = weighted.mean(vertrauen, weight = weight)) %>% 
  select(stadt.land, mean2) %>%
  distinct()

## Welle 3

table.3.br <- corona.3.br %>%
  group_by(stadt.land) %>%
  mutate(mean3 = weighted.mean(vertrauen, weight = weight)) %>% 
  select(stadt.land, mean3) %>%
  distinct()

## Welle 4

table.4.br <- corona.4.br %>%
  group_by(stadt.land) %>%
  mutate(mean4 = weighted.mean(vertrauen, weight = weight)) %>% 
  select(stadt.land, mean4) %>%
  distinct()

## Welle 5

table.5.br <- corona.5.br %>%
  group_by(stadt.land) %>%
  mutate(mean5 = weighted.mean(vertrauen, weight = weight)) %>% 
  select(stadt.land, mean5) %>%
  distinct()

# create one table for all observations

table.br <- table.1.br
table.br$mean2 <- table.2.br$mean2
table.br$mean3 <- table.3.br$mean3
table.br$mean4 <- table.4.br$mean4 
table.br$mean5 <- table.5.br$mean5

# Compare deutschschweiz and romandie

# Divide original dataset into five waves

corona.1.br1 <- corona.backup %>%
  filter(Welle == "1")

corona.2.br1 <- corona.backup %>%
  filter(Welle == "2")

corona.3.br1 <- corona.backup %>%
  filter(Welle == "3")

corona.4.br1 <- corona.backup %>%
  filter(Welle == "4")

corona.5.br1 <- corona.backup %>%
  filter(Welle == "5")

# Means Welle 1

romandie.1.br <- corona.1.br1%>%
  filter(kanton %in% c("Genf", "Wallis", "Freiburg","Waadt", "Neuenburg", "Jura"))%>%
  mutate(vertrauen = as.numeric(str_extract(vertrauen, "[0-9]+")))%>%
  mutate(vertrauen = weighted.mean(vertrauen, na.rm=T, weight = weight))%>%
  select(kanton, vertrauen)

deutsch.1.br <- corona.1.br1%>%
  filter(!kanton %in% c("Genf", "Wallis", "Freiburg","Waadt", "Neuenburg", "Jura", "Tessin"))%>%
  mutate(vertrauen = as.numeric(str_extract(vertrauen, "[0-9]+")))%>%
  mutate(vertrauen = weighted.mean(vertrauen, na.rm=T, weight = weight))%>%
  select(kanton, vertrauen)

# Means Welle 2

romandie.2.br <- corona.2.br1%>%
  filter(kanton %in% c("Genf", "Wallis", "Freiburg","Waadt", "Neuenburg", "Jura"))%>%
  mutate(vertrauen = as.numeric(str_extract(vertrauen, "[0-9]+")))%>%
  mutate(vertrauen = weighted.mean(vertrauen, na.rm=T, weight = weight))%>%
  select(kanton, vertrauen)

deutsch.2.br <- corona.2.br1 %>%
  filter(!kanton %in% c("Genf", "Wallis", "Freiburg","Waadt", "Neuenburg", "Jura", "Tessin"))%>%
  mutate(vertrauen = as.numeric(str_extract(vertrauen, "[0-9]+")))%>%
  mutate(vertrauen = weighted.mean(vertrauen, na.rm=T, weight = weight))%>%
  select(kanton, vertrauen)

# Means Welle 3

romandie.3.br <- corona.3.br1 %>%
  filter(kanton %in% c("Genf", "Wallis", "Freiburg","Waadt", "Neuenburg", "Jura"))%>%
  mutate(vertrauen = as.numeric(str_extract(vertrauen, "[0-9]+")))%>%
  mutate(vertrauen = weighted.mean(vertrauen, na.rm=T, weight = weight))%>%
  select(kanton, vertrauen)

deutsch.3.br <- corona.3.br1 %>%
  filter(!kanton %in% c("Genf", "Wallis", "Freiburg","Waadt", "Neuenburg", "Jura", "Tessin"))%>%
  mutate(vertrauen = as.numeric(str_extract(vertrauen, "[0-9]+")))%>%
  mutate(vertrauen = weighted.mean(vertrauen, na.rm=T, weight = weight))%>%
  select(kanton, vertrauen)

# Means Welle 4

romandie.4.br <- corona.4.br1 %>%
  filter(kanton %in% c("Genf", "Wallis", "Freiburg","Waadt", "Neuenburg", "Jura"))%>%
  mutate(vertrauen = as.numeric(str_extract(vertrauen, "[0-9]+")))%>%
  mutate(vertrauen = weighted.mean(vertrauen, na.rm=T, weight = weight))%>%
  select(kanton, vertrauen)

deutsch.4.br <- corona.4.br1 %>%
  filter(!kanton %in% c("Genf", "Wallis", "Freiburg","Waadt", "Neuenburg", "Jura", "Tessin"))%>%
  mutate(vertrauen = as.numeric(str_extract(vertrauen, "[0-9]+")))%>%
  mutate(vertrauen = weighted.mean(vertrauen, na.rm=T, weight = weight))%>%
  select(kanton, vertrauen)

# Means Welle 5

romandie.5.br <- corona.5.br1 %>%
  filter(kanton %in% c("Genf", "Wallis", "Freiburg","Waadt", "Neuenburg", "Jura"))%>%
  mutate(vertrauen = as.numeric(str_extract(vertrauen, "[0-9]+")))%>%
  mutate(vertrauen = weighted.mean(vertrauen, na.rm=T, weight = weight))%>%
  select(kanton, vertrauen)

deutsch.5.br <- corona.5.br1 %>%
  filter(!kanton %in% c("Genf", "Wallis", "Freiburg","Waadt", "Neuenburg", "Jura", "Tessin"))%>%
  mutate(vertrauen = as.numeric(str_extract(vertrauen, "[0-9]+")))%>%
  mutate(vertrauen = weighted.mean(vertrauen, na.rm=T, weight = weight))%>%
  select(kanton, vertrauen)

# Add to dataframe

# romandie

romandie <- data.frame("Romandie", unique(romandie.1.br$vertrauen), unique(romandie.2.br$vertrauen), unique(romandie.3.br$vertrauen),
                       unique(romandie.4.br$vertrauen), unique(romandie.5.br$vertrauen))

names(romandie) <- c("stadt.land", "mean1", "mean2","mean3","mean4","mean5")

table.br1 <- rbind(table.br, romandie)

# deutschweiz

deutsch <- data.frame("Deutschschweiz", unique(deutsch.1.br$vertrauen), unique(deutsch.2.br$vertrauen), unique(deutsch.3.br$vertrauen),
                       unique(deutsch.4.br$vertrauen), unique(deutsch.5.br$vertrauen))

names(deutsch) <- c("stadt.land", "mean1", "mean2","mean3","mean4","mean5")

table.br2 <- rbind(table.br1, deutsch)

# get only relevant rows

data5 <- table.br2[-c(1, 2, 3), ]
data5 <- rename(data5, "März" = "mean1", "April" = "mean2", "Mai" = "mean3", "Juni" = "mean4", "Oktober" = "mean5")
data6 <- melt(data5, id.vars = "stadt.land", measure.vars = c("März", "April", "Mai", "Juni", "Oktober"))

data6$stadt.land <- factor(data6$stadt.land, levels = c("grosse Kernstadt", "ländlicher Raum",
                                                        "Deutschschweiz", "Romandie"))

plot.br <- ggplot(data6, aes(x=variable, y=value)) + 
  geom_line(aes(group=stadt.land, color = stadt.land), size = 1) + 
  geom_point(aes(color=stadt.land)) +
  coord_cartesian(ylim = c(2.5, 4.5)) +
  labs(title = "Ob Stadt oder Land: Das Vertrauen in den Bundesrat sinkt", 
       subtitle = "")+
  ylab("Vertrauen in Bundesrat: 1 = sehr klein bis 5 = sehr gross")+
  xlab("")+ 
  scale_color_manual(values = c("#FE9A2E", "#04B404", "#08088A", "#8A084B"))+
  theme(legend.title = element_blank(),
        axis.text.x = element_text(angle = 60, vjust = 0.5, face = "bold"),
        panel.background = element_rect(fill="white"),
        panel.grid.minor.y = element_line(size=1),
        panel.grid.major = element_line(colour = "grey"),
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_blank(),
        plot.title = element_text(size=12))
  
plot.br

#ggsave(plot.br, file = "vertrauen.png", width = 7, height = 7)