REPRO-Bench / 27 /replication_package /Figure3_4-28-20.R
anonymous-submission-acl2025's picture
add 27
3af64c0
#replicate figure 3
rm(list=ls())
#SET WORKING DIRECTORY
#devtools::install_github("dkahle/ggmap")
#devtools::install_github("hadley/ggplot2")
# install.packages('ggrepel')
# install.packages('plyr')
# install.packages('dplyr')
# install.packages('RColorBrewer')
# install.packages('reshape2')
library(ggplot2)
library(ggrepel)
library(plyr)
library(dplyr)
library(ggmap)
library(RColorBrewer)
library(reshape2)
s = function(x){summary(factor(x))}
#Read household survey data for mapping
#NOTE: Geolocation data (lat/long) has been offset by a random number to avoid compromising respondent anonymity
C = readRDS('map_dataset_4-28-20.RDS')
#Read map file
#NOTE: Geolocation data (lat/long) has been offset by a random number to avoid compromising respondent anonymity
map_19_cluster_offset = readRDS('map_out_4-28-20.RDS')
C = C %>% rename(Religion = C.C6_Religion.x)
#Create categories from KNN values
C$Cat = NA
C$Cat[which(C$Nearest10_OwnReligion <= 5)] = 0
C$Cat[which(C$Nearest10_OwnReligion > 5 & C$Nearest10_OwnReligion <= 8)] = 1
C$Cat[which(C$Nearest10_OwnReligion > 8)] = 2
C$Cat = factor(C$Cat)
#Switch order of data points for more attractive plot
D = C[seq(dim(C)[1],1),] #
#Set limits of scale bar
sc_x_lo = min(D$pseudo_lon) + (max(D$pseudo_lon) - min(D$pseudo_lon)) * (1/10)
sc_y = min(D$pseudo_lat) + (max(D$pseudo_lat) - min(D$pseudo_lat)) * (9.34/10)
#Create map
#Note again that lat-long values are PSEUDO, ie they have been offset by a random value to preserve respondent anonymity
ggmap(map_19_cluster_offset) + geom_point(data = D, aes(x=pseudo_lon, y=pseudo_lat, shape = Religion, fill=Cat),
size=6.7, alpha=1, color = 'white') +
geom_segment(aes(x=sc_x_lo, xend=sc_x_lo + 0.0005/1.0835,y=sc_y,yend=sc_y),color='white', size = 1.05) + #scale bar +
geom_text(aes(x=sc_x_lo + 0.00065,y=sc_y-0.0000),label='50 m',color='white', size = 6.5) + #scale bar label
theme(axis.line=element_blank(),axis.text.x=element_blank(), #suppress axes
axis.text.y=element_blank(),axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.title = element_text(size = 14),
legend.text = element_text(size = 12)) +
scale_fill_grey('10-nearest-\nneighbors', labels = c('(0,5]','(5,8]','(8,10]')) +
scale_shape_manual('Religion', values = c(24, 21), labels = c('Hindu','Muslim')) +
guides(fill=guide_legend(override.aes=list(shape=21)),
shape=guide_legend(override.aes=list(fill='black')))
ggsave(filename = 'figure3.png',
device = 'png', width = 6, height = 4, units = 'in', dpi = 'print')