-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive_world_map.R
194 lines (146 loc) · 8.1 KB
/
interactive_world_map.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
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
####------Making an Interactive Map Related to The World's Population-------####
### Install and Load libraries--------------------------------------------------
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("rnaturalearthdata")) install.packages("rnaturalearthdata")
if (!require("mapview")) install.packages("mapview")
if (!require("sf")) install.packages("sf")
if (!require("leafsync")) install.packages("leafsync")
if (!require("leaflet.extras2")) install.packages("leaflet.extras2")
if (!require("countrycode")) install.packages("countrycode")
library(tidyverse) # data wrangling and viz ecosystem
library(rnaturalearth) # Access Natural Earth dataset
library(mapview) # Create Interactive Maps easily
library(sf) # mapping with Simple Feature
library(leaflet) # interactive maps
library(leafsync) # plugin for leaflet
library(countrycode)
### Cleaning Data---------------------------------------------------------------
list.files()
df <- read.csv("D:/it proj/portfolio1/population-and-demography.csv")
print(lapply(df, class))
colnames(df) <- make.names(colnames(df))
hfa_sf <- ne_countries(scale = "medium", returnclass = "sf")
#(RENAMING)
df <- df %>% mutate(
Country.name = case_match(Country.name,
"United States" ~ "United States of America",
"Democratic Republic of Congo" ~
"Democratic Republic of the Congo",
"Tanzania" ~ "United Republic of Tanzania",
"Congo" ~ "Republic of Congo",
"Cote d'Ivoire" ~ "Ivory Coast",
"Czechia" ~ "Czech Republic",
.default = Country.name))
#(CLEANING AND SYNCING THE MAP)
hfa_map <- df %>%
filter(!Country.name %in% c("world",
"Less developed regions",
"Less developed regions, excluding China",
"Less developed regions,
excluding least developed countries",
"Lower-middle-income countries",
"Asia (UN)",
"Africa (UN)",
"Least developed countries",
"Upper-middle-income countries",
"Low-income countries",
"Land-locked developing countries (LLDC)",
"High-income countries",
"More developed regions",
"Latin America and the Caribbean (UN)",
"Europe (UN)",
"Small island developing states (SIDS)")) %>%
mutate(Country.name = str_trim(Country.name)) %>%
mutate(Percentage.Under.25 = Population.under.the.age.of.25/Population*100)
# left_join(hfa_sf, by = c("Country.name" = "sovereignt")) %>%
# sf::st_as_sf()
#(MAKING 4 CATEGORIES)
hfa_map <- hfa_map %>%
mutate(Population.Above.60 = hfa_map$Population.aged.60.to.69.years+
hfa_map$Population.aged.70.to.79.years+
hfa_map$Population.aged.80.to.89.years+
hfa_map$Population.aged.90.to.99.years+
hfa_map$Population.older.than.100.years) %>%
mutate(Percentage.Above.60 = round(Population.Above.60/Population*100, 1)) %>%
mutate(Population.40.to.59 = hfa_map$Population.aged.40.to.49.years+
hfa_map$Population.aged.50.to.59.years) %>%
mutate(Percentage.40.to.59 = round(Population.40.to.59/Population*100, 1)) %>%
mutate(Population.20.to.39 = hfa_map$Population.aged.20.to.29.years+
hfa_map$Population.aged.30.to.39.years) %>%
mutate(Percentage.20.to.39 = round(Population.20.to.39/Population*100, 1)) %>%
mutate(Population.Below.20 = hfa_map$Population.at.age.1+
hfa_map$Population.aged.1.to.4.years+
hfa_map$Population.aged.5.to.9.years+
hfa_map$Population.aged.10.to.14.years+
hfa_map$Population.aged.15.to.19.years) %>%
mutate(Percentage.Below.20 = round(Population.Below.20/Population*100, 1))
write.csv(hfa_map, "D:/it proj/practice dashboard/population-and-demography1.csv")
sum(hfa_map$Percentage.Under.25 == "Germany")/71
### Customizing Data and Creating Maps------------------------------------------
cols<- c("#F0F9E8", "#CCEBC5", "#A8DDB5", "#7BCCC4", "#4EB3D3"
, "#2B8CBE", "#0868AC", "#084081")
breaks <- c(1e6, 5e6, 10e6, 50e6, 100e6, 500e6, 1000e6, 1500e6)
new.breaks <- c(1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
## Youth Population in Different Countries______________________________________
numbers.of.youngs <- hfa_map %>%
filter(Year=="2021") %>%
select(Country.name,Population.under.the.age.of.25, Population, Year)
Youth.num <- mapview(numbers.of.youngs ,zcol = "Population.under.the.age.of.25",
layer.name = "Population under age 25 in 2021",
col.regions = cols, at = breaks, crs = "+proj=robin")
# Compare with The Past---
numbers.of.youngs.old <- hfa_map %>%
filter(Year=="1950") %>%
select(Country.name,Population.under.the.age.of.25, Population, Year)
Youth.num.old <-mapview(numbers.of.youngs.old,zcol =
"Population.under.the.age.of.25",
layer.name = "Population under age 25 in 1950",
col.regions = cols, at = breaks, crs = "+proj=robin")
Youth.num | Youth.num.old
## Youth Percentage in Different Countries______________________________________
persentage.of.youngs <- hfa_map %>%
filter(Year=="2021") %>%
select(Country.name,Percentage.Under.25, Population, Year)
Youth.perc <- mapview(persentage.of.youngs ,zcol = "Percentage.Under.25",
at = new.breaks,layer.name = "Age Under 25 in % (2021)",
crs = "+proj=robin")
# Compare with The Past---
persentage.of.youngs.old <- hfa_map %>%
filter(Year=="1950") %>%
select(Country.name,Percentage.Under.25, Population, Year)
Youth.perc.old <-mapview(persentage.of.youngs.old ,zcol = "Percentage.Under.25",
at =new.breaks,layer.name = "Age Under 25 in % (1950)",
crs = "+proj=robin")
Youth.perc | Youth.perc.old
## Elderly percentage and Population in Different countries_____________________
Eldery.new <- hfa_map %>%
filter(Year == "2021") %>%
select(Country.name,Population,Population.Above.60,Percentage.Above.60, Year)
Elderly.perc <- mapview(Eldery.new, zcol = "Percentage.Above.60",at =new.breaks,
layer.name = "Age Above 60 in % (2021)",
crs = "+proj=robin")
# Compare with The Past---
Eldery.old <- hfa_map %>%
filter(Year == "1950") %>%
select(Country.name,Population,Population.Above.60,Percentage.Above.60, Year)
Elderly.perc.old<-mapview(Eldery.old, zcol = "Percentage.Above.60",
at=new.breaks,layer.name = "Age Above 60 in % (1950)",
crs = "+proj=robin")
Elderly.perc | Elderly.perc.old
## Compare of four different categories of ages_________________________________
four.categories <- hfa_map %>%
filter(Year == "2021") %>%
select(Country.name,Population, Population.Below.20, Population.20.to.39,
Population.40.to.59, Population.Above.60,
Percentage.Below.20, Percentage.20.to.39, Percentage.40.to.59,
Percentage.Above.60, Year)
leafsync::sync(
mapview(four.categories, zcol = "Percentage.Below.20", at = new.breaks,
layer.name = "Age under 20 in %(2021)", crs = "+proj=robin"),
mapview(four.categories, zcol = "Percentage.20.to.39", at = new.breaks,
layer.name = "Age 20.to.39 in %(2021)", crs = "+proj=robin"),
mapview(four.categories, zcol = "Percentage.40.to.59", at = new.breaks,
layer.name = "Age 40.to.59 in %(2021)", crs = "+proj=robin"),
mapview(four.categories, zcol = "Percentage.Above.60", at = new.breaks,
layer.name = "Age over 60 in %(2021)", crs = "+proj=robin")
)