-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatachoroplethmap.R
83 lines (63 loc) · 2.31 KB
/
datachoroplethmap.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
download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="world_shape_file.zip")
system("unzip world_shape_file.zip")
library(dplyr)
library(leaflet)
library(rgdal)
library(viridis)
# reading the downloaded geoJson
world_spdf=readOGR( dsn= getwd() , layer="TM_WORLD_BORDERS_SIMPL-0.3")
# Look at the info provided with the geospatial object
head(world_spdf@data)
# provided CSV file
gtdData <- read.csv("C:/Users/dell/Desktop/eventsGTDCountries.csv")
head(gtdData)
# creating subset to rename column
sb_gtd <- gtdData %>%
rename(NAME=country)
#sb_gtd
# merging the 2 datasets on NAME
world_spdf@data <- left_join(world_spdf@data, sb_gtd, by="NAME")
head(world_spdf@data)
# removing NA to 0
world_spdf@data$count[ which(is.na(world_spdf@data$count))] = 0
# range of data
# 0-10 60
# 10-50 29
# 50-100 9
# 100-1000 24
# 1000-7000 13
# 7000+ 1
# creating Palatte
# Create a color palette with handmade bins.
mybins=c(0,10,50,100,1000,7000,Inf)
pal = colorBin( palette="YlOrRd", domain=world_spdf@data$count, na.color="transparent", bins=mybins)
# creating Palatte ~ DEFAULT
# pal <- colorNumeric(palette="viridis", domain=world_spdf@data$count, na.color="transparent")
# creating popup
# popup_sb <- paste0("Attacks: ", as.character(world_spdf@data$count))
popup_sb <- paste0("Country: ", world_spdf@data$NAME,"<br/>", "Terror Attacks: ", round(world_spdf@data$count, 2), sep="") %>%
lapply(htmltools::HTML)
# creating th map plot
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView( lat=10, lng=0 , zoom=2) %>%
addPolygons(data=world_spdf,
fillColor=~pal(world_spdf@data$count),
fillOpacity=.9,
weight=.2,
smoothFactor=.2,
highlight = highlightOptions(
weight = 2,
color = "#666",
fillOpacity = 0.7,
bringToFront = TRUE),
label=popup_sb,
# popup=~popup_sb
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")) %>%
addLegend(pal = pal,
values = world_spdf@data$count,
position = "bottomleft",
title = "Terror Attacks<br />2014 - 2017")