-
Notifications
You must be signed in to change notification settings - Fork 0
/
Syria_Jordan_Flow.Rmd
210 lines (122 loc) · 6.15 KB
/
Syria_Jordan_Flow.Rmd
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
---
title: "Syria-Jordan_Flow"
output: html_document
---
## R Markdown
```{r}
# if you chose to pull directly from the site, use the below syntax for your specific country.
library(maptools)
library(raster)
library(ggplot2)
# grab some of the key shapefile we will need.
sy_border <- getData("GADM", country="SY", level=0)
sy_map <- getData("GADM", country="SY", level=1)
jo_border <- getData("GADM", country="JO", level=0)
jo_map <- getData("GADM", country="JO", level=1)
# take a look at the shapefiles we just pulled
plot(jo_map)
plot(sy_map)
```
```{r}
sy_map@data$NAME_1
```
## Read in Key files
```{r}
# this is how we load in a shapefile from our computer. This has a little more detail for internal admin borders
syria_border <- readRDS("C:/Users/john.mataya/Desktop/Datasets/Syria Data/spatial_data/SYR_adm2.rds")
library(readr)
Jordan <- read_csv("C:/Users/john.mataya/Desktop/Datasets/Syria Data/UNHRC/Custom_Camp/Syria_ref_in_Jordan_Origins.csv", col_types = cols(Lat.x = col_number(), Lat.y = col_number(), Lon.x = col_number(), Lon.y = col_number(), Refugees = col_number()))
#delete that row with nans - it isn't needed
Jordan <- Jordan[-c(15), ]
head(Jordan)
```
```{r}
# let's plot our data showing to and drom destinations
# plot the two countries together and use the curved arrows.
ggplot() + geom_polygon(data = sy_map, aes(long, lat, group=group), fill = 'whitesmoke') + geom_path(data=sy_map, aes(long,lat, group=group), color="grey", size=0.1) + geom_polygon(data = jo_border, aes(long, lat, group=group), fill = 'whitesmoke') + geom_curve(data = Jordan, aes(x = Lon.x, y = Lat.x, xend = Lon.y, yend = Lat.y, size = Refugees), curvature = -0.2, arrow = arrow(length = unit(0.01, 'npc')))
```
```{r}
sy_jordan <- read_csv("C:/Users/john.mataya/Desktop/Datasets/Syria Data/UNHRC/Custom_Camp/Syria_ref_in_Jordan_Origins.csv")
jo_df <- read_csv("C:/Users/john.mataya/Desktop/Datasets/Syria Data/UNHRC/Custom_Camp/Jordan_custom_camp.csv",
col_types = cols(Lat = col_number(),
Lon = col_number()))
sy_jordan$Destination <- NULL
sy_jordan <- sy_jordan[, -c(3:6)] # delete columns 5 through
sy_jordan <- sy_jordan[-c(15), ]
sy_jordan
```
And we can join our Refugee count data to the admin areas so that we can visualise it.
You can use the code below to join data from a csv to a shapefile, based on a common spatial value, here it is administrative boundaries, but it could be anything.
```{r}
sy_map@data$id <- rownames(sy_map@data)
# rename the columns in my sy_jordan dataframe to do the join
colnames(sy_jordan) <- c("NAME_1", "Refugees")
# perform the join
sy_map@data <- join(sy_map@data, sy_jordan, by="NAME_1")
# this step was necessary because for some reason the join was creating a single NA for the number of refgueess in Al-Hasakah provence. Here I'm manually putting in the number by selecting the df[row number, col number] = value
sy_map@data[1, 15] = 5129
syria_df <- fortify(sy_map)
syria_df <- join(syria_df, sy_map@data, by="id")
syria_df
```
```{r}
ggplot() + geom_polygon(data = syria_df, aes(x = long, y = lat, group = group, fill = Refugees), color = "black", size = 0.25)
```
```{r}
p <- ggplot() + geom_polygon(data = syria_df, aes(x = long, y = lat, group = group, fill = Refugees), color = "black", size = 0.25) + scale_fill_distiller(name="Refugees", direction = 1, palette = "Reds", breaks = pretty_breaks(n = 5)) +
labs(title="Origins of Syrian Refugees in Jordan")
p
```
```{r}
p <- ggplot() + geom_polygon(data = syria_df, aes(x = long, y = lat, group = group, fill = Refugees), color = "black", size = 0.25) + scale_fill_distiller(name="Refugees", direction = 1, palette = "Reds", breaks = pretty_breaks(n = 5)) +
labs(title="Origins of Syrian Refugees in Jordan")
p <- ggplot() + geom_polygon(data = syria_df, aes(x = long, y = lat, group = group, fill = Refugees), color = "black", size = 0.25) +
scale_fill_distiller(name="Refugees", direction = 1, palette = "Reds", breaks = pretty_breaks(n = 5)) +
labs(title="Origins of Syrian Refugees in Jordan") + geom_path(data=sy_border, aes(long,lat, group=group), size=1) +
geom_path(data=sy_map, aes(long,lat, group=group), color="grey", size=0.1) +
geom_polygon(data = jo_map, aes(long, lat, group=group), fill = 'whitesmoke') +
geom_path(data=jo_border, aes(long,lat, group=group), size=1) +
geom_path(data=jo_map, aes(long,lat, group=group), color="grey", size=0.1)
```
```{r}
# plot the two countries together and use the curved arrows.
p <- p + geom_curve(data = Jordan, aes(x = Lon.x, y = Lat.x, xend = Lon.y, yend = Lat.y), curvature = -0.2, arrow = arrow(length = unit(0.01, 'npc')))
```
```{r}
# let's add cities - pull the lat and lon from google maps
city <- c('Aleppo','Al-Hasaka', 'Homs', 'Damascus', 'Lattakia', 'Amman')
Lat <- c(36.1956095, 36.5044741, 34.7302927, 33.5074706, 35.549496, 31.8360368)
Lon <- c(37.0551996, 40.7085281, 36.6420627, 36.2478761, 35.911248, 35.6674449)
cities <- data.frame(city, Lat, Lon)
head(cities)
str(cities)
```
```{r}
# add the city names to the map
p + geom_point(data = cities, aes(x = Lon, y = Lat), size = 2, alpha = 0.5) + geom_text(data=cities, hjust=0.5, vjust=-0.5, aes(x=Lon, y=Lat, label=city), colour="black", size=3, alpha = 0.6)
```
```{r}
# just city points
p <- p + geom_point(data = cities, aes(x = Lon, y = Lat), size = 2, alpha = 0.5)
p
```
We can use this bit of code to clean up the map so it is ready to be used in publication type format.
```{r}
theme_opts<-list(theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
panel.background = element_blank(),
plot.background = element_blank(),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_blank()))
```
```{r}
p + theme_opts
```
Let's create a little bar chart of places in Jordan with Syrian Refugees
```{r}
```