-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.Rmd
434 lines (334 loc) · 13.5 KB
/
code.Rmd
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
---
title: "Bihar Assembly Election Analysis"
output:
flexdashboard::flex_dashboard:
orientation: columns
social: menu
vertical_layout: fill
---
```{r setup, include=FALSE}
packages <- c("plotly","readxl","flexdashboard","dplyr","ggplot2","sf","tmap","readr","DT","ggridges","ggthemes","knitr","rAmCharts")
if(!require(packages)){
install.packages(packages)
}
library(flexdashboard)
library(ggplot2)
library(plotly)
library(dplyr)
#library(magrittr)
#library(cartography)
library(sf)
#library(haven)
#loading the Bihar Assambly Election Data
#read_csv("bihar-elec-data.csv")
library(readxl)
dataset<- read_excel("data/src/Bihar Assembly Election 2020 cleaned.xls")
#str(dataset)
#head(dataset)
#View(dataset)
#Dataset of all the candidates
dataset2 <-dataset %>% filter(dataset$`STATE/UT NAME`==dataset[[1]][1])
#str(dataset2)
#head(dataset2)
#View(dataset2)
#Dataset without NOTA
dataset3 <- dataset2 %>% filter(dataset2$PARTY !="NOTA")
#str(dataset3)
#head(dataset3)
#View(dataset3)
#Dataset without Independent candidate and NOTA
dataset4 <- dataset3 %>% filter(dataset3$PARTY !="IND")
#str(dataset4)
#head(dataset4)
#View(dataset4)
#Winning Candidates data
win_data_temp <- dataset3 %>% group_by(`AC NO.`) %>% filter(TOTAL==max(TOTAL))
colnames(win_data_temp)[colnames(win_data_temp)=="AC NO."]<-"AC_NO"
colnames(win_data_temp)[colnames(win_data_temp)=="CANDIDATE NAME"]<-"CANDIDATE_NAME"
```
Winning Candidates
==============================================================================
Column {data-width="1000"}
------------------------------------------------------------------------------
### **Winning Candidates data**
```{r,echo=F,out.width="100%",out.height="100%"}
#Winning Candidates data
win_data_temp <- dataset3 %>% group_by(`AC NO.`) %>% filter(TOTAL==max(TOTAL))
colnames(win_data_temp)[colnames(win_data_temp)=="AC NO."]<-"AC_NO"
colnames(win_data_temp)[colnames(win_data_temp)=="CANDIDATE NAME"]<-"CANDIDATE_NAME"
win_data_temp = as.data.frame(win_data_temp)
DT::datatable(win_data_temp,options = list(pageLength=20, scrollY=F),colnames=c("GENDER"="SEX", "STATE"="STATE/UT NAME"))
```
Interactive Maps
===============================================================================
Column {data-width="1000"}
------------------------------------------------------------------------------
```{r, echo=FALSE}
shape_data<-read_sf("Shape file/INDIA_AC.shp")
Bihar_shape_data<- shape_data %>% filter(shape_data$ST_CODE==10)
#View(Bihar_shape_data)
mapdata<-merge(Bihar_shape_data, win_data_temp, by="AC_NO")
#View(mapdata)
#names(mapdata)
#mapdata=View(mapdata)
#write.csv2(mapdata,"d.csv")
```
```{r, echo=FALSE}
#library(tmaptools)
pal=c("#05980D","#EE7B06","#CA4741","#EA3606","#E50505","#E72415","#AA6D6D",
"#E5E5E5","#BC06C5","#8456C5","#737373","#0B461E","#1A1A1A")
#Map code----------------------------------------------------------------
#library
library(tmap)
#Intering Interective mode------------------------------------------------
tmap_mode("view")
#District Layer
Bihar_assembly_map<-tm_shape(mapdata)+
#tm_fill(col = "PARTY", alpha=1, palette = "Reds", legend.show = F)+
tm_borders(col="black",lwd=1,alpha = 1)+
tm_fill(id="AC NAME", col = "PARTY", alpha=1, palette = pal, legend.show = T,
popup.vars = c("AC NAME", "CANDIDATE_NAME", "SEX","AGE", "CATEGORY",
"PARTY", "% VOTES POLLED" ))+tm_text("AC NAME", size=0.6)+
#tm_layout(style="cat", aes.palette = "Reds", aes.color = "Reds" )+
#other customization
#tm_borders(col="white",lwd=0.2)+
#tm_layout(frame = T, outer.margins = 0, inner.margins = 0)+
#tm_style("cobalt")+
tm_view(set.view = 8, alpha = 0.9, set.zoom.limits=c(7,11))+
tm_legend(legend.outside=T,legend.outside.position=c("right","top"),title="Bihar Assembly Election (Wining Candidates)" )
```
```{r interective-map, echo=FALSE, message=FALSE, warning=FALSE,out.width="100%", out.height="100%"}
Bihar_assembly_map
```
Data Summary
==============================================================================
Columns {data-width="300"}
------------------------------------------------------------------------------
### **Number of Candidates Fielded by Canstituency**
```{r}
#Number of Candidates Fielded by Canstituency
AC_cand = table(dataset3$`AC NAME`)
AC_cand = as.data.frame(AC_cand)
DT::datatable(AC_cand,options = list(pageLength=20, scrollY=F), colnames = c("ASSEMBLY CONSTITUENCY" = "Var1","CANDIDATES"="Freq"))
```
Columns {data-width="300"}
------------------------------------------------------------------------------
### **Number Of Candidates Fielded Party wise**
```{r}
#Number Of Candidates Fielded Party wise
DT::datatable(as.data.frame(table(dataset3$PARTY)),options = list( scrollY=F),colnames = c("PARTY" = "Var1","CANDIDATES"="Freq"))
```
### **Winning Candidates party wise**
```{r}
#Winning Candidates party wise
DT::datatable(as.data.frame(table(win_data_temp$PARTY)),options = list(scrollY=F),colnames = c("PARTY" = "Var1","CANDIDATES"="Freq"))
```
Columns {data-width="300"}
------------------------------------------------------------------------------
### **Female winning candidates**
```{r}
#Gender wie and partywise winning candidates
#FEMALES
win_data_temp_w <- win_data_temp %>% group_by(PARTY) %>% filter(SEX=="FEMALE")
DT::datatable(as.data.frame(table(win_data_temp_w$PARTY)),options = list(scrollY=F),colnames = c("PARTY" = "Var1","CANDIDATES"="Freq"))
```
### **Male winning candidates**
```{r}
#MALES
win_data_temp_m <- win_data_temp %>% group_by(PARTY) %>% filter(SEX=="MALE")
DT::datatable(as.data.frame(table(win_data_temp_m$PARTY)),options = list( scrollY=F),colnames = c("PARTY" = "Var1","CANDIDATES"="Freq"))
```
Columns {data-width="100"}
------------------------------------------------------------------------------
### **Winning candidates Gender Wise**
```{r}
#Winning candidates Gender Wise
knitr::kable(as.data.frame(table(win_data_temp$SEX)),col.names = c("Gender","Candidates"))
```
### **Winning Candidates Catagory wise**
```{r}
#Winning Candidates Catagory wise
knitr::kable(as.data.frame(table(win_data_temp$CATEGORY)),col.names = c("Catogary","Candidates"))
```
Plots
=============================================================================
Column {data-width="333"}
---------------------------------------------------------------------------
### **Catagory wise plot**
```{r}
#Catagory wise plot
p1 <- ggplot(dataset3, aes(x = CATEGORY, y = ..count../sum(..count..) )) +
geom_bar()+
labs(x="Catagory",title="Bihar Assembly Election Candidates By Catagory.",y="Percentage Of Candidates")+
scale_y_continuous(labels=scales::percent)
ggplotly(p1)
```
### **Gender Wise plot**
```{r}
#Gender Wise plot
p2 <- ggplot(dataset3, aes(x = SEX,y = ..count../sum(..count..), fill=CATEGORY))+
geom_bar(color="brown")+
labs(x="Gender",title="Bihar Assembly Election Candidates By Gender.",y="Percentage Of Candidates")
ggplotly(p2)
```
Column {data-width="333"}
-----------------------------------------------------------------------------
### **Age distribution Gender wise**
```{r}
#Age distribution Gender wise
p3 <- ggplot(dataset3, aes(x = SEX, y = AGE) )+
geom_boxplot()
ggplotly(p3)
```
### **Age distribution category wise**
```{r}
#Age distribution category wise
p4 <- ggplot(dataset3, aes(x=CATEGORY, y=AGE))+
geom_boxplot()
ggplotly(p4)
```
Column {data-width="333"}
------------------------------------------------------------------------------
### **Age distribution of big parties (Boxplot)**
```{r}
#Age distribution of big parties (Boxplot)
Big_party_data <- filter(dataset3, dataset3$PARTY %in% c("BJP","JD(U)","RJD","INC","LJP","IND"))
p5 <- ggplot(Big_party_data,aes(x=PARTY,y=AGE))+
geom_boxplot()
ggplotly(p5)
```
### **Age Distribution Of Big Parties (ridgesdensity plot)**
```{r, echo=FALSE,include=T,message=FALSE,error=F,warning=F}
#Age Distribution Of Big Parties (ridgesdensity plot)
library(ggridges)
p6 <- ggplot(Big_party_data,aes(x=AGE,y=PARTY,fill=PARTY))+
geom_density_ridges()+
theme_ridges()
p6
```
Static maps
==============================================================================
Columns {data-width="500"}
----------------------------------------------------------------------------
### **Male and Female Candidates**
```{r}
map_a <-ggplot(mapdata) +
aes(fill = SEX) +
geom_sf(shape = "circle", size = 0.5) +
scale_fill_brewer(palette = "RdGy",
direction = 1) +
labs(title = "Bihar Assembly Election Result", subtitle = "Assembly Election 2020",
caption = "data source:https://eci.gov.in/files/file/12787-bihar-legislative-election-2020", fill = "Male and Female Candidates") +
ggthemes::theme_economist() +
theme(plot.caption = element_text(face = "italic"))
map_a
```
Columns {data-width="500"}
----------------------------------------------------------------------------
### **Winning Candidates Party Wise**
```{r}
map_b <- ggplot(mapdata) +
aes(fill = PARTY) +
geom_sf(shape = "circle", size = 0.5) +
scale_fill_manual(values = c(AIMIM = "#05980D",
BJP = "#EE7B06", BSP = "#CA4741", CPI = "#EA3606", `CPI(ML)(L)` = "#E50505", CPIM = "#E72415", HAMS = "#AA6D6D",
INC = "#E5E5E5", IND = "#BC06C5", `JD(U)` = "#8456C5", LJP = "#737373", RJD = "#0B461E", VSIP = "#1A1A1A"
)) +
labs(title = "Bihar Assembly Election Result", subtitle = "Winning Candidates Party Wise", caption = "data source:https://eci.gov.in/files/file/12787-bihar-legislative-election-2020",
fill = "Party") +
theme_minimal() +
theme(plot.caption = element_text(face = "italic"))
map_b
```
Success Comparision Plot
============================================================================
Columns {data-width="500"}
--------------------------------------------------------------------------
```{r}
#setup chunk
#candidates won partywise without independent candidates
win_data_temp_tbl <-as.data.frame(table(win_data_temp$PARTY)) %>% filter(Var1 !="IND")
colnames(win_data_temp_tbl)<-c("PARTY","CANDIDATES WON")
win_part <- win_data_temp_tbl$PARTY
```
### **Winning Candidate Partywise**
```{r}
bar_a <- ggplot(win_data_temp_tbl) +
aes(x = PARTY, weight = `CANDIDATES WON`) +
geom_bar(fill = "#370E21") +
labs(
x = "Party",
y = "No. Of Winning Candidate",
title = "Winning Candidates",
subtitle = "Winning Candidates Partywise inclusing Independent Candidates",
caption = "Source: https://eci.gov.in/files/file/12787-bihar-legislative-election-2020/"
) +
ggthemes::theme_wsj()
ggplotly(bar_a)
```
### **Candidate Fielded Partywise**
```{r}
#candidates fielded partywise without independent candidates
dataset3 %>% filter(PARTY %in% win_part )%>% filter(PARTY != "IND") -> Can_field_partywise
Can_field_partywise=as.data.frame(table(Can_field_partywise$PARTY) )
colnames(Can_field_partywise) <- c("PARTY","CANDIDATES CONTESTED")
bar_b <- ggplot(Can_field_partywise) +
aes(x = PARTY, weight = `CANDIDATES CONTESTED`) +
geom_bar(fill = "#370E21") +
labs(
x = "Party",
y = "Number Of Candidate Fielded",
title = "Number Of Candidate Fielded Partywise",
subtitle = "Fielded Candidates Partywise inclusing Independent Candidates",
caption = "Source: https://eci.gov.in/files/file/12787-bihar-legislative-election-2020/"
) +
ggthemes::theme_wsj()
ggplotly(bar_b)
```
Columns {data-width="500"}
-----------------------------------------------------------------------------
### ***Candidates Won Partywise (Pie chart)***
```{r,out.width="100%", out.height="100%"}
library(rAmCharts)
amPieChart(valueField = 'CANDIDATES WON', titleField = 'PARTY',
theme = "light", dataProvider = win_data_temp_tbl) %>%
setLegend(position = "bottom", useMarkerColorForLabels = TRUE)%>%
addListener(name = "clickSlice", expression = paste("function (event) {",
"var obj = event.dataItem;",
"alert('Candidates Won: ' + obj.value);",
"}"))%>%
plot()
```
### ***Candidates Fielded Partywise (Pie chart)***
```{r }
amPieChart(valueField = 'CANDIDATES CONTESTED', titleField = 'PARTY',
theme = "light", dataProvider = Can_field_partywise) %>%
setLegend(position = "bottom", useMarkerColorForLabels = TRUE)%>%
addListener(name = "pullOutSlice", expression = paste("function (event) {",
"var obj = event.dataItem;",
"alert('Candidates Contested: ' + obj.value);",
"}"))%>%
plot()
```
VOTE SHARE PLOT
============================================================================
Columns {data-width="1000"}
--------------------------------------------------------------------------
### ***Party Performance***
```{r}
Perform_data = cbind(Can_field_partywise,win_data_temp_tbl,by="PARTY")
amSerialChart(dataProvider = Perform_data, categoryField = 'PARTY',
creditsPosition = "bottom-right",theme = "light")%>%
addGraph(valueField = "CANDIDATES WON", title = "Winning candidates")%>%
addGraph(valueField = "CANDIDATES CONTESTED", title = "Candidates Contested")%>%
setLegend(position = "bottom", useMarkerColorForLabels = TRUE)
# we use the setter 'add' since the property titles is an array of 'Title' objects"
```
Other Plot
============================================================================
Columns {data-width="1000"}
--------------------------------------------------------------------------
### ***Layered barplot***
```{r}
amBarplot(x = "PARTY", y = c("CANDIDATES CONTESTED","CANDIDATES WON"), data = Perform_data, horiz = F,show_values = T,layered = T)
```