-
Notifications
You must be signed in to change notification settings - Fork 2
/
Project.R
380 lines (320 loc) · 12.1 KB
/
Project.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
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
packages=c('corrplot',
'ggpubr',
'plotly',
'tidyverse',
'readxl',
'Hmisc',
'geojsonio',
'sf',
'tmap',
'spData',
'maptools',
'shiny',
'shinythemes',
'leaflet',
'RColorBrewer',
'rnaturalearth',
'rnaturalearthdata',
'hrbrthemes',
"WDI",
"ggvis",
'reshape2')
for(p in packages){library
if (!require(p,character.only = T)){
install.packages(p)
}
library(p,character.only = T)
}
### This part is done by ZHU Honglu
### This part has not been merged.
indicator2 <- WDI(country="all", indicator=c("NY.GDP.PCAP.CD", "SP.POP.TOTL", "SP.DYN.LE00.IN"), start=2011, end=2018, extra = TRUE)
drops <- c("iso2c","iso3c", "capital", "longitude", "latitude", "income", "lending")
indicator2 <- indicator2[ , !(names(indicator2) %in% drops)]
colnames(indicator2) <- c("country","year", "GDP_per_capita", "population_total", "life_expectancy", "region")
indicator2 <- indicator2[-c(1, 2, 3, 4, 5, 6, 19, 66, 67, 159, 178, 179, 180, 181, 182, 201, 202, 203, 204, 205, 206, 207, 225, 226, 227, 228, 236, 237, 238, 239, 240, 241, 242, 243, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 262, 263), ]
ui <- fluidPage(
sliderInput(inputId = "Year",
label = h5("Select Year"),
min = 2011,#as.Date(cv_min_date,"%Y-%m-%d"),
max = 2018,#as.Date(current_date,"%Y-%m-%d"),
value = 2018, #as.Date(current_date),
timeFormat = '%Y',
#timeFormat = "%d %b",
#animate=animationOptions(interval = 3000, loop = FALSE),
),
plotOutput("graph")
)
server <- function(input, output) {
output$graph = renderPlot({
indicator2 %>%
ggvis(~GDP_per_capita, ~life_expectancy, fill=~factor(region)) %>%
layer_points(size= ~population_total/1000000,opacity:=0.6) %>%
add_legend(scales = "size", properties = legend_props(legend = list(y = 200))) %>%
scale_numeric("y", domain = c(0, 90), nice = FALSE) %>%
add_axis("x", title = "GDP per Capita(USD)") %>%
add_axis("x", orient = "top", ticks = 0, title = "GDP per Capita vs. Life Expectancy 2019",
properties = axis_props(
axis = list(stroke = "white"),
labels = list(fontSize = 0)))
})
}
shinyApp(ui = ui, server = server)
# load packages
# and next
# try to draw a map
# here, argument country can change the country
# also we can choose the continent to see.
#world <- ne_countries(scale = 'medium', returnclass = "sf")
worldcountry = geojson_read("data/50m.geojson", what = "sp")
#worldcountry<-subset(worldcountry, NAME_LONG!="Antarctica")
#country_geoms = read_csv("data/country_geoms.csv")
HDI = read_csv('data/data_cleaned/HDI/0_HDI.csv')
HDI_selected = subset(HDI,Year==2018)
worldCountry_HDI <- merge(worldcountry, HDI_selected, by.x = "NAME_LONG", by.y = "Country")
##### HDI Distribution #####
distribution_HDI = worldcountry %>%
merge(filter(HDI,Year==2018),by.x = "NAME_LONG", by.y = "Country")
distribution_HDI$HDI
ggplot(filter(HDI,Year==2018),aes(x = reorder(Country,-HDI),
y = HDI,#color = Country,#fill = region
))+
geom_bar(position="stack", stat="identity",fill = "#cc4c02")+
ylab("HDI INDEX") +
#scale_x_categorical(breaks=seq(0, 10, 1))
theme_bw() +
scale_fill_manual(values=c("#cc4c02")) +
#scale_y_continuous(labels = function(l) {trans = l / 1000; paste0(trans, "K")}) +
scale_y_continuous(expand = c(0, 0))+
theme(legend.title = element_blank(),
axis.text.x = element_blank(),
axis.ticks = element_blank(),
legend.position = "",
plot.title = element_text(size=10),
plot.margin = margin(5, 12, 5, 5))
#plot_map <- worldcountry
# create plotting parameters for map
#bins = c(0,1,10,50,100,500,1000,Inf)
#HDI_pal <- colorBin("Blues", domain = worldCountry_HDI$"2018")#, bins = bins)
HDI_pal <- colorQuantile("Blues", domain = worldCountry_HDI$HDI)
#plot_map <- worldcountry[worldcountry$ADM0_A3 %in% cv_large_countries$alpha3, ]
labels <- sprintf(
"<strong>%s</strong><br/>
Index: %g",
worldCountry_HDI$NAME_LONG, worldCountry_HDI$HDI
) %>% lapply(htmltools::HTML)
popup = sprintf(
"<strong>%g</strong><br/>",worldCountry_HDI$HDI
) %>% lapply(htmltools::HTML)
# weight: the thickness of the boundary lines in pixels
# color: the color of the polygons
# label: the information to appear on hover
# highlightOptions: options to highlight a polygon on hover
#++++++++++++++++++ WORLD MAP AREA ++++++++++++++++++++++++++
basemap = leaflet(worldCountry_HDI) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons(#stroke = FALSE, # use to turn off the broad
smoothFactor = 0.2,
fillColor = ~HDI_pal(worldCountry_HDI$HDI), # ÊÇÂÖÀªÄÚµÄÑÕÉ«
fillOpacity = 0.7,
color="white", #stroke color
weight = 1, # stroke width in pixels
highlight = highlightOptions(
#weight = 5,
color = "#666",
#dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE #Whether the shape should be brought to front on hover
),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto"),
popup = popup,
popupOptions = popupOptions(maxWidth ="100%", closeOnClick = TRUE),
group = "Human Development Index"
) %>%
addLayersControl(
position = "bottomright",
baseGroups = c("Human Development Index","Gender Development Index"),
#overlayGroups = c("Human Development Index","Gender Development Index"),
options = layersControlOptions(collapsed = FALSE)) %>%
hideGroup(c("Human Development Index","Gender Development Index")) %>%
addLegend("bottomright", pal = HDI_pal, values = ~worldCountry_HDI$HDI,title = "<small>Index Value</small>")
basemap
#addPolygons(
#smoothFactor = 0.2,
#fillOpacity = 1,
#fillColor = ~HDI_pal(worldCountry_HDI$"2018"),
#weight = 0.5,
#opacity = 0.1,
#color = "white",
#dashArray = "1",
#fillOpacity = 0.7,
#highlight = highlightOptions(
# weight = 0.5,
#color = "#666",
#dashArray = "¡ª",
#fillOpacity = 0.5,
#bringToFront = TRUE),
#label = labels,
#labelOptions = labelOptions(
#style = list("font-weight" = "normal", padding = "3px 8px"),
#textsize = "15px",
#direction = "auto")) %>%
#=================================================================
states <-
geojson_read(
x = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
, what = "list"
)
class(states)
bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
pal <- colorBin("YlOrRd", domain = states$density, bins = bins)
labels <- sprintf(
"<strong>%s</strong><br/>%g people / mi<sup>2</sup>",
states$name, states$density
) %>% lapply(htmltools::HTML)
leaflet(states) %>%
setView(-96, 37.8, 4) %>%
addProviderTiles("MapBox", options = providerTileOptions(
id = "mapbox.light",
accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN'))) %>%
addPolygons(
fillColor = ~pal(density),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7,
highlight = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")) %>%
addLegend(pal = pal, values = ~density, opacity = 0.7, title = NULL,
position = "bottomright")
#========================================================================================================
# count the number of countries in different level of development of HDI
temp=all_data %>%
filter(Year == 2018) %>%
group_by(Level) %>%
summarise(
HDI = mean(HDI),
number_of_distinct_orders = length(unique(Country))) %>%
ungroup()
#______________Draw a density graph___________________
density_plot_2018=all_data %>%
filter(Year==2018) %>%
ggplot(aes(x=Gender_Inequality_Index)) +
geom_density(fill="Region", color="#e9ecef", alpha=0.8) +
ggtitle("Night price distribution of Airbnb appartements") +
theme_ipsum()
ggplotly(density_plot_2018)
#___________Draw a sparkline__________________________
# data wraggling:
Country_all_years_data = all_data %>%
filter(Country == 'China')
# at first , we keep all the variables and
# later we will decide which part of these variables will be kept.
# Now transform the data in a tidy form.
# indicator, year and value.
Country_all_years_data = all_data %>%
filter(Country == 'China') %>%
subset(select = -c(Continent,Region,Country,Level)) %>%
melt(id=c("Year"))
# now we can try to creat a sparkline
Country_all_years_data = all_data %>%
filter(Country == 'China') %>%
#filter(Year >= 2010) %>%
subset(select = c(Year,HDI,
Gender_Development_Index,
Gender_Inequality_Index,
Total_Unemployment_Rate,
Education_Index
)) %>%
melt(id=c("Year")) %>%
drop_na() %>%
ggplot(aes(x=Year, y=value)) +
facet_grid(variable ~ ., scales = "free_y") +
geom_line()+
theme_bw()+
theme(axis.text.x = element_text(angle = 60, hjust = 1))
ggplotly(Country_all_years_data)
#=========
worldcountry %>%
merge(filter(all_data,Year==2018) %>%
select(Country,Year,HDI),by.x = "NAME_LONG", by.y = "Country") #%>%# here we can change the input of data
#______________________-
Sys.Date()
#__________
heatmap_data <- all_data %>%
filter(Year == 2018) %>%
filter(Country %in% c('China','Australia'))%>%
select(Country,HDI,Gender_Development_Index,Gender_Inequality_Index)
row.names(heatmap_data) <- heatmap_data$Country
heatmap_data <- heatmap_data %>%
select(-Country)
heatmap_matrix <- data.matrix(heatmap_data)
row.names(heatmap_matrix) <- heatmap_data$Country
heatmap_data <- heatmap_data %>%
select(-Country)
heatmap_matrix <-heatmap_matrix[,-1]
normalize(heatmap_matrix)
temp= heatmaply(heatmap_matrix,
scale = 'none',
dist_method = 'euclidean',
hclust_method = 'ward.D',
seriate = 'OLO',
colors = Blues,
Colv=NA,
#k_row = 2,
margins = c(NA,200,60,NA),
fontsize_row = 4,
fontsize_col = 5,
main="World Happiness Score and Variables by Country, 2018 \nDataTransformation using Normalise Method",
xlab = "World Happiness Indicators",
ylab = "World Countries")
temp
#______________________________________--
heatmap <- all_data %>%
filter(Year == 2018) %>%
column_to_rownames(var = "Country") %>%
normalize() %>%
heatmaply(scale = 'none',
dist_method = 'euclidean',
hclust_method = 'ward.D',
seriate = 'OLO',
colors = Blues,
Colv=NA,
k_row = 5,
#k_col = 3,
#margins = c(NA,800,1000,NA),
fontsize_row = 4,
fontsize_col = 5,
#main="World Happiness Score and Variables by Country, 2018 \nDataTransformation using Normalise Method",
xlab = "Indicators",
ylab = "Countries") %>%
layout(height=800,width=1000)
#select(Country,HDI,Gender_Development_Index,Gender_Inequality_Index)
heatmap
##=______________________________________
dev.new()
scatter_plot <- ggplotly(all_data %>%
filter(Year == 2018) %>%
ggplot(aes(x = HDI,
y = Gender_Development_Index,
col = Region,
size = Total_GDP,
text = Country)) +
geom_point(alpha = 0.5) +
theme_classic()+
geom_text(aes(x = 0.5, y= 0.5, label = Year), size = 10, color = 'lightgrey',family = 'Oswald'))
ggplotly()
scatter_plot