---
title: "ANLY 512 Dashboard Laboratory: Global Refugee Crisis - United States Region"
author: "by Yuan, Xin, Yun - Chia Lo, Jingwen Nie"
date: "`r Sys.Date()`"
output:
flexdashboard::flex_dashboard:
orientation: rows
source_code: embed
---
```{r data, include=FALSE}
data = read.csv('./USA_RefugeesSeekers.csv', colClasses = c("Year"="numeric",
"Country"="character",
"Origin"="character",
"Refugees"="numeric",
"seekers"="numeric"))
country = read.csv('./all.csv',colClasses = c("name" = "character",
"alpha2" = "character",
"region" = "character",
"subregion" = "character"))
coord = read.csv('./coodinate.csv',colClasses = c("country" = "character",
"latitude" = "numeric",
"longtitude" = "numeric"))
region = c()
subregion = c()
long = c()
lat = c()
for (i in data$Origin){
if (i %in% country$name) {
region = append(region,country[country$name == i,]$region)
subregion = append(subregion,country[country$name == i,]$subregion)
long = append(long, coord[coord$country == country[country$name == i,]$alpha2,]$longtitude)
lat = append(lat, coord[coord$country == country[country$name == i,]$alpha2,]$latitude)
}
else{
region = append(region, 'others')
subregion = append(subregion, 'others')
long = append(long, 0)
lat = append(lat, 0)
}
}
data = cbind(cbind(data,region),subregion)
data = cbind(cbind(data,long),lat)
data$region = as.character(data$region)
data$subregion = as.character(data$subregion)
years = c()
years6 = c()
types = c()
value = c()
RR = c() # region refugee
rtop5 = c()
rtop5_nation = c()
RS = c() # region seekers
stop5 = c()
stop5_nation = c()
SRR = c() # sub-region refugee
SRS = c() # sub-region seekers
idx = 1
for(y in unique(data$Year)){
years = append(years,rep(y , 2))
years6 = append(years6,rep(y , 6))
subdata = data[data$Year == y,]
types = append(types,c('Refugees','seekers'))
value = append(value, c(sum(subdata$Refugees), sum(subdata$seekers)))
rtop = subdata[order(subdata$Refugees, decreasing = TRUE),]
rtop5 = cbind(rtop5, append(rtop$Refugees[1:5],sum(rtop$Refugees[-(1:5)])))
rtop5_nation = cbind(rtop5_nation, append(rtop$Origin[1:5],'others'))
stop = subdata[order(subdata$seekers, decreasing = TRUE),]
stop5 = cbind(stop5, append(stop$seekers[1:5],sum(stop$seekers[-(1:5)])))
stop5_nation = cbind(stop5_nation, append(stop$Origin[1:5],'others'))
colnames(rtop5)[idx] = y
colnames(rtop5_nation)[idx] = y
colnames(stop5)[idx] = y
colnames(stop5_nation)[idx] = y
idx = idx +1
for(j in unique(data$region)){
RR = append(RR, sum(data[data$region == j,]$Refugees))
RS = append(RS, sum(data[data$region == j,]$seekers))
}
for(j in unique(data$subregion)){
SRR = append(SRR, sum(data[data$subregion == j,]$Refugees))
SRS = append(SRS, sum(data[data$subregion == j,]$seekers))
}
}
rregions = rep(unique(data$region),length(unique(data$Year)))
rsubregions = rep(unique(data$subregion),length(unique(data$Year)))
rtop5 = rtop5/1000
```
```{r}
library(plotly)
library(plyr)
library(flexdashboard)
library(ggthemes)
library(scales)
library(maps)
library(geosphere)
```
Row {data-height=650}
---------------------------------------------------------
### 2017 Refugees migration map
```{r,echo = FALSE, message = FALSE,fig.width=12}
year = 2017
xlim <- c(-170, 170)
ylim <- c(-70, 90)
p1 = map("world", col="#1241a1", fill=TRUE, border = NA, bg="#eeeeee", lwd=0.005, xlim=xlim, ylim=ylim)
US = c(-95.712891, 37.09024)
subdata = data[data$Year == year,]
pal = colorRampPalette(c("#000000", "white"))
colors = pal(100)
max10 = tail(sort(subdata$Refugees),10)[1]
for (i in 1:length(subdata$Origin)) {
cnt = min(subdata$Refugees[i], max10)
inter = gcIntermediate(c(subdata$long[i], subdata$lat[i]), US, n=1000, addStartEnd=TRUE, breakAtDateLine = TRUE)
colindex = round( (cnt / max10) * length(colors) )
if (length(inter) == 2){
lines(inter[[1]], col=colors[colindex], lwd=0.6)
lines(inter[[2]], col=colors[colindex], lwd=0.6)
}
else{
lines(inter, col=colors[colindex], lwd=0.6)
}
}
```
### 2017 Top 5 Refugees origin Countries
```{r,echo = FALSE, message = FALSE,fig.width=8}
year = 2017
plotidx = year-2012
ggplot(data.frame(rtop5[,plotidx],rtop5_nation[,plotidx]), aes(x = 'numbers', y =rtop5[,plotidx], fill = rtop5_nation[,plotidx])) +
geom_bar(width = 1, stat = "identity") +
labs(title = paste("[Refugees] Top 5 Countries in",as.character(year)), x="Numbers(K)", y="")+
coord_polar(theta = "y") +scale_fill_brewer(palette="Blues")+
theme_minimal()+ scale_fill_discrete(name = "Country")
```
Row{.tabset .tabset-fade}
-----------------------------------------------------------------------
### Refugees Origin Region
```{r,echo = FALSE, message = FALSE,fig.width=20}
ggplot(data.frame(years6,rregions,RR), aes(fill=rregions, y=RR, x=years6)) +
geom_bar(position="dodge", stat="identity")+
labs(title = "Refugees Origin Region", x="Years", y="Refugees Numbers") +
theme_economist() + scale_fill_discrete(name = "Origin Region")
```
### Asylum Seekers Origin Region
```{r,echo = FALSE, message = FALSE,fig.width=20}
ggplot(data.frame(years6,rregions,RS), aes(fill=rregions, y=RS, x=years6)) +
geom_bar(position="dodge", stat="identity")+
labs(title = "Seekers Origin Region", x="Years", y="Seekers Numbers") +
theme_economist() + scale_fill_discrete(name = "Origin Region")
```
### Refugees Origin - Sub-Region
```{r,echo = FALSE, message = FALSE,fig.width=20}
ggplot(data.frame(years6,rsubregions,SRR), aes(fill=rsubregions, y=SRR, x=years6)) +
geom_bar(position="dodge", stat="identity")+
labs(title = "Refugees Origin Sub-Region", x="Years", y="Refugees Numbers") +
theme_economist() + scale_fill_discrete(name = "Origin Sub-Region")
```
### Asylum Seekers Origin - Sub-Region
```{r,echo = FALSE, message = FALSE,fig.width=20}
ggplot(data.frame(years6,rsubregions,SRS), aes(fill=rsubregions, y=SRS, x=years6)) +
geom_bar(position="dodge", stat="identity")+
labs(title = "Seekers Origin Sub-Region", x="Years", y="Seekers Numbers") +
theme_economist() + scale_fill_discrete(name = "Origin Sub-Region")
```
### Refugees and Asylum Seekers (stacked barplot)
```{r,echo = FALSE, message = FALSE,fig.width=12}
ggplot(data.frame(years,types,value), aes(fill=types, y=value, x=years)) +
geom_bar( stat="identity")+
labs(title = "Percentage of Refugees and seekers", x="Years", y="Refugees Numbers") +
theme_economist() + scale_fill_discrete(name = "Type")
```
### Refugees and Asylum Seekers (stacked Percent barplot)
```{r,echo = FALSE, message = FALSE,fig.width=12}
ggplot(data.frame(years,types,value), aes(fill=types, y=value, x=years)) +
geom_bar( stat="identity", position="fill")+
labs(title = "Percentage of Refugees and seekers", x="Years", y="Percentage of Incidents") +
theme_economist() + scale_fill_discrete(name = "Type")
```