-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_elections.R
140 lines (126 loc) · 4.54 KB
/
map_elections.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
cat("\n---- START ----\n")
# Packages required
library(magrittr)
library(htmlwidgets)
library(htmltools)
library(leaflet)
library(countrycode)
library(rgdal)
library(stringdist)
# Importing the results
df_score <- read.csv2("data/Tour_1_Resultats_par_pays_240417.csv",stringsAsFactors=FALSE)
for (i in c(2:13)) {
# Merging Jerusalem and Israel
df_score[71,i] <- df_score[71,i] + df_score[74,i]
# Merging Serbia and Kosovo
df_score[121,i] <- df_score[121,i] + df_score[78,i]
}
df_score <- df_score[-c(74,78), ]
# extract votes and rename candiates (better to use votes and calcule % afterwards)
df_score <- df_score[, c(1:13)]
names(df_score)[2:ncol(df_score)] <- c(
"voteTot",
"M. Nicolas DUPONT-AIGNAN",
"Mme. Marine LE PEN",
"M. Emmanuel MACRON",
"M. Benoît HAMON",
"Mme. Nathalie ARTHAUD",
"M. Philippe POUTOU",
"M. Jacques CHEMINADE",
"M. Jean LASSALLE",
"M. Jean Luc MÉLENCHON",
"M. François ASSELINEAU",
"M. François FILLON")
# Importing the shapefile
wrld_adm <- readOGR(dsn="data", layer="worldsimple")
## RENAME some countries to ease the match...
iso3cm <- iso3c <- read.csv("./data/iso3-fr.csv", header=FALSE, stringsAsFactors=F)
iso3cm$V5[c(40, 51, 58, 62, 105, 106, 117, 120, 125, 142, 182, 211, 230, 240)] <- c(
"CENTRAFRICAINE (RÉPUBLIQUE)", "CONGO", "TCHÈQUE (RÉPUBLIQUE)", "DOMINICAINE (RÉPUBLIQUE)",
"IRAN", "IRAK", "CORÉE DU SUD", "LAOS", "LIBYE", "MOLDAVIE", "RUSSIE",
"SYRIE", "TANZANIE", "SERBIE")
## SERBIE SYRIE TCHEQ EQUATEUR
df_score$ISO3 <- ""
for(r in 1:nrow(df_score)){
df_score$ISO3[r] <- as.character(iso3c[stringdist::amatch(df_score$Pays[r],
toupper(iso3cm$V5), maxDist=20), 'V4'])
}
## Seems like there is an issue with Serbia
df_score$ISO3[df_score$ISO3=="SCG"] <- "SRB"
## checks
# length(unique(df_score$ISO3))
# table(df_score$ISO3)
# df_score[c('ISO3','Pays')]
# wrld_adm@data
wrld_df <- data.frame(
ISO3 = as.character(wrld_adm@data$ISO3),
Pays = as.character(wrld_adm@data$FRENCH),
stringsAsFactors = FALSE
)
tmp <- merge(x =wrld_df, y = df_score, by = "ISO3",
all = TRUE, sort=TRUE)
rownames(tmp) <- NULL
tmp <- tmp[rank(wrld_df$ISO3),]
tmp[is.na(tmp)] <- 0
wrld_adm@data <- tmp
## Remove France
idf <- which(wrld_adm@data$ISO3=="FRA")
wrld_adm <- wrld_adm[-idf,]
idomtom <- which(wrld_adm@data$Pays.x %in% c("Guadeloupe","Martinique","Guyanne Française","Ile de la Réunion","Saint-Pierre-et-Miquelon"))
wrld_adm <- wrld_adm[-idomtom,]
## LABELS
cat("\n---- CREATING LABELS ----\n")
nbc <- nrow(wrld_adm@data)
ls_labels <- rep("", nbc)
vec_pos <- rep(0, nbc)
for (i in 1:nbc){
tot <- wrld_adm@data$voteTot[i]
ls_labels[i] %<>% paste0("<h2 style='margin:5px 0px 5px 0px;padding:0px 0px 0px 0px;font-weight:bold;'>", wrld_adm@data$Pays.x[i],
"</h2>")
if (tot) {
rk <- rank(tot-wrld_adm@data[i, 5:15], ties.method = "min")
rk2 <- rank(tot-wrld_adm@data[i, 5:15], ties.method = "random")
vec_pos[i] <- which(rk2 == 1)
ls_labels[i] %<>% paste0("Nombre de votes : ", tot)
for (j in 1:11){
id <- which(rk2==j)
vot <- wrld_adm@data[i, id+4]
pct <- round(100*vot/wrld_adm@data$voteTot[i], 2)
ls_labels[i] %<>% paste0(
"<br>", j, ". ", names(wrld_adm@data)[id+4],
" : ", vot, " soit ", pct, "%"
)
}
}
}
ls_labels %<>% lapply(htmltools::HTML)
## COLORS (only 4 winners...)
vec_col <- c("white", "#232f70", "#232f70", "#c9a00e","#97c121", "#c9462c", "#c9462c",
"#232f70", "#c9462c", "#c9462c", "#232f70", "#01b2fb")
ls_col <- vec_col[vec_pos+1] %>% as.list
## I decided to remove France (makes clearer that we do not include it and we
## we can see Monaco!)
## Creating the Map using leaflet;
cat("\n---- CREATING THE MAP ----\n")
map_elec <- leaflet(wrld_adm) %>%
setView(lng = 5, lat = 10, zoom = 3) %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addPolygons(
weight = 1,
opacity = 1,
color = ls_col,
fillOpacity = 0.8,
highlight = highlightOptions(
weight = 1,
color = "#ffffff",
fillOpacity = 0.8,
bringToFront = TRUE),
label = ls_labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "10px 10px 10px 10px",opacity=1),
textsize = "13px", direction = "auto")) %>%
addProviderTiles("Esri.WorldImagery") %>%
addLegend("bottomright",title = "Candidats",colors = c("#c9a00e","#01b2fb","#c9462c","#97c121","white"), label = c("M. Emmanuel Macron","M. François Fillon","M. Jean-Luc Mélanchon","M. Benoit Hamon","Aucun ressortissants"), opacity = 1)
##
cat("\n---- SAVING THE MAP ----\n")
saveWidget(widget = map_elec, file = "index.html")