-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.Rmd
More file actions
298 lines (255 loc) · 8.81 KB
/
Copy pathcode.Rmd
File metadata and controls
298 lines (255 loc) · 8.81 KB
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
---
title: "Data Analysis on World Happiness Report"
author: "Ruo Shan Tan"
date: "July 6, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Import packages and data files
```{r}
library(ggplot2)
library(dplyr)
library(highcharter)
library(formattable)
library(plotly)
library(countrycode)
library(maps)
data(worldgeojson)
df16<-read.csv("C:/Users/8Users/Desktop/Happiness/2016.csv")
df17<-read.csv("C:/Users/8Users/Desktop/Happiness/2017.csv")
```
## Organize dataset
```{r}
df1<-merge(df16[,c(1,3)],
df17[,c(1,3)],
by.x = "Country",
by.y = "Country")
colnames(df1)<-c("Country","Happiness Rank 2016","Happiness Rank 2017")
df1<-df1%>%
mutate(`Rank Change`=`Happiness Rank 2016`-`Happiness Rank 2017`)
```
## TABULATION BY CHANGES IN HAPPINESS RANKING
```{r}
formattable(df1,list(
`Rank Change` = formatter(
"span",
style=~formattable::style(color=ifelse(`Rank Change`>0,"green","red")))))%>%
as.datatable()
```
##HAPPINESS OVERVIEW ON WORLD MAP
```{r}
df<-df16[,c("Country","Happiness.Score")]
df$Country<-as.character(df$Country)
colnames(df)<-c("country","value")
correction16<-c("Congo (Brazzaville)"="Democratic Republic of the Congo","Guinea"="Equatorial Guinea","North Cyprus"="Northern Cyprus","Serbia"="Republic of Serbia","Congo (Brazzaville)"="Republic of Congo","Somaliland region"="Somaliland","Tanzania"="United Republic of Tanzania","United States Minor Outlying Islands"="United States","United States"="United States of America","United States"="United States Virgin Islands","Russia"="Russian Federation","Venezuela"="Venezuela, Bolivarian Republic of","Bolivia"="Bolivia (Plurinational State of)","South Korea"="Republic of Korea" )
for(i in names(correction16)){
df[df$country==i,"country"]<-correction16[i]
}
df17.1<-df17[,c("Country","Happiness.Score")]
df17.1$Country<-as.character(df17.1$Country)
colnames(df17.1)<-c("country","value")
correction17.1<-c("Congo (Brazzaville)"="Democratic Republic of the Congo","Guinea"="Equatorial Guinea","North Cyprus"="Northern Cyprus","Serbia"="Republic of Serbia","Congo (Brazzaville)"="Republic of Congo","Somaliland region"="Somaliland","Tanzania"="United Republic of Tanzania","United States Minor Outlying Islands"="United States","United States"="United States of America","United States"="United States Virgin Islands","Somaliland Region"="Somaliland","Russia"="Russian Federation","Venezuela"="Venezuela, Bolivarian Republic of","Bolivia"="Bolivia (Plurinational State of)","South Korea"="Republic of Korea" )
for(i in names(correction17.1)){
df17.1[df17.1$country==i,"country"]<-correction17.1[i]
}
countrycode_data$country.name.en<-as.factor(countrycode_data$country.name.en)
df$iso3<-countrycode_data[match(df$country,countrycode_data$country.name.en),"iso3c"]
df17.1$iso3<-countrycode_data[match(df17.1$country,countrycode_data$country.name.en),"iso3c"]
map1<-highchart() %>%
hc_add_series_map(worldgeojson, df, value = "value", joinBy = "iso3") %>%
hc_legend(enabled = TRUE) %>%
hc_add_theme(hc_theme_google()) %>%
hc_mapNavigation(enabled = TRUE) %>%
hc_colorAxis(min=min(df$value),max=max(df$value),minColor = "#00ffff", maxColor = "#ffff00")%>%
hc_credits(enabled = F)
map2<- highchart() %>%
hc_add_series_map(worldgeojson, df17.1, value = "value", joinBy = "iso3") %>%
hc_legend(enabled = TRUE) %>%
hc_add_theme(hc_theme_google()) %>%
hc_mapNavigation(enabled = TRUE) %>%
hc_colorAxis(min=min(df17.1$value),max=max(df17.1$value),minColor = "#00ffff", maxColor = "#ffff00")%>%
hc_credits(enabled = F)
```
###2016
```{r}
map1
```
###2017
```{r}
map2
```
##BOXPLOT BY CONTINENTS
###2016
```{r}
plot_ly(df16,x=~Region,
y=~Happiness.Score,
type="box",
boxpoints="all",
pointpos = -1.8,
color=~Region)%>%
layout(xaxis=list(showticklabels = FALSE),
margin=list(b = 100))
```
### 2017
```{r}
plot_ly(df17,x=~Region,
y=~Happiness.Score,
type="box",
boxpoints="all",
pointpos = -1.8,
color=~Region)%>%
layout(xaxis=list(showticklabels = FALSE),
margin=list(b = 100))
```
## HAPPINESS VS LIFE EXPECTANCY
###2016
```{r}
plot_ly(df16,x=~Happiness.Score,
y=~Health..Life.Expectancy.,
color=~Region,
colors=c("red","orange","yellow","green","blue","purple","black","grey","gold","darkblue"),
size=~Happiness.Score,
hoverinfo = 'text',
text=~paste(Country))%>%
layout(xaxis=list(title="Happiness Score"),
yaxis=list(title="Life Expectancy"))
```
###2017
```{r}
plot_ly(df17,x=~Happiness.Score,
y=~Health..Life.Expectancy.,
color=~Region,
colors=c("red","orange","yellow","green","blue","purple","black","grey","gold","darkblue"),
size=~Happiness.Score,
hoverinfo = 'text',
text=~paste(Country))%>%
layout(xaxis=list(title="Happiness Score"),
yaxis=list(title="LIFE EXPECTANCY"))
```
##HAPPINESS VS ECONOMY
###2016
```{r}
plot_ly(df16,x=~Happiness.Score,
y=~Economy..GDP.per.Capita.,
color=~Region,
colors=c("red","orange","yellow","green","blue","purple","black","grey","gold","darkblue"),
size=~Happiness.Score,
hoverinfo = 'text',
text=~paste(Country))%>%
layout(xaxis=list(title="Happiness Score"),
yaxis=list(title="GDP per Capita"))
```
###2017
```{r}
plot_ly(df17,x=~Happiness.Score,
y=~Economy..GDP.per.Capita.,
color=~Region,
colors=c("red","orange","yellow","green","blue","purple","black","grey","gold","darkblue"),
size=~Happiness.Score,
hoverinfo = 'text',
text=~paste(Country))%>%
layout(xaxis=list(title="Happiness Score"),
yaxis=list(title="GDP per Capita"))
```
##HAPPINESS VS FAMILY
###2016
```{r}
plot_ly(df16,x=~Happiness.Score,
y=~Family,
color=~Region,
colors=c("red","orange","yellow","green","blue","purple","black","grey","gold","darkblue"),
size=~Happiness.Score,
hoverinfo = 'text',
text=~paste(Country))%>%
layout(xaxis=list(title="Happiness Score"),
yaxis=list(title="Family"))
```
###2017
```{r}
plot_ly(df17,x=~Happiness.Score,
y=~Family,
color=~Region,
colors=c("red","orange","yellow","green","blue","purple","black","grey","gold","darkblue"),
size=~Happiness.Score,
hoverinfo = 'text',
text=~paste(Country))%>%
layout(xaxis=list(title="Happiness Score"),
yaxis=list(title="Family"))
```
##HAPPINESS VS FREEDOM
###2016
```{r}
plot_ly(df16,x=~Happiness.Score,
y=~Freedom,
color=~Region,
colors=c("red","orange","yellow","green","blue","purple","black","grey","gold","darkblue"),
size=~Happiness.Score,
hoverinfo = 'text',
text=~paste(Country))%>%
layout(xaxis=list(title="Happiness Score"),
yaxis=list(title="Freedom"))
```
###2017
```{r}
plot_ly(df17,x=~Happiness.Score,
y=~Freedom,
color=~Region,
colors=c("red","orange","yellow","green","blue","purple","black","grey","gold","darkblue"),
size=~Happiness.Score,
hoverinfo = 'text',
text=~paste(Country))%>%
layout(xaxis=list(title="Happiness Score"),
yaxis=list(title="Freedom"))
```
##HAPPINESS VS GENEROSITY
###2016
```{r}
plot_ly(df16,x=~Happiness.Score,
y=~Generosity,
color=~Region,
colors=c("red","orange","yellow","green","blue","purple","black","grey","gold","darkblue"),
size=~Happiness.Score,
hoverinfo = 'text',
text=~paste(Country))%>%
layout(xaxis=list(title="Happiness Score"),
yaxis=list(title="Generosity"))
```
###2017
```{r}
plot_ly(df17,x=~Happiness.Score,
y=~Generosity,
color=~Region,
colors=c("red","orange","yellow","green","blue","purple","black","grey","gold","darkblue"),
size=~Happiness.Score,
hoverinfo = 'text',
text=~paste(Country))%>%
layout(xaxis=list(title="Happiness Score"),
yaxis=list(title="Generosity"))
```
##Happiness Trust;Gov
###2016
```{r}
plot_ly(df16,x=~Happiness.Score,
y=~Trust..Government.Corruption.,
color=~Region,
colors=c("red","orange","yellow","green","blue","purple","black","grey","gold","darkblue"),
size=~Happiness.Score,
hoverinfo = 'text',
text=~paste(Country))%>%
layout(xaxis=list(title="Happiness Score"),
yaxis=list(title="Trust..Government.Corruption."))
```
###2017
```{r}
plot_ly(df17,x=~Happiness.Score,
y=~Trust..Government.Corruption.,
color=~Region,
colors=c("red","orange","yellow","green","blue","purple","black","grey","gold","darkblue"),
size=~Happiness.Score,
hoverinfo = 'text',
text=~paste(Country))%>%
layout(xaxis=list(title="Happiness Score"),
yaxis=list(title="Trust towards the Government"))
```