| --- |
| title: "Figure 9" |
| output: pdf_document |
| --- |
| |
| ```{r setup, include=FALSE} |
| knitr::opts_chunk$set(echo = F, warning = F, message = F) |
| ``` |
| |
| ```{r, include = F, echo = F} |
| # Importing Packages |
| rm(list=ls()) |
| gc() |
| library(tidyverse) |
| library(extrafont) |
| library(gtable) |
| library(ggplot2) |
| library(gridExtra) |
| library(grid) |
| library(cowplot) |
| library(grDevices) |
| loadfonts() |
| ``` |
| |
| ```{r} |
| # SPECIFY DIRECTORY OF THE REPLICATION PACKAGE |
| setwd("C:/Users/Francesco/Dropbox/AMS_Redistribution/Data/Survey_Data/Replication RESTUD sharing") |
| |
| # IMPORTING DATASET |
| datafig_second_gen = read_csv('Out/Figures_data/figure_9_data.csv', show_col_types = FALSE) |
| ``` |
| |
| ```{r} |
| # Function to Export Figures: SPECIFY DIRECTORY OF THE REPLICATION PACKAGE |
| |
| FigureExporter = function(Figure, Name, WhereFolderDraft = "C:/Users/Francesco/Dropbox/AMS_Redistribution/Data/Survey_Data/Replication RESTUD sharing"){ |
| figure_name <- Name |
| figures_dir <- file.path(WhereFolderDraft, 'Out/Figures') |
| fig_path <- file.path(figures_dir, figure_name) |
| png(fig_path, width = 4800, height=2700, res = 720, type = c("cairo-png")) |
| Figure1 = Figure |
| print(Figure1) |
| } |
| ``` |
| |
| |
| |
| ```{r} |
| #Data Adjustment for cleaning (Left Panel) Function - Without Rescaling |
| |
| DataAdjustNoResc = function(data){ |
| data = data[data$t %in% c("Sweden", "Germany", "Italy", "France", "UK", "US"), !(names(data) %in% "n")] |
| data$m_perc <- data$m_perc |
| data$m_real <- data$m_real |
| data <- transform(data, |
| lbound = lb_perc, |
| ubound = ub_perc) |
| |
| diff <- data %>% |
| mutate(Max = max(m_perc, m_real), |
| Min = min(m_perc, m_real), |
| Diff = Max / Min - 1) %>% |
| arrange(desc(Diff)) |
| |
| ## Create difference between perception and real |
| data$diff <- (data$m_perc - data$m_real)/data$m_real |
| data_adj = data |
| data_adj |
| } |
| ``` |
| |
| |
| #Figure 9 |
| |
| ```{r} |
| # Data adjustment: rescaling not needed |
| datafig_second_gen_adj = DataAdjustNoResc(datafig_second_gen) |
| |
| # Function to generate the graph |
| Left_10_B = function(xlb, xupb, distance_ticks){ |
| data = datafig_second_gen_adj |
| |
| p <- ggplot(data, aes(x=t)) + |
| ## Plot segment joining and points |
| geom_segment(aes(x=t, xend=t, y=m_real, yend=m_perc), size = 0.4, color="black") + |
| geom_segment(aes(x=t, xend=t, y=lbound, yend=ubound), size = 2, color = rgb(0.7,0.2,0.1,0.3)) + |
| geom_point(aes(x=t, y=m_real_2, colour= 'Act. 1 & 2 Gen.', shape = 'Act. 1 & 2 Gen.'), size=4) + |
| geom_point(aes(x=t, y=m_real, colour='Act. 1 Gen.', shape = 'Act. 1 Gen.'), size=4) + |
| geom_point(aes(x=t, y=m_perc, colour='Perceived (mean)', shape = 'Perceived (mean)'), size=3) + |
| coord_flip() |
| |
| ## Add theme, labels, and modify scale |
| myshapes <- c("my_b" = "15", "my_d" = "18") |
| p1 <- p + |
| scale_y_continuous(limits = c(xlb, xupb), breaks = seq(xlb, xupb, by = distance_ticks)) + |
| theme_light() + |
| theme( |
| ## Control grid |
| panel.grid.major = element_line(linetype = 'dashed', color = 'darkgrey'), |
| panel.grid.major.y = element_line(linetype = 'solid', color = 'darkgrey'), |
| |
| ## Control legend |
| legend.position = "bottom", |
| legend.box = "horizontal", |
| legend.justification = c(0.5,0), |
| legend.title = element_blank(), |
| text = element_text(family = "LM Roman 10", color='black', size=11), |
| axis.text = element_text(colour = 'black'), |
| axis.text.x = element_text(size=9), |
| axis.text.y = element_text(size=9), |
| axis.title.x = element_text(size = 9, margin = margin(t=15)), |
| axis.ticks.x = element_blank() |
| ) + |
| scale_colour_manual(name = "", labels = c( "Act. 1 & 2 Gen.", 'Act. 1 Gen.','Perceived (mean)'), |
| values = c("Act. 1 & 2 Gen." = "#1A476F", 'Act. 1 Gen.' = "#0080FF", |
| 'Perceived (mean)' = rgb(0.7,0.2,0.1,1))) + |
| scale_shape_manual(name = "", labels = c("Act. 1 & 2 Gen.",'Act. 1 Gen.', 'Perceived (mean)'), |
| values = c( "Act. 1 & 2 Gen." = 20, 'Act. 1 Gen.' = 18, 'Perceived (mean)' = 15)) + |
| xlab("") + |
| ylab("Share of Immigrants") + |
| guides(col = guide_legend(nrow=2,byrow=TRUE)) |
| p1} |
| ``` |
| |
| ```{r} |
| # Generate Figure |
| plot_grid(Left_10_B(0, 40, 5), labels=NULL, ncol=2, align='h', axis='b') |
| |
| # Export |
| FigureExporter(plot_grid(Left_10_B(0, 40, 5), labels=NULL, ncol=2, align='h', axis='b'), "Figure_9.png") |
| |
| ``` |
| |