##################################################
################ PREPARATION #####################
##################################################
# Clear environment
rm(list=setdiff(ls(), "path_to"))

# Load data
world = readRDS(path_to("world"))

# Settings
settings = list(
  "biregcoeff_coop_belief" = list("A: Cooperation belief", c(-0.2, 1, 0.2), "From 0% to 100%"),
  "biregcoeff_norm" = list("B: Approval of social norm", c(-0.2, 1, 0.2), "Binary (0/1)"),
  "biregcoeff_pg_preference" = list("C: Preference for common good      ", c(-0.2, 1, 0.2), "Binary (0/1)"),
  "biregcoeff_altruism_std" = list("D: Altruism (std.)", c(-0.2, 0.2, 0.1), "Standardized"),
  "biregcoeff_patience_std" = list("E: Patience (std.)", c(-0.2, 0.2, 0.1), "Standardized"),
  "biregcoeff_risktaking_std" = list("F: Risk-taking (std.)", c(-0.2, 0.2, 0.1), "Standardized")
)

# pval colors
pval = c(
  "p < 0.01" = "#90353b",
  "p < 0.05" = "red2",
  "p < 0.10" = "salmon1",
  "Not significant" = "bisque2"
)


##################################################
################ PLOT  ##########################
##################################################
plots = subplots = list()
vars = names(settings)


# Plot!
for (v in vars) {
  # Extract data
  rel_col = c("iso3", "country", colnames(world)[grepl(paste0("^", gsub("coeff", ".*", v)), colnames(world))])
  df = world[, rel_col]
  colnames(df) = gsub("bireg|reg|_.*", "", colnames(df))
  
  # Order countries
  df = df[order(df$coeff), ]
  df = df[!is.na(df$coeff),]
  
  # Signficiance levels
  df$sig = ifelse(df$p < 0.01, "p < 0.01",
           ifelse(df$p < 0.05, "p < 0.05",
           ifelse(df$p < 0.1, "p < 0.10", "Not significant")))
  df$sig = factor(df$sig, levels=names(pval))
  
  # Plots
  subplots[[v]] = ggplot(df, aes(x = iso3, y = coeff, fill=sig)) +
    geom_hline(yintercept = 0) +
    geom_bar(stat="identity", width=.8) + 
    #geom_text(aes(y=coeff*1.04), size=2.5, vjust=0.75) +
    scale_x_discrete(limits = df$iso3) +
    scale_fill_manual(values=pval) +
    scale_y_continuous(breaks=seq(settings[[v]][[2]][1], settings[[v]][[2]][2], settings[[v]][[2]][3]), labels=paste0(seq(settings[[v]][[2]][1], settings[[v]][[2]][2], settings[[v]][[2]][3])*100)) +
    coord_cartesian(ylim=(c(settings[[v]][[2]][1], settings[[v]][[2]][2]) + c(-0.01, 0.01))) +
    labs(title = settings[[v]][[1]], subtitle = settings[[v]][[3]]) +
    xlab("Countries (ordered)") + ylab("Effect on coop. rate (in pp)") +
    theme_minimal() +
    theme(
      #text = element_text(family="rob_con"),
      plot.title = element_text(size=14, hjust=0.5, face="bold"),
      plot.subtitle = element_text(size=11, hjust=0.5),
      axis.title.x = element_text(size=11),
      axis.title.y = element_text(size=11, face="bold"),
      axis.text.x = element_blank(),#element_text(size=6, angle=90, hjust=1, vjust=0.5),
      axis.text.y = element_text(size=11),
      axis.ticks = element_blank(),
      panel.border = element_blank(),
      panel.grid.major.x = element_blank(),
      panel.grid.minor.x = element_blank(),
      legend.title=element_blank(),
      legend.text=element_text(size=11),
      legend.position = "bottom"
    )
}

plots$fig_2 = ggarrange(
  plotlist = subplots,
  common.legend=T, legend="bottom", ncol=3, nrow = 2
)



##################################################
################ PLOT ############################
##################################################
n = "fig_2"
pdf(paste0(path_to("figures"), n, ".pdf"), width = 10, height=6)
print(plots[[n]])
dev.off() 

