---
title: "Report III."
author: "František Kalvas"
date: "Compiled: `r format(Sys.time(), '%d. %m. %Y')`"
output: 
  html_document:
    toc: true
    toc_float: true
    toc_depth: 4
    code_folding: hide
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

## FrK 2020-06-23 Vytvořil
## FrK 2021-05-05 Upravil

# Hlavička
rm(list = ls())
wd = getwd()

# Načtení packagí
library(readxl)
library(dplyr)
library(ggplot2)
library(tidyr)
library(readr)
library(stringr)
library(forcats)
```

## Loading data and their transformation



```{r loading}
preX = read_xlsx("dk data.xlsx")
names(preX)[1:2] = c("id", "alGov")
pre = preX %>% select(-alGov) %>% 
  pivot_longer(cols = market:architecture, names_to = "Governance") %>% 
  filter(value > 0) 
pre$Phase = "Pre (trust)"

post = read_xlsx("dk data.xlsx", sheet = 2)
names(post)[c(1, 3)] = c("id", "applications")
post = post %>% pivot_longer(cols = architecture:government, names_to = "Governance") %>% 
  filter(value > 0) %>% mutate(value = ((6 - value) * 9 / 5) + 1)
post$Phase = "Post (priority)"

df = rbind(pre, post) %>% 
  mutate(Phase = factor(Phase, levels = c("Pre (trust)", "Post (priority)"))) %>% 
  left_join(., preX[, 1:2], by = "id") %>% 
  mutate(alGov = case_when(
    alGov == 0 ~ "Opposes AlGov", 
    alGov == 1 ~ "Supports AlGov")) %>% filter(!is.na(alGov)) %>% 
  pivot_wider(id_cols = c("id", "Governance", "Phase", "alGov"),
              names_from = c("Governance", "Phase"),
              names_sep = "_",
              values_from = value)

dfr = df[rowSums(is.na(df[, 9:14]))< 6, ]  # DFR = data.frame reduced

df = df %>% 
  pivot_longer(cols = 3:14,
               names_to = c("Governance", "Phase"),
               names_sep = "_") %>% 
  mutate(Phase = factor(Phase, levels = c("Pre (trust)", "Post (priority)")),
         Governance = str_to_title(Governance)) %>% 
  filter(!is.na(value))

dfr = dfr %>% 
  pivot_longer(cols = 3:14,
               names_to = c("Governance", "Phase"),
               names_sep = "_") %>% 
  mutate(Phase = factor(Phase, levels = c("Pre (trust)", "Post (priority)")),
         Governance = str_to_title(Governance)) %>% 
  mutate(value = case_when(
    is.na(value) & Phase == "Post (priority)" ~ 0,
    TRUE ~ value
  )) %>% 
  filter(!is.na(value))


```

## Plots on full sample  

Only repondents refusing to state their support/opposition to AlGov were filtered out:   

```{r plots1, message = F, warning = F}
df %>% 
  ggplot(aes(x = Governance, y = value, fill = Phase, color = Phase)) +
  facet_grid(rows = vars(alGov)) +
  geom_boxplot(alpha = 0.15) + 
  geom_point(size = 2.5, alpha = 0.35, 
             position = position_jitterdodge(jitter.height = 0.25, 
                                             jitter.width = 0.5,)) +
  labs (
    title = "Comparison of trust (pre-workshop) and priority (post-workshop) of\ngovernance measures according support of AlGov",
    x = "",
    y = "Trust/Priority (1 - min, 10 - max)"
  ) +
  scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  scale_y_continuous(breaks = 1:10)+
  theme_classic() + 
  theme(
    legend.position = "bottom"
  )

df %>% group_by(Governance, Phase, alGov) %>% summarise(value = mean(value, na.rm = T)) %>%  
  pivot_wider(id_cols = c("Governance", "Phase", "alGov"), names_from = Phase) %>% 
  mutate(Size = 1 + 2 * abs(`Pre (trust)` - `Post (priority)`)) %>% 
  pivot_longer(cols = 3:4, names_to = "Phase") %>% 
  ggplot(aes(x = Governance, y = value, fill = Phase, color = Phase, group = Governance, size = Size)) +
  facet_grid(rows = vars(alGov)) +
  geom_point(alpha = 0.5, position = position_dodge2(width = .5)) +
  geom_line(size = 1, color = "#808080", position = position_dodge2(width = .5)) +
  labs(title = "Comparison of trust (pre-workshop) and priority (post-workshop) of\ngovernance measures according support of AlGov",
       y = " Average trust/priority (1 - min, 10 - max)",
       caption = "Size of points reflects the difference between pre and post assessment") +
  guides(size = "none") +
  scale_y_continuous(breaks = 1:10, limits = c(1, 10))+
  scale_size_identity() +
  theme_classic() + 
    theme(
    legend.position = "bottom"
  )



```


## Plots on reduced sample  

Repondents refusing to state their support/opposition to AlGov were filtered out, but we also filtered out respondents refusing state their priorities regarding governance measures:   

```{r plots2, message = F, warning = F}
dfr %>% 
  ggplot(aes(x = Governance, y = value, fill = Phase, color = Phase)) +
  facet_grid(rows = vars(alGov)) +
  geom_boxplot(alpha = 0.15) + 
  geom_point(size = 2.5, alpha = 0.35, 
             position = position_jitterdodge(jitter.height = 0.25, 
                                             jitter.width = 0.5,)) +
  labs (
    title = "Comparison of trust (pre-workshop) and priority (post-workshop) of\ngovernance measures according support of AlGov",
    subtitle = "Reduced sample",
    x = "",
    y = "Trust/Priority (1 - min, 10 - max)"
  ) +
  scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  scale_y_continuous(breaks = 1:10)+
  theme_classic() + 
  theme(
    legend.position = "bottom"
  )

dfr %>% group_by(Governance, Phase, alGov) %>% summarise(value = mean(value, na.rm = T)) %>%  
  ggplot(aes(x = Governance, y = value, fill = Phase, color = Phase, group = Governance)) +
  facet_grid(rows = vars(alGov)) +
  geom_point(size = 5, alpha = 0.5, position = position_dodge2(width = .5)) +
  geom_line(color = "#808080", position = position_dodge2(width = .5)) +
  labs(title = "Comparison of trust (pre-workshop) and priority (post-workshop) of\ngovernance measures by support of AlGov",
       subtitle = "Reduced sample",
       y = " Average trust/priority (1 - min, 10 - max)") +
  scale_y_continuous(breaks = 1:10, limits = c(1, 10))+
  theme_classic() +
    theme(
    legend.position = "bottom"
  )

dfr %>% group_by(Governance, Phase, alGov) %>% summarise(value = mean(value, na.rm = T)) %>%  
  ggplot(aes(x = Governance, y = value, fill = alGov, color = alGov, group = Governance)) +
  facet_grid(rows = vars(Phase)) +
  geom_point(size = 5, alpha = 0.5, position = position_dodge2(width = .5)) +
  geom_line(color = "#808080", position = position_dodge2(width = .5)) +
  labs(title = "Comparison of support of AlGov\n by trust (pre-workshop) and priority (post-workshop) of governance measures",
       subtitle = "Reduced sample",
       y = " Average trust/priority (1 - min, 10 - max)") +
  scale_y_continuous(breaks = 1:10, limits = c(1, 10))+
  theme_classic() +
    theme(
    legend.position = "bottom"
  )
 


```




## Comparison of Plots on reduced and Full Sample  


```{r plots3, message = F, warning = F}
df %>% 
  ggplot(aes(x = Governance, y = value, fill = Phase, color = Phase)) +
  facet_grid(rows = vars(alGov)) +
  geom_boxplot(alpha = 0.15) + 
  geom_point(size = 2.5, alpha = 0.35, 
             position = position_jitterdodge(jitter.height = 0.25, 
                                             jitter.width = 0.5,)) +
  labs (
    title = "Comparison of trust (pre-workshop) and priority (post-workshop) of\ngovernance measures according support of AlGov",
    subtitle = "Full sample",
    x = "",
    y = "Trust/Priority (1 - min, 10 - max)"
  ) +
  scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  scale_y_continuous(breaks = 1:10)+
  theme_classic() + 
  theme(
    legend.position = "bottom"
  )

dfr %>% 
  ggplot(aes(x = Governance, y = value, fill = Phase, color = Phase)) +
  facet_grid(rows = vars(alGov)) +
  geom_boxplot(alpha = 0.15) + 
  geom_point(size = 2.5, alpha = 0.35, 
             position = position_jitterdodge(jitter.height = 0.25, 
                                             jitter.width = 0.5,)) +
  labs (
    title = "Comparison of trust (pre-workshop) and priority (post-workshop) of\ngovernance measures according support of AlGov",
    subtitle = "Reduced sample",
    x = "",
    y = "Trust/Priority (1 - min, 10 - max)"
  ) +
  scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  scale_y_continuous(breaks = 1:10)+
  theme_classic() + 
  theme(
    legend.position = "bottom"
  )

df %>% group_by(Governance, Phase, alGov) %>% summarise(value = mean(value, na.rm = T)) %>%  
  ggplot(aes(x = Governance, y = value, fill = Phase, color = Phase, group = Governance)) +
  facet_grid(rows = vars(alGov)) +
  geom_point(size = 5, alpha = 0.5, position = position_dodge2(width = .5)) +
  geom_line(color = "#808080", position = position_dodge2(width = .5)) +
  labs(title = "Comparison of trust (pre-workshop) and priority (post-workshop) of\ngovernance measures according support of AlGov",
       subtitle = "Full sample",
       y = " Average trust/priority (1 - min, 10 - max)") +
  scale_y_continuous(breaks = 1:10, limits = c(1, 10))+
  theme_classic() +
    theme(
    legend.position = "bottom"
  )


dfr %>% group_by(Governance, Phase, alGov) %>% summarise(value = mean(value, na.rm = T)) %>%  
  ggplot(aes(x = Governance, y = value, fill = Phase, color = Phase, group = Governance)) +
  facet_grid(rows = vars(alGov)) +
  geom_point(size = 5, alpha = 0.5, position = position_dodge2(width = .5)) +
  geom_line(color = "#808080", position = position_dodge2(width = .5)) +
  labs(title = "Comparison of trust (pre-workshop) and priority (post-workshop) of\ngovernance measures according support of AlGov",
       subtitle = "Reduced sample",
       y = " Average trust/priority (1 - min, 10 - max)") +
  scale_y_continuous(breaks = 1:10, limits = c(1, 10))+
  theme_classic() +
    theme(
    legend.position = "bottom"
  )



```





