-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLife_expectancy_pj.Rmd
More file actions
382 lines (358 loc) · 6.92 KB
/
Life_expectancy_pj.Rmd
File metadata and controls
382 lines (358 loc) · 6.92 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
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
---
title: "Tuan Linh Dao"
output: html_notebook
---
####
# Project 2: Data visualization with life expectancy
# 10/08/2021
####
# A. Load library :
####
### To install a package from R, type the command install.packages("name of the package") or click "Packages" then "Install", type the name of package that we want to install.
####
```{r}
library(ggplot2)
library(gganimate)
library(dplyr)
library(ggthemes)
library(RColorBrewer)
```
####
# B. Import dataset :
####
```{r}
data <- read.csv(file.choose(), header = T)
data
```
####
# C. Line chart with ggplot :
####
```{r}
data %>%
ggplot(aes(y=Life.expectancy,x=Year, col=Entity)) +
geom_line() +
labs(title = "Life expectancy every 50 years of 9 countries",
y = "Life expectancy",
x = "Year",
color = "Country") +
theme_fivethirtyeight()
```
####
# D. Countries :
####
## 1) Australia :
####
### a) Dataframe :
```{r}
aus <- data[data$Entity == "Australia", ]
aus
```
####
### b) Summarize data :
####
```{r}
summary(aus)
# Average life expectancy of Australia :
m_aus <- mean(aus$Life.expectancy)
m_aus
```
####
### c) Plot :
```{r}
aus %>%
ggplot(aes(y=Life.expectancy,x=Year, col=Entity)) +
geom_line() +
labs(title = "Life expectancy of Australia",
y = "Life expectancy",
x = "Year",
color = "Country")
```
####
## 2) Canada :
####
### a) Dataframe :
```{r}
can <- data[data$Entity == "Canada", ]
can
```
####
### b) Summarize data :
```{r}
summary(can)
# Average life expectancy of Canada :
m_can <- mean(can$Life.expectancy)
m_can
```
####
### c) Plot :
```{r}
can %>%
ggplot(aes(y=Life.expectancy,x=Year, col=Entity)) +
geom_line() +
labs(title = "Life expectancy of Canada",
y = "Life expectancy",
x = "Year",
color = "Country")
```
####
## 3) France :
####
### a) Dataframe :
```{r}
fra <- data[data$Entity == "France", ]
fra
```
####
### b) Summarize data :
```{r}
summary(fra)
# Average life expectancy of France :
m_fra <- mean(fra$Life.expectancy)
m_fra
```
####
### c) Plot :
```{r}
fra %>%
ggplot(aes(y=Life.expectancy,x=Year, col=Entity)) +
geom_line() +
labs(title = "Life expectancy of France",
y = "Life expectancy",
x = "Year",
color = "Country")
```
####
## 3) Germany :
####
### a) Dataframe :
```{r}
ger <- data[data$Entity == "Germany", ]
ger
```
####
### b) Summarize data :
```{r}
summary(ger)
# Average life expectancy of Germany :
m_ger <- mean(ger$Life.expectancy)
m_ger
```
####
### c) Plot :
```{r}
ger %>%
ggplot(aes(y=Life.expectancy,x=Year, col=Entity)) +
geom_line() +
labs(title = "Life expectancy of Germany",
y = "Life expectancy",
x = "Year",
color = "Country")
```
####
## 5) Italy :
####
### a) Dataframe :
```{r}
ita <- data[data$Entity == "Italy", ]
ita
```
####
### b) Summarize data :
```{r}
summary(ita)
# Average life expectancy of Italy :
m_ita <- mean(ita$Life.expectancy)
m_ita
```
####
### c) Plot :
```{r}
ita %>%
ggplot(aes(y=Life.expectancy,x=Year, col=Entity)) +
geom_line() +
labs(title = "Life expectancy of Italy",
y = "Life expectancy",
x = "Year",
color = "Country")
```
####
## 6) Japan :
####
### a) Dataframe :
```{r}
jpn <- data[data$Entity == "Japan", ]
jpn
```
####
### b) Summarize data :
```{r}
summary(jpn)
# Average life expectancy of Japan :
m_jpn <- mean(jpn$Life.expectancy)
m_jpn
```
####
### c) Plot :
```{r}
jpn %>%
ggplot(aes(y=Life.expectancy,x=Year, col=Entity)) +
geom_line() +
labs(title = "Life expectancy of Japan",
y = "Life expectancy",
x = "Year",
color = "Country") +
transition_time(Year)
```
####
## 7) Switzerland :
####
### a) Dataframe :
```{r}
swi <- data[data$Entity == "Switzerland", ]
swi
```
####
### b) Summarize data :
```{r}
summary(swi)
# Average life expectancy of Switzerland :
m_swi <- mean(swi$Life.expectancy)
m_swi
```
####
### c) Plot :
```{r}
swi %>%
ggplot(aes(y=Life.expectancy,x=Year, col=Entity)) +
geom_line() +
labs(title = "Life expectancy of Switzerland",
y = "Life expectancy",
x = "Year",
color = "Country")
```
####
## 8) United Kingdom :
####
### a) Dataframe :
```{r}
uk <- data[data$Entity == "United Kingdom", ]
uk
```
####
### b) Summarize data :
```{r}
summary(uk)
# Average life expectancy of UK :
m_uk <- mean(uk$Life.expectancy)
m_uk
```
####
### c) Plot :
```{r}
uk %>%
ggplot(aes(y=Life.expectancy,x=Year, col=Entity)) +
geom_line() +
labs(title = "Life expectancy of United Kingdom",
y = "Life expectancy",
x = "Year",
color = "Country")
```
####
## 9) United States :
####
### a) Dataframe :
```{r}
usa <- data[data$Entity == "United States", ]
usa
```
####
### b) Summarize data :
```{r}
summary(usa)
# Average life expectancy of USA :
m_usa <- mean(usa$Life.expectancy)
m_usa
```
####
### c) Plot :
```{r}
usa %>%
ggplot(aes(y=Life.expectancy,x=Year, col=Entity)) +
geom_line() +
labs(title = "Life expectancy of United States",
y = "Life expectancy",
x = "Year",
color = "Country")
```
####
# E. Summary :
####
## a) Dataframe with all countries :
```{r}
# Column of countries :
Country <- c("Australia","Canada","France","Germany","Italy","Japan","Swiss","UK","USA")
n
df1 <- rbind(m_aus,m_can,m_fra,m_ger,m_ita,m_jpn,m_swi,m_uk,m_usa)
df1
```
####
```{r}
df2 <- data.frame(Country,df1)
df2
```
####
## b) Plot :
####
### b.1) Bar plot by defautlt :
####
```{r}
df2 %>%
ggplot(aes(Country,df1,fill=Country)) +
geom_bar(stat = "identity") +
labs(title = "Average life expectency of 9 countries",
y="Life expectancy",
x="Countries")
# Customize the color of the bar :
#+ scale_fill_manual(values = c("Australia" = "salmon",
#"Canada" = "firebrick",
#"France" = "blue",
#"Germany" = "yellow",
#"Italy" = "green",
#"Japan" = "white",
#"Swiss" = "brown",
#"UK" = "red",
#"USA" = "orange"))
```
####
### b.2) Bar plot in ascending order :
####
```{r}
df2 %>%
ggplot(aes(reorder(Country,df1),df1,fill=Country)) +
geom_bar(stat = "identity") +
labs(title = "Average life expectency of 9 countries in ascending order",
y="Life expectancy",
x="Countries")
```
####
### b.3) Bar plot in descending order :
####
```{r}
df2 %>%
ggplot(aes(reorder(Country,-df1),df1,fill=Country)) +
geom_bar(stat = "identity") +
labs(title = "Average life expectency of 9 countries in descending order",
y="Life expectancy",
x="Countries")
```
####
### b.4) We can type geom_col() instead of geom_bar(stat="identity) to reduce the amount of code, they are the same function.
####
```{r}
df2 %>%
ggplot(aes(reorder(Country,-df1),df1,fill=Country)) +
geom_col() +
labs(title = "Average life expectency of 9 countries in descending order",
y="Life expectancy",
x="Countries")
```