---
title: "Dashboards World Bank Indicators"
author: "Shadi Henchiri"
output:
flexdashboard::flex_dashboard:
orientation: row
social: menu
source_code: embed
---
```{r setup, include=FALSE}
library(ggplot2)
library(plotly)
library(plyr)
library(flexdashboard)
library(sf)
library(htmlwidgets)
location="https://github.com/pubpolicy/PubPolicy-543/raw/main/"
file="gbdChildMortality_2010s.csv"
linkToFile=paste0(location,file)
gdb=read.csv(linkToFile)
location="https://github.com/pubpolicy/PubPolicy-543/raw/main/"
file="gbdChildMortality_Modified_2010s.csv"
linkToFile2=paste0(location,file)
gdb2=read.csv(linkToFile2)
linkCSV='https://github.com/pubpolicy/PubPolicy-543/raw/main/gbdChildMortality_2010s.csv'
dataCSV=read.csv(linkCSV)
location="https://github.com/pubpolicy/PubPolicy-543/raw/main/"
file="WB_countries_Admin0_10m.json"
linkToFile3=paste0(location,file)
library(sf)
mapWorld=read_sf(linkToFile3)
```
Static Plots
=======================================================================
Column {data-width=380}
-------------------------------------
### World Bank Regions and Under five mortality indicators
```{r,fig.height=13, fig.width=17}
library(reshape2)
names(gdb2)[names(gdb2)=="neoMR"]<- "Neonates"
names(gdb2)[names(gdb2)=="age1_5MR"]<- "Age 1 to 5 "
names(gdb2)[names(gdb2)=="postneoMR"]<- "Post Neonatles"
names(gdb2)[names(gdb2)=="under5MR"]<- "Under 5 years old"
gdbL=melt(gdb2, # all the data
id.vars = 'gbdRegion') # unique id per row
library(ggplot2)
base = ggplot(data = gdbL, aes(x = variable,
y =gbdRegion))
heat1= base + geom_tile(aes(fill = value))
#inverse color -1
heat2 = heat1 + scale_fill_gradient(low = 'grey',
high = "black")
# change in REORDER
base= ggplot(data = gdbL, aes(x = reorder(variable,
value, median),
y =reorder(gbdRegion,
value, median)))
plot7=base + geom_tile(aes(fill = value)) + labs(title = "Cases of Infants Mortality by World Bank Regions in 2010", caption ="Source :World Bank 2010") + scale_fill_gradient(low = 'grey90',high = "grey50") +
theme(legend.title=element_text(size=20),legend.text=element_text(size=17),plot.caption = element_text(hjust = 1.3, size = 20, face = "bold"),plot.title = element_text(hjust = 0.5, size = 27, face = "bold"), axis.text.x = element_text(angle = 0, hjust = 0.5,vjust = 0.5,size = 14,face = 'bold'),
axis.text.y = element_text(size = 18,face = 'bold'))+ labs(fill=' Infants Mortality cases in 2010 ',
x =NULL,
y = NULL)
plot7
```
Column {data-width=450}
-------------------------------------
### Distribution of World Bank Countries by Region
```{r, fig.height=10, fig.width=26}
# absolute values
absoluteT=table(gdb$gbdRegion,
exclude = 'nothing') #include all!
prop=prop.table(absoluteT)
ToPlot=prop*100
# as data frame
tableFreq=as.data.frame(ToPlot)
names(tableFreq)=c("Region","proportion")
library(ggplot2)
base= ggplot(data = tableFreq,
aes(x = Region,
y = proportion))+ theme_classic()
plot1 = base + geom_bar(fill ="blue",
stat = 'identity') +
coord_flip()
titleText='Distribution of countries by World Bank Regions'
sourceText='Source: World Bank'
plot2 = plot1 + labs(title=titleText,
x =NULL,
y = NULL,
caption = sourceText)
plot3 = plot2 + geom_hline(yintercept = 5.88, #where
linetype="dashed",
size=1, #thickness
alpha=0.5) #transparency
library(scales) # for "unit_format""
# customize Y axi
plot4 = plot3 + scale_y_continuous(breaks=c(0,2,4,6,8,10,12),
limits = c(0,12),
labels=unit_format(suffix = '%'))
plot5 = plot4 + theme(plot.caption = element_text(hjust = 1),
plot.title = element_text(hjust = 0.5))
P=paste0(round(tableFreq$proportion,2), '%')
LABELS=P
plot6 = plot5 + geom_text(vjust=0.5, #hjust if flipping
size = 3,
aes(y = proportion ,
label = LABELS))
tableFreq=tableFreq[order(tableFreq$proportion),]
regionOrd=tableFreq[order(tableFreq$proportion),'Region']
LABELS=paste0(round(tableFreq$proportion,2), '%')
base= ggplot(data = tableFreq,
aes(x = region,
y = proportion))
base= base + scale_x_discrete(limits=regionOrd)
base= base +theme_classic()
base= ggplot(data = tableFreq,
aes(x = Region,
y = proportion))
base = base + scale_x_discrete(limits=regionOrd)
plot1 = base + geom_bar(fill ="blue",
stat = 'identity') +
coord_flip()
titleText='Distribution of countries by World Bank Regions'
sourceText='Source: World Bank'
plot2 = plot1 + labs(title=titleText,
x =NULL,
y = NULL,
caption = sourceText)
plot3 = plot2 + geom_hline(yintercept = 5.88, #where
linetype="dashed",
size=1, #thickness
alpha=0.5) #transparency
plot4 = plot3 + scale_y_continuous(breaks=c(0,2,4,6,8,10,12),
limits = c(0,13),
labels=unit_format(suffix = '%'))
plot5 = plot4 + theme(plot.caption = element_text(hjust = 1,face = 'bold', size = 25),
plot.title = element_text(hjust = 0.5, size = 40))+ theme(axis.text.x = element_text(angle = 0,
hjust = 1,
size = 30, face = "bold"),
axis.text.y = element_text(size = 28, face = "bold"))
plot6 = plot5 + geom_text(vjust=0.5,hjust=0, #hjust if flipping
size = 10,
aes(y = proportion ,
label = LABELS))
plot6 #+ coord_flip() # wanna flip the plot?
```
### Map World Bank Economic Regions
```{r , fig.height=13, fig.width=27}
mapWorldVars=merge(mapWorld, #map first
dataCSV,
by.x='ISO_A3', by.y='iso')
names(mapWorld)[names(mapWorld)=="ECONMY"]<- "Economic Region"
library(ggplot2)
# plot original map
base=ggplot(data=mapWorld) + geom_sf(fill='grey90',
color=NA) + theme_classic()
titleText='Distribution of countries by World Bank Economy Classification'
sourceText='Source: World Bank 2010'
colMap= base + geom_sf(data=mapWorldVars,
aes(fill= POP_RANK),
color=NA) + labs(title=titleText,
x =NULL,
y = NULL,
caption = sourceText)+ theme(legend.title.align = 0.5,legend.title=element_text(margin = margin(t = 100),size=25, face = "bold"),legend.text=element_text(size=25, face = "bold"),plot.caption = element_text(hjust = 1,face = 'bold',size = 25),
plot.title = element_text(hjust = 1, size = 40,face = "bold"),legend.key.size = unit(2, "cm"))+ theme(axis.text.x = element_text(angle = 0,
hjust = 1,
size = 15, face = "bold"),
axis.text.y = element_text(size = 15, face = "bold"))
colMap
```
Interactive Plots
=======================================================================
row
-----------------------------------------------------------------------
```{r,fig.height=6, fig.width=10}
plot5 = plot4 + theme(plot.caption = element_text(hjust = 1,face = 'bold', size = 8),
plot.title = element_text(hjust = 0.5, size = 25))+ theme(axis.text.x = element_text(angle = 0,
hjust = 1,
size = 10, face = "bold"),
axis.text.y = element_text(size = 10, face = "bold"))
plot6 = plot5 + geom_text(vjust=0.5,hjust=-0.2, #hjust if flipping
size = 4,
aes(y = proportion ,
label = LABELS))
#+ coord_flip() # wanna flip the plot?
plot6%>%ggplotly()
```
row
-----------------------------------------------------------------------
```{r,fig.height=6, fig.width=10}
library(htmlwidgets)
base= ggplot(data = gdbL, aes(x = reorder(variable,
value, median),
y =reorder(gbdRegion,
value, median)))
plot10 =base + geom_tile(aes(fill = value)) + labs(title = "Cases of Infants Mortality by World Bank Regions in 2010", caption ="Source :World Bank 2010")
Plot11 = plot10 + scale_fill_gradient(low = 'grey90',high = "grey50")
plot12 = Plot11 + theme(legend.title=element_text(size=0),legend.text=element_text(size=0),plot.caption = element_text(hjust = 1.3, size = 7, face = "bold"),plot.title = element_text(hjust = 0.5, size = 11, face = "bold"), axis.text.x = element_text(angle = 0, hjust = 0.5,vjust = 0.5,size = 0,face = 'bold'),
axis.text.y = element_text(size = 8,face = 'bold'))
plot7= plot12 + labs(fill=' Infants Mortality cases in 2010 ',
x =NULL,
y = NULL)
plot7%>%ggplotly()
```