Refugee Types

Column

Number of Refugees and Asylum Seekers

Percentage of Refugees and Asylum Seekers

Refugee Origins

Column

Refugees Origin

Number of Refugees and Asylum Seekers by Regions over Time

Refugees Migration Map

Asylum Seekers Migration Map

---
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: columns
    vertical_layout: fill
    social: menu
    source_code: embed
---

```{r data, include=FALSE}
setwd("C:\\Users\\Hannah\\Desktop\\Assignment\\ANLY512\\Project")
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
top5 = c()
top5_nation = c()

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)))
  top = subdata[order(subdata$Refugees, decreasing = TRUE),]
  top5 = cbind(top5, append(top$Refugees[1:5],sum(top$Refugees[-(1:5)])))
  top5_nation = cbind(top5_nation, append(top$Origin[1:5],'others'))
  colnames(top5)[idx] = y
  colnames(top5_nation)[idx] = y
  idx = idx +1
  top =
  for(j in unique(data$region)){
    RR = append(RR, sum(data[data$region == j,]$Refugees))
  }
}

rregions = rep(unique(data$region),length(unique(data$Year)))

```


Refugee Types
=======================================================================
Column {.tabset data-width=450}
-----------------------------------------------------------------------

### Number of Refugees and Asylum Seekers

```{r}
# Dashbord packages
library(plotly)
library(plyr)
library(flexdashboard)

# plot figures
library(ggthemes)
library(ggplot2)
library(scales)

types[types == "seekers"] <- "Seekers"

# refugees and seekers (stacked barplot)
plot1<-ggplot(data.frame(years,types,value), aes(fill=types, y=value, x=years)) +
  geom_bar( stat="identity")+ 
  labs(title = "Number of Refugees and Asylum Seekers", x="Years", y="Numbers") + 
  theme_economist() + scale_fill_discrete(name = "Type")+
  theme(plot.title = element_text(hjust = 0.5), legend.title=element_text(size=14))+
  scale_y_continuous(labels = comma)
ggplotly(plot1)

```


### Percentage of Refugees and Asylum Seekers
```{r figure 2}
# refugees and seekers (stacked Percent barplot)
plot2<-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 Asylum Seekers", x="Years", y="Percentage of Incidents") + 
  scale_y_continuous(labels = function(x) paste0(abs(x)*100, "%"))+
  theme_economist() + 
  scale_fill_discrete(name = "Type")+
  theme(plot.title = element_text(hjust = 0.5), legend.title=element_text(size=14))
ggplotly(plot2)
```

Refugee Origins
=======================================================================

Column {.tabset data-width=550}
-----------------------------------------------------------------------

### Refugees Origin
```{r figure 3}
# Refugees origin (group barplot)
rregions[rregions == "others"] <- "Others"

plot3<-ggplot(data.frame(years6,rregions,RR), aes(fill=rregions, y=RR, x=years6)) + 
  geom_bar(position="dodge", stat="identity")+
  labs(title = "Refugees Origin", x="Years", y="Numbers of Refugees") + 
  theme_economist() + scale_fill_discrete(name = "Origin Region")+
  scale_y_continuous(labels = comma)+
  theme(plot.title = element_text(hjust = 0.5), legend.title=element_text(size=14))
ggplotly(plot3)
```


```{r data for figure 4, , include=FALSE}
datar<-as.data.frame(cbind(data$Year, data$region, data$Refugees))
colnames(datar)=c("Year","Region","Refugees")
datas<-as.data.frame(cbind(data$Year, data$region, data$seekers))
colnames(datas)=c("Year","Region","Seekers")

datar_sum<-as.data.frame(aggregate(.~Year+Region, datar, sum))
datas_sum<-as.data.frame(aggregate(.~Year+Region, datas, sum))

n<-nrow(datar_sum)
datar_sum$Group = c(rep("Refugees", n))
datas_sum$Group = c(rep("Seekers", n))
            
colnames(datar_sum)=c("Year", "Region", "Number", "Group")
colnames(datas_sum)=c("Year", "Region", "Number", "Group")

data_sum<-rbind(datar_sum, datas_sum)
```


### Number of Refugees and Asylum Seekers by Regions over Time
```{r figure 4}
datar<-as.data.frame(cbind(data$Year, data$region, data$Refugees))
colnames(datar)=c("Year","Region","Refugees")
datas<-as.data.frame(cbind(data$Year, data$region, data$seekers))
colnames(datas)=c("Year","Region","Seekers")

datar_sum<-as.data.frame(aggregate(.~Year+Region, datar, sum))
datas_sum<-as.data.frame(aggregate(.~Year+Region, datas, sum))

n<-nrow(datar_sum)
datar_sum$Group = c(rep("Refugees", n))
datas_sum$Group = c(rep("Seekers", n))
            
colnames(datar_sum)=c("Year", "Region", "Number", "Group")
colnames(datas_sum)=c("Year", "Region", "Number", "Group")

data_sum<-rbind(datar_sum, datas_sum)

plot4<-ggplot(data_sum, aes(Year, Number, group=Group, colour=Group))+
  geom_line() + 
  geom_point()+
  facet_wrap(~Region, strip.position = "bottom")+  
  xlab("Years") + ylab("Numbers") + 
  ggtitle("Number of Refugees and Asylum Seekers by Regions over Time") + 
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(axis.text.x = element_text(size = 7))

ggplotly(plot4)
```


###Refugees Migration Map

```{r figure 5}

library(maps)
library(geosphere)


xlim <- c(-170, 170)
ylim <- c(-70, 90)
map("world", col="#191919", fill=TRUE, bg="gray", lwd=0.05, xlim=xlim, ylim=ylim)
US = c(-95.712891, 37.09024)
year = 2017
subdata = data[data$Year == year,]

# Color
pal = colorRampPalette(c("#333333", "white", "#1292db"))
colors = pal(100)

max5 = tail(sort(subdata$Refugees),5)[1]

for (i in 1:length(subdata$Origin)) {
  
  cnt = min(subdata$Refugees[i], max5)
  inter = gcIntermediate(c(subdata$long[i], subdata$lat[i]), US, n=1000, addStartEnd=TRUE, breakAtDateLine = TRUE)
  colindex = round( (cnt / max5) * 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)  
  }
}
```

###Asylum Seekers Migration Map
```{r figure 6}
xlim <- c(-170, 170)
ylim <- c(-70, 90)
map("world", col="#191919", fill=TRUE, bg="gray", lwd=0.05, xlim=xlim, ylim=ylim)
US = c(-95.712891, 37.09024)
subdata = data[data$Year == year,]

# Color
pal = colorRampPalette(c("#333333", "white", "#1292db"))
colors = pal(100)

max5 = tail(sort(subdata$seekers),5)[1]

for (i in 1:length(subdata$Origin)) {
  
  cnt = min(subdata$seekers[i], max5)
  inter = gcIntermediate(c(subdata$long[i], subdata$lat[i]), US, n=1000, addStartEnd=TRUE, breakAtDateLine = TRUE)
  colindex = round( (cnt / max5) * 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)  
  }
}
```