Column

Percentage of Refugees and Seekers

Number of Refugees and Seekers

Column

Refugees Origin

Number of Refugees and Seekers by Regions over Time

---
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 Source: http://popstats.unhcr.org/en/asylum_seekers

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)))

```

Column {data-width=500}
-----------------------------------------------------------------------

### Percentage of Refugees and 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 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)

```


### Number of Refugees and 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 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)
```


Column {data-width=500}
-----------------------------------------------------------------------

### 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 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 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)
```