---
title: "R code for Figure 1"
author: "Yoshikuni Ono"
date: "2020/8/20"
output: 
  html_document:
    toc: true
    toc_depth: 3
    toc_float: true
---

```
## My Session info ##
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
  ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/C/ja_JP.UTF-8/ja_JP.UTF-8

attached base packages:
  stats     graphics  grDevices utils     datasets  methods  
  base     

other attached packages:
  ggstance_0.3.4  forcats_0.5.0   stringr_1.4.0  
  dplyr_1.0.2     purrr_0.3.4     readr_1.3.1    
  tidyr_1.1.2     tibble_3.0.3    ggplot2_3.3.2  
  tidyverse_1.3.0
```

#install any necessary packages, inserting package name as appropriate
install.packages("packagename")


## Initial settings

```{r}
rm(list = ls())
library(tidyverse)
library(ggstance)
options(stringsAsFactors = FALSE)
```


## Read data
## We created Fig1_data.csv based on the outcomes produced on Stata (See "Ono Zilis Study1_AJPS.do")

```{r}
all = read.csv("Fig1_data.csv", 
                header = TRUE, 
                check.name = FALSE, 
                stringsAsFactors = TRUE)
```


## Save Colour Scheme

```{r}
g <- ggplot(all, aes(y = est, x = order, colour = Attribute))

c <- ggplot_build(g)$data[[1]]["colour"] %>% distinct(colour) 
  
default_colour_palett  <- c$colour
default_colour_palett2 <- c(default_colour_palett, "lightgrey", "black")
```


## Function for customized theme

```{r}
mytheme <- function(base_size = 13,
                    base_family = "", 
                    legend_position = "none") { 
    theme_grey(base_size = base_size, 
               base_family = base_family) %+replace%
        theme(axis.text.x =  element_text(size = base_size,
                                          colour = "black",
                                          hjust = .5 , vjust = 1),
              axis.text.y =  element_text(size = base_size, 
                                          colour = "black", hjust = 0,
                                          vjust = 0.5),
              axis.ticks =   element_line(colour = "grey50"),
              axis.title.y = element_text(size = base_size,
                                          angle = 90, vjust = .01, hjust = .1),
              legend.position = legend_position)
}
```


## Make a chart for the main results using all respondents

```{r}
p <- all %>% 
    mutate(
        Judge = if_else(Attribute == "Partisanship_female", 
                        "Hispanic Judge", "Female Judge"),
        Judge = fct_inorder(Judge),
        Level = fct_inorder(Level)   
    ) %>% 
    filter(!`var.names` %in% c("Hispanic Judge", "Female Judge")) %>% 
    ggplot(aes(y = Level, x = est, shape = Judge, color = Judge,
               xmin = est - 1.96 * se, xmax = est + 1.96 * se)) +
    geom_vline(xintercept = 0, size = .5,
               colour = "darkgrey", linetype = "solid") +
    geom_pointrangeh(position = position_dodgev(height = .75), size = .75) +
    labs(x = "Estimated Marginal Effects", y = NULL) +
    xlim(-.5, .5) +
    scale_colour_manual(values = c(`Female Judge` = "black",
                                  `Hispanic Judge` = "grey60"),
                        guide = guide_legend(reverse = TRUE)) +
    scale_shape_manual(values = c(`Female Judge` = 15,
                                  `Hispanic Judge` = 17),
                       guide = guide_legend(reverse = TRUE))

p + mytheme(base_size = 11, legend_position = "bottom")

ggsave("Figure1.png", width = 6, height = 2.8)
```


