-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-initial-leaflet.R
85 lines (71 loc) · 2.61 KB
/
03-initial-leaflet.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
# visualization
library(ggplot2)
library(spatstat)
library(plotrix)
library(fields)
library(leaflet)
library(maptools)
library(RColorBrewer)
library(lattice)
library(geoR)
library(plotrix)
library(RcppArmadillo)
# spatial data management and point process analysis
library(sp)
library(gtools)
# point processes
library(spatstat)
library(splancs) # K-function
library(smacpod) # Spatial scanning statistic
library(car) # contains a function for logistic transformation (log odds ratio) to make more normal
library(leaflet)
library(tidyverse)
library(shiny)
library(xts)
# read in csv
acled <- read.csv("1900-01-01-2020-05-06-Sudan.csv", sep = ";") # Sudan
# select for year 2005+ (after CPA was signed), filter, and mutate
acled_sub <- acled %>% select(year, event_type, admin2, fatalities, longitude, latitude) %>%
filter(year >= 2005) %>%
mutate(i_extreme = ifelse(event_type %in% "Battles" |
event_type %in% "Explosions/Remote violence" |
event_type %in% "Violence against civilians",
1, 0),
i_extreme_label = ifelse(event_type %in% "Battles" |
event_type %in% "Explosions/Remote violence" |
event_type %in% "Violence against civilians",
'Extreme violence', 'Non-extreme violence'))
# ui
ui <- fluidPage(
titlePanel("Mapping Violence in Post-War Sudan, Source: ACLED"),
sliderInput("year",
label = "Years 2005 to 2020",
min = min(acled_sub$year),
max = max(acled_sub$year),
value = max(acled_sub$year),
step=1,
animate = animationOptions(interval = 1400)),
leafletOutput("mymap")
)
# server
server <- function(input, output, session) {
df <- reactive({
acled_sub %>%
filter(year %in% input$year)
})
output$mymap <- renderLeaflet({
pal = colorFactor("YlOrRd", acled_sub$i_extreme_label, reverse=T)
leaflet() %>%
setView(lat = 15.898457, lng = 30.392880, zoom = 5) %>%
addProviderTiles(providers$Stamen.Toner,
options = providerTileOptions(opacity = .4)) %>%
addCircleMarkers(data = df(), lng = ~longitude, lat = ~latitude,
fillOpacity=0.3, fillColor = ~pal(i_extreme_label),
radius=8, weight=0.1, stroke=TRUE
) %>%
leaflet::addLegend("bottomright", pal = pal,
values = acled_sub$i_extreme_label,
title = "Violence Type")
})
}
shinyApp(ui, server)