-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
388 lines (388 loc) · 35.5 KB
/
.Rhistory
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
pacman::p_load(tidyverse,readxl, plyr, dplyr, ggplot2, tidyr, tidyverse, extrafont, gganimate, gapminder, plotly, shiny, sp, sf, foreign, readr, rgdal, ggrepel, gridExtra, grid, lattice, pdftools, tesseract, rgdal, tmap, raster)
#read legislative elections datasets
congreso<- read_xlsx("CongresosLocalesMR.xlsx")
summary(congreso)
QR16 <- congreso[congreso$id_state== 23 & congreso$election_year==2016,]
QR19 <- congreso[congreso$id_state== 23 & congreso$election_year==2019,]
D21 <- congreso[congreso$id_state== 10 & congreso$election_year==2021,]
D18 <- congreso[congreso$id_state== 10 & congreso$election_year==2018,]
H21 <- congreso[congreso$id_state== 10 & congreso$election_year==2021,]
H18 <- congreso[congreso$id_state== 23 & congreso$election_year==2018,]
congreso$winner<- as.factor(congreso$winner)
congreso$second<- as.factor(congreso$second)
Dtto <- st_read("DISTRITO_LOCAL.shp")
QR <- Dtto[Dtto$ENTIDAD == 23,]
D <- Dtto[Dtto$ENTIDAD == 10,]
H <- Dtto[Dtto$ENTIDAD == 13,]
#congreso 2016
MQR19 <- merge(QR, QR19, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroo19 <- MQR19 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","#DF2630", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo19
mapzoom2 <- MQR19 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#B1191E", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapa2 <- grid.arrange( grobs = list(mapaqroo19, mapzoom2),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa2
MQR16 <- merge(QR, QR16, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroo16 <- MQR16 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","#DF2630", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo16
mapzoom <- MQR16 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#DF2630", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapa <- grid.arrange( grobs = list(mapaqroo16, mapzoom),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa
#Hidalgo
HGO21 <- merge(H, H21, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapah21 <- HGO21 %>% ggplot() + geom_sf(fill= ifelse( HGO21$winner == "PAN", "#001D82", ifelse( HGO21$winner=="PRI","#DF2630", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapah21
HGO18 <- merge(H, H18, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapah18 <- HGO18 %>% ggplot() + geom_sf(fill= ifelse( HGO18$winner=="MORENA","#B1191E","#DF2630"), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapah18
mapa2
MQR19 <- merge(QR, QR19, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroo19 <- MQR19 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","#DF2630", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo19
MQR16 <- merge(QR, QR16, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroo16 <- MQR16 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","#DF2630", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo16
mapaqroo19 <- MQR19 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82","#DF2630")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo19
mapzoom2 <- MQR19 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#DF2630", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapa2 <- grid.arrange( grobs = list(mapaqroo19, mapzoom2),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa2
mapaqroo19 <- MQR19 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82","#DF2630"), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo19
mapa2 <- grid.arrange( grobs = list(mapaqroo19, mapzoom2),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa2
MQR16 <- merge(QR, QR16, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroo16 <- MQR16 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82","#DF2630"), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo16
mapzoom <- MQR16 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#DF2630", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapa <- grid.arrange( grobs = list(mapaqroo16, mapzoom),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa
MQR19 <- merge(QR, QR19, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroo19 <- MQR19 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","#DF2630", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo19
mapzoom2 <- MQR19 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#B1191E", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapa2 <- grid.arrange( grobs = list(mapaqroo19, mapzoom2),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa2
mapaqroo19 <- MQR19 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","red", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapa2 <- grid.arrange( grobs = list(mapaqroo19, mapzoom2),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa2
mapah18
mapah18 <- HGO18 %>% ggplot() + geom_sf(fill= ifelse( HGO18$winner=="MORENA","#B1191E","#DF2630"), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapah18 <- HGO18 %>% ggplot() + geom_sf(fill= ifelse( HGO18$winner=="PRI","#DF2630", "#B1191E"), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapah18
View(mapah18)
pacman::p_load(tidyverse,readxl, plyr, dplyr, ggplot2, tidyr, tidyverse, extrafont, gganimate, gapminder, plotly, shiny, sp, sf, foreign, readr, rgdal, ggrepel, gridExtra, grid, lattice, pdftools, tesseract, rgdal, tmap, raster)
#read legislative elections datasets
congreso<- read_xlsx("CongresosLocalesMR.xlsx")
summary(congreso)
QR16 <- congreso[congreso$id_state== 23 & congreso$election_year==2016,]
QR19 <- congreso[congreso$id_state== 23 & congreso$election_year==2019,]
D21 <- congreso[congreso$id_state== 10 & congreso$election_year==2021,]
D18 <- congreso[congreso$id_state== 10 & congreso$election_year==2018,]
H21 <- congreso[congreso$id_state== 13 & congreso$election_year==2021,]
H18 <- congreso[congreso$id_state== 13 & congreso$election_year==2018,]
congreso$winner<- as.factor(congreso$winner)
congreso$second<- as.factor(congreso$second)
Dtto <- st_read("DISTRITO_LOCAL.shp")
QR <- Dtto[Dtto$ENTIDAD == 23,]
D <- Dtto[Dtto$ENTIDAD == 10,]
H <- Dtto[Dtto$ENTIDAD == 13,]
#congreso 2016
MQR19 <- merge(QR, QR19, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroo19 <- MQR19 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","red", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo19
mapzoom2 <- MQR19 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#B1191E", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapa2 <- grid.arrange( grobs = list(mapaqroo19, mapzoom2),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa2
HGO21 <- merge(H, H21, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapah21 <- HGO21 %>% ggplot() + geom_sf(fill= ifelse( HGO21$winner == "PAN", "#001D82", ifelse( HGO21$winner=="PRI","#DF2630", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapah21
HGO18 <- merge(H, H18, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapah18 <- HGO18 %>% ggplot() + geom_sf(fill= ifelse( HGO18$winner=="PRI","#DF2630", "#B1191E"), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapah18
mapah21
mapah18
pacman::p_load(tidyverse,readxl, plyr, dplyr, ggplot2, tidyr, tidyverse, extrafont, gganimate, gapminder, plotly, shiny, sp, sf, foreign, readr, rgdal, ggrepel, gridExtra, grid, lattice, pdftools, tesseract, rgdal, tmap, raster)
#read legislative elections datasets
congreso<- read_xlsx("CongresosLocalesMR.xlsx")
summary(congreso)
QR16 <- congreso[congreso$id_state== 23 & congreso$election_year==2016,]
QR19 <- congreso[congreso$id_state== 23 & congreso$election_year==2019,]
D21 <- congreso[congreso$id_state== 10 & congreso$election_year==2021,]
D18 <- congreso[congreso$id_state== 10 & congreso$election_year==2018,]
H21 <- congreso[congreso$id_state== 13 & congreso$election_year==2021,]
H18 <- congreso[congreso$id_state== 13 & congreso$election_year==2018,]
congreso$winner<- as.factor(congreso$winner)
congreso$second<- as.factor(congreso$second)
Dtto <- st_read("DISTRITO_LOCAL.shp")
QR <- Dtto[Dtto$ENTIDAD == 23,]
D <- Dtto[Dtto$ENTIDAD == 10,]
H <- Dtto[Dtto$ENTIDAD == 13,]
#congreso 2016
MQR19 <- merge(QR, QR19, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroo19 <- MQR19 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","red", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo19
mapzoom2 <- MQR19 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#B1191E", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapa2 <- grid.arrange( grobs = list(mapaqroo19, mapzoom2),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa2
MQR16 <- merge(QR, QR16, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroo16 <- MQR16 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82","#42A03F"), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo16
mapzoom <- MQR16 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#42A03F", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapa <- grid.arrange( grobs = list(mapaqroo16, mapzoom),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa
#Hidalgo
HGO21 <- merge(H, H21, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapah21 <- HGO21 %>% ggplot() + geom_sf(fill= ifelse( HGO21$winner == "PAN", "#001D82", ifelse( HGO21$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapah21
HGO18 <- merge(H, H18, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapah18 <- HGO18 %>% ggplot() + geom_sf(fill= ifelse( HGO18$winner=="PRI","#42A03F", "#B1191E"), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapah18
#Durango
DGO21 <- merge(D, D21, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapadgo21 <- DGO21 %>% ggplot() + geom_sf(fill= ifelse( DGO21$winner == "PAN", "#001D82", ifelse( DGO21$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapadgo21
DGO18 <- merge(D, D18, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapadgo18 <- DGO18 %>% ggplot() + geom_sf(fill= ifelse( DGO18$winner == "PAN", "#001D82", ifelse( DGO18$winner=="PRI","#42A03F", ifelse( DGO18=="Partido Duranguense PD","grey","#B1191E"))), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapadgo18
mapa
mapa
qroo19 <- MQR19 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","red", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo19
mapaqroo19 <- MQR19 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","red", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo19
mapzoom2 <- MQR19 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#B1191E", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapa2 <- grid.arrange( grobs = list(mapaqroo19, mapzoom2),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa2
mapaqroo19 <- MQR19 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo19
mapzoom2 <- MQR19 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#B1191E", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapa2 <- grid.arrange( grobs = list(mapaqroo19, mapzoom2),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa2
MQR16 <- merge(QR, QR16, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroo16 <- MQR16 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82","#42A03F"), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo16
mapzoom <- MQR16 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#42A03F", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapa <- grid.arrange( grobs = list(mapaqroo16, mapzoom),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapa
congreso<- read_xlsx("CongresosLocalesMR.xlsx")
GQR <- congreso[congreso$id_state== 23 & congreso$election_type=="governor",]
congreso$winner<- as.factor(congreso$winner)
congreso$second<- as.factor(congreso$second)
GQR <- congreso[congreso$id_state== 23 & congreso$election_type=="governor",]
GH <- congreso[congreso$id_state== 13 & congreso$election_type=="governor",]
GD <- congreso[congreso$id_state== 10 & congreso$election_type=="governor",]
GQR16 <- merge(QR, GQR, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroogob <- GQR16 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroogob
GQR16 <- merge(QR, GQR, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroogob <- GQR16 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroogob
mapaqroogob <- GQR16 %>% ggplot() + geom_sf(fill= ifelse( GQR16$winner == "PAN", "#001D82", ifelse( GQR16$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroogob
mapzoomgob <- GQR16 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#B1191E", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapagob <- grid.arrange( grobs = list(mapaqroog, mapzoomgob),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapagob
mapzoomgob <- GQR16 %>% filter(DISTRITO_L >= 2 & DISTRITO_L <= 7 ) %>% ggplot() + geom_sf(fill= "#B1191E", color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapagob <- grid.arrange( grobs = list(mapaqrogog, mapzoomgob),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapagob <- grid.arrange( grobs = list(mapaqrogob, mapzoomgob),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapagob <- grid.arrange( grobs = list(mapaqroogob, mapzoomgob),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapagob
DGOGob <- merge(D, GD, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapadgogob <- DGOGob %>% ggplot() + geom_sf(fill= ifelse( DGOGob$winner == "PAN", "#001D82", ifelse( DGOGob$winner=="PRI","#42A03F", ifelse( DGOGob=="Partido Duranguense PD","grey","#B1191E"))), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapadgogob
HGob <- merge(H, GH, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapahgogob <- HGob %>% ggplot() + geom_sf(fill= ifelse( HGob$winner == "PAN", "#001D82", ifelse( HGob$winner=="PRI","#42A03F", ifelse( HGob=="Partido Duranguense PD","grey","#B1191E"))), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapahgogob
#Quintana Roo, Hidalgo,Durango
pacman::p_load(tidyverse,readxl, plyr, dplyr, ggplot2, tidyr, tidyverse, extrafont, gganimate, gapminder, plotly, shiny, sp, sf, foreign, readr, rgdal, ggrepel, gridExtra, grid, lattice, pdftools, tesseract, rgdal, tmap, raster)
congreso<- read_xlsx("CongresosLocalesMR.xlsx")
congreso$winner<- as.factor(congreso$winner)
congreso$second<- as.factor(congreso$second)
QR16 <- congreso[congreso$id_state== 23 & congreso$election_year==2016,]
QR19 <- congreso[congreso$id_state== 23 & congreso$election_year==2019,]
D21 <- congreso[congreso$id_state== 10 & congreso$election_year==2021,]
D18 <- congreso[congreso$id_state== 10 & congreso$election_year==2018,]
H21 <- congreso[congreso$id_state== 13 & congreso$election_year==2021,]
H18 <- congreso[congreso$id_state== 13 & congreso$election_year==2018,]
GQR <- congreso[congreso$id_state== 23 & congreso$election_type=="governor",]
GH <- congreso[congreso$id_state== 13 & congreso$election_type=="governor",]
GD <- congreso[congreso$id_state== 10 & congreso$election_type=="governor",]
AG21<- congreso[congreso$id_state==1 & congreso$election_year==2021,]
AG18<- congreso[congreso$id_state==1 & congreso$election_year==2018,]
AG15<- congreso[congreso$id_state==1 & congreso$election_year==2015,]
HD21 <- congreso[congreso$id_state==13 & congreso$election_year==2021,]
HD15 <- congreso[congreso$id_state==13 & congreso$election_year==2015,]
H <- Dtto[Dtto$ENTIDAD == 13,]
A <- Dtto[Dtto$ENTIDAD == 1,]
O <- Dtto[Dtto$ENTIDAD == 20,]
DR21 <- congreso[congreso$id_state==10 & congreso$election_year==2021,]
OX21 <- congreso[congreso$id_state== 20 & congreso$election_year==2021,]
QR16 <- congreso[congreso$id_state== 23 & congreso$election_year==2016,]
elecciones<- read_xlsx("CongresosLocalesMR.xlsx")
elecciones$winner<- as.factor(elecciones$winner)
elecciones$second<- as.factor(elecciones$second)
congreso <- elecciones[elecciones$election_type=="congress",]
AG21 <- congreso[congreso$id_state==1 & congreso$election_year==2021,]
AG18 <- congreso[congreso$id_state==1 & congreso$election_year==2018,]
AG15 <- congreso[congreso$id_state==1 & congreso$election_year==2015,]
QR16 <- congreso[congreso$id_state== 23 & congreso$election_year==2016,]
QR19 <- congreso[congreso$id_state== 23 & congreso$election_year==2019,]
D21 <- congreso[congreso$id_state== 10 & congreso$election_year==2021,]
D18 <- congreso[congreso$id_state== 10 & congreso$election_year==2018,]
H21 <- congreso[congreso$id_state== 13 & congreso$election_year==2021,]
H18 <- congreso[congreso$id_state== 13 & congreso$election_year==2018,]
OX21 <- congreso[congreso$id_state== 20 & congreso$election_year==2021,]
OX18 <- congreso[congreso$id_state==20 & congreso$election_year==2018,]
OX21 <- congreso[congreso$id_state== 20 & congreso$election_year==2021,]
OX18 <- congreso[congreso$id_state==20 & congreso$election_year==2018,]
pacman::p_load(tidyverse,readxl, plyr, dplyr, ggplot2, tidyr, tidyverse, extrafont, gganimate, gapminder, plotly, shiny, sp, sf, foreign, readr, rgdal, ggrepel, gridExtra, grid, lattice, pdftools, tesseract, rgdal, tmap, raster)
#read legislative elections datasets
elecciones<- read_xlsx("CongresosLocalesMR.xlsx")
summary(congreso)
elecciones$winner<- as.factor(elecciones$winner)
elecciones$second<- as.factor(elecciones$second)
congreso <- elecciones[elecciones$election_type=="congress",]
AG21 <- congreso[congreso$id_state==1 & congreso$election_year==2021,]
AG18 <- congreso[congreso$id_state==1 & congreso$election_year==2018,]
A <- Dtto[Dtto$ENTIDAD == 1,]
Dtto <- st_read("DISTRITO_LOCAL.shp")
A <- Dtto[Dtto$ENTIDAD == 1,]
TM18 <- congreso[congreso$id_state==28 & congreso$election_year==2018,]
elecciones<- read_xlsx("CongresosLocalesMR.xlsx")
elecciones$winner<- as.factor(elecciones$winner)
elecciones$second<- as.factor(elecciones$second)
congreso <- elecciones[elecciones$election_type=="congress",]
AG21 <- congreso[congreso$id_state==1 & congreso$election_year==2021,]
AG18 <- congreso[congreso$id_state==1 & congreso$election_year==2018,]
AG15 <- congreso[congreso$id_state==1 & congreso$election_year==2015,]
QR16 <- congreso[congreso$id_state== 23 & congreso$election_year==2016,]
QR19 <- congreso[congreso$id_state== 23 & congreso$election_year==2019,]
D21 <- congreso[congreso$id_state== 10 & congreso$election_year==2021,]
D18 <- congreso[congreso$id_state== 10 & congreso$election_year==2018,]
H21 <- congreso[congreso$id_state== 13 & congreso$election_year==2021,]
H18 <- congreso[congreso$id_state== 13 & congreso$election_year==2018,]
OX21 <- congreso[congreso$id_state== 20 & congreso$election_year==2021,]
OX18 <- congreso[congreso$id_state==20 & congreso$election_year==2018,]
TM21 <- congreso[congreso$id_state== 28 & congreso$election_year==2021,]
TM18 <- congreso[congreso$id_state==28 & congreso$election_year==2018,]
TM18 <- congreso[congreso$id_state==28 & congreso$election_year==2018,]
elecciones<- read_xlsx("CongresosLocalesMR.xlsx")
elecciones$winner<- as.factor(elecciones$winner)
elecciones$second<- as.factor(elecciones$second)
congreso <- elecciones[elecciones$election_type=="congress",]
AG21 <- congreso[congreso$id_state==1 & congreso$election_year==2021,]
AG18 <- congreso[congreso$id_state==1 & congreso$election_year==2018,]
AG15 <- congreso[congreso$id_state==1 & congreso$election_year==2015,]
QR16 <- congreso[congreso$id_state== 23 & congreso$election_year==2016,]
QR19 <- congreso[congreso$id_state== 23 & congreso$election_year==2019,]
D21 <- congreso[congreso$id_state== 10 & congreso$election_year==2021,]
D18 <- congreso[congreso$id_state== 10 & congreso$election_year==2018,]
H21 <- congreso[congreso$id_state== 13 & congreso$election_year==2021,]
H18 <- congreso[congreso$id_state== 13 & congreso$election_year==2018,]
OX21 <- congreso[congreso$id_state== 20 & congreso$election_year==2021,]
OX18 <- congreso[congreso$id_state==20 & congreso$election_year==2018,]
TM21 <- congreso[congreso$id_state== 28 & congreso$election_year==2021,]
TM18 <- congreso[congreso$id_state==28 & congreso$election_year==2018,]
GQR <- congreso[congreso$id_state== 23 & congreso$election_type=="governor",]
GH <- congreso[congreso$id_state== 13 & congreso$election_type=="governor",]
GD <- congreso[congreso$id_state== 10 & congreso$election_type=="governor",]
Dtto <- st_read("DISTRITO_LOCAL.shp")
QR <- Dtto[Dtto$ENTIDAD == 23,]
D <- Dtto[Dtto$ENTIDAD == 10,]
H <- Dtto[Dtto$ENTIDAD == 13,]
A <- Dtto[Dtto$ENTIDAD == 1,]
O <- Dtto[Dtto$ENTIDAD == 20,]
TM <- Dtto[Dtto$ENTIDAD == 28,]
GA <- congreso[congreso$id_state== 1 & congreso$election_type=="governor",]
GO <- congreso[congreso$id_state== 20 & congreso$election_type=="governor",]
GT <- congreso[congreso$id_state== 28 & congreso$election_type=="governor",]
GA <- congreso[congreso$id_state== 1 & congreso$election_type=="governor",]
GQR <- elecciones[elecciones$id_state== 23 & elecciones$election_type=="governor",]
GH <- elecciones[elecciones$id_state== 13 & elecciones$election_type=="governor",]
GD <- elecciones[elecciones$id_state== 10 & elecciones$election_type=="governor",]
GA <- elecciones[elecciones$id_state== 1 & elecciones$election_type=="governor",]
GO <- elecciones[elecciones$id_state== 20 & elecciones$election_type=="governor",]
GT <- elecciones[elecciones$id_state== 28 & elecciones$election_type=="governor",]
View(GA)
View(OX18)
View(AG21)
MQR19 <- merge(QR, QR19, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaqroo19 <- MQR19 %>% ggplot() + geom_sf(fill= ifelse( MQR19$winner == "PAN", "#001D82", ifelse( MQR19$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaqroo19
AG21 <- merge(A, A21, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaa21 <- AG21 %>% ggplot() + geom_sf(fill= ifelse( AG21$winner == "PAN", "#001D82", ifelse( AG21$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaa21
AG21 <- merge(A, AG21, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaa21 <- AG21 %>% ggplot() + geom_sf(fill= ifelse( AG21$winner == "PAN", "#001D82", ifelse( AG21$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaa21
AG18 <- merge(A, AG18, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaa18 <- AG18 %>% ggplot() + geom_sf(fill= ifelse( AG18$winner=="PRI","#42A03F", "#B1191E"), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaa18
mapaa18 <- AG18 %>% ggplot() + geom_sf(fill= ifelse( AG18$winner == "PAN", "#001D82", ifelse( AG18$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaa18
mapaagsgob <- AGob %>% ggplot() + geom_sf(fill= ifelse( AGob$winner == "PAN", "#001D82", ifelse( AGob$winner=="PRI","#42A03F","#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaagsgob
AGob <- merge(A, GA, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaagsgob <- AGob %>% ggplot() + geom_sf(fill= ifelse( AGob$winner == "PAN", "#001D82", ifelse( AGob$winner=="PRI","#42A03F","#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaagsgob
O21 <- merge(O, OX21, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapao21 <- O21 %>% ggplot() + geom_sf(fill= ifelse( O21$winner == "PAN", "#001D82", ifelse( O21$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapao21
O18 <- merge(O, OX18, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapao18 <- O18 %>% ggplot() + geom_sf(fill= ifelse( O18$winner == "PAN", "#001D82", ifelse( O18$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapao18
OGob <- merge(O, GO, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapaoxcgob <- OGob %>% ggplot() + geom_sf(fill= ifelse( OGob$winner == "PAN", "#001D82", ifelse( OGob$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapaoxcgob
TM21 <- merge(TM, TM21, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapat21 <- TM21 %>% ggplot() + geom_sf(fill= ifelse( TM21$winner == "PAN", "#001D82", ifelse( TM21$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapat21
TM18 <- merge(TM, TM18, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapat18 <- TM18 %>% ggplot() + geom_sf(fill= ifelse( TM18$winner == "PAN", "#001D82", ifelse( TM18$winner=="PRI","#42A03F", "#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapat18
TGob <- merge(TM, GT, by.x="DISTRITO_L", by.y="id_district", all.x=TRUE, all.y=TRUE)
mapatamgob <- TGob %>% ggplot() + geom_sf(fill= ifelse( TGob$winner == "PAN", "#001D82", ifelse( TGob$winner=="PRI","#42A03F","#B1191E")), color="white") + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank())
mapatamgob
mapzoom <- mapat18 %>% filter(DISTRITO_L==11) %>% ggplot() + geom_sf(fill= alpha("red",0.5)) + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank()) + labs(title = "Distrito 11") + theme(plot.title = element_text(face = "bold", size = 6, family = "Lato", hjust = 0.5))
mapzoom <- TM18 %>% filter(DISTRITO_L==11) %>% ggplot() + geom_sf(fill= alpha("red",0.5)) + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank()) + labs(title = "Distrito 11") + theme(plot.title = element_text(face = "bold", size = 6, family = "Lato", hjust = 0.5))
mapa <- grid.arrange( grobs = list(mapat18, mapzoom),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))
mapzoom <- TM18 %>% filter(DISTRITO_L==11) %>% ggplot() + geom_sf(fill= alpha("#B1191E")) + theme_minimal() + theme(axis.title = element_blank(), axis.text=element_blank(), panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank()) + labs(title = "Distrito 11") + theme(plot.title = element_text(face = "bold", size = 6, family = "Lato", hjust = 0.5))
mapa <- grid.arrange( grobs = list(mapat18, mapzoom),layout_matrix = rbind(c(1, 1, 1),
c(1, 1, 2),
c(1, 1, 2)))