-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmap.R
219 lines (137 loc) · 4.27 KB
/
tmap.R
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
# tmap package, thematic maps can be generated
# install.packages("tmap")
# install.packages("devtools")
# library(devtools)
# install_github("username/repo")
library(tmap)
library(sf)
library(readr)
library(tidyverse)
library(tigris)
admin2 <- st_read("shapefiles/LGAs/LGAs.shp")
roads <- st_read("shapefiles/shapefiles/Accessibility Roads/All_new_roads.shp")
towns <- st_read("shapefiles/shapefiles/Accessibility Points/All_new_points.shp")
test <- read.csv("data.csv")
inso <- st_read("shapefiles/shapefiles/inso/inso.shp") %>% filter(inso$Location_3 == c("Borno", "Adamawa", "Yobe"))
admin2 <- st_read("shapefiles/LGAs/LGAs.shp")
data <- read.csv("shapefiles/shapefiles/partners/partners.csv") |>
# convert a csv table into a spacial object using st_as_sf() from the sf package
st_as_sf(
# Columns
coords = c("long", "lat"),
remove = FALSE,
# Projection
crs = 4326
)
tm_shape(admin2) +
tm_polygons() +
tm_text("ADM2_EN", size = 0.6) +
tm_shape(data) +
tm_dots(size = "partners_2" , col = "partners_2", palette = "PRGn" )
# Static map
tmap_mode("plot")
# interactive map
tmap_mode("view")
tm_shape(admin2)+
tm_fill("yellow"
) +
tm_borders(alpha = .5) +
tm_text("ADM2_EN", size = 0.6) +
tm_shape(shp = inso)+
tm_dots(alpha = 0.8, col = "Act_1", palette = "PRGn",
stretch.palette = TRUE, size = 0.07)
map <- tm_shape(admin2) +
# fill color
tm_fill("TARGET_FID",
title="individuals per sqm"
) +
# add boundaries
tm_borders(alpha = .5) +
#tm_polygons() boundaries + fill
# Add labels
tm_text("ADM2_EN", size = 0.6)+
# Create multiple polygons
#tm_facets(by="ADM1_EN") +
# remove box
tm_layout(frame = FALSE)
map
# Add multiple layers
# Polygon
tm_shape(admin2)+
tm_polygons(col = "purple")+
tm_add_legend(type = "fill" , labels = "State", col = "purple", title = "Legend")+
# Lines
tm_shape( shp = roads) +
tm_lines(col = "red", lty = "solid") +
tm_add_legend(type = "line", labels = "Roads", col = "red", lty = "solid") +
# Points
tm_shape(shp = towns) +
tm_dots(col = "green") +
tm_add_legend(type = "symbol", labels = "Town", col = "green", shape = 18)
# remove box
# tm_layout(frame = TRUE) +
# tm_legend(Position = c("left", "top"))
library(readr)
dtm <- read_csv("/Users/hany/Desktop/RLeaflet/data/dtm_data.csv")
library(sf)
adm2 <- st_read("/Users/hany/Desktop/adm2/nga_admbnda_adm2_osgof_20190417_WFP.shp")
library(tmap)
dtm_sf <- st_as_sf(dtm, coords = c("lon", "lat"), crs = 4326)
tm_shape(dtm_sf) +
tm_dots(size = "ind")
tm_shape(dtm_sf) +
tm_dots(alpha = 0.8, col = "ind", palette = "PRGn",
stretch.palette = TRUE,size = 0.01)
#point to polygon
library(spatialEco)
dtm_lga <- point.in.poly(dtm_sf,adm2)
dtmlgadf <-
as.data.frame(dtm_lga) # Creates the table to see how many individuals in each lga
library(dplyr)
ind_dtm <-
dtmlgadf %>%
filter(!is.na(ADM1_EN))%>%
group_by(ADM2_EN) %>%
summarise(tot_ind = sum(ind))
library(tigris)
dtm_pol <- geo_join(adm2,
ind_dtm,
"ADM2_EN",
"ADM2_EN",
how = "left") # Joins the rwi average to the LGA shapefile
#polygon map
tm_shape(dtm_pol) +
tm_fill("blue")
#add data - color
tm_shape(dtm_pol) +
tm_fill(col = "tot_ind")
#convert to density
tm_shape(dtm_pol) +
tm_fill(col = "tot_ind", convert2density=TRUE,
style="kmeans",
title="individuals by sqm")
#Add borders
tm_shape(dtm_pol) +
tm_fill(col = "tot_ind", convert2density=TRUE,
style="kmeans",
title="individuals by sqm")+
tm_borders(alpha = .5)
#Add text
tm_shape(dtm_pol) +
tm_fill(col = "tot_ind", convert2density=TRUE,
style="kmeans",
title="individuals by sqm")+
tm_borders(alpha = .5) +
tm_text("ADM2_EN",
size = 0.4)
#Add facets
tm_shape(dtm_pol) +
tm_fill(col = "tot_ind", convert2density=TRUE,
style="kmeans",
title="individuals by sqm")+
tm_borders(alpha = .5) +
tm_text("ADM2_EN",
size = 0.4) +
tm_facets(by="ADM1_EN") +
tm_layout(legend.show = FALSE,
outer.margins=0)