-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDS_Leaflet.Rmd
129 lines (109 loc) · 4.91 KB
/
IDS_Leaflet.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
---
title: "IDS_Leaflet"
author: "Krishnamoorthy Manohara and Wojciech Kuznicki"
output:
html_document:
theme: darkly
toc: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
##What are tilemaps?
It is a map rendered by the browser by individually querying for various vector images and seamlessly joining them.
Different layers of vectors can be overlayed over the map.
##Importing libraries
```{r}
library(legislatoR)
library(leaflet)
library(dplyr)
library(htmltools)
library(tidyverse)
```
##LegislatoR dataset
import the dataset
```{r}
usaCore <- get_core(legislature = "usa_house")
```
extract the latitude and longitude values of the birthplace and deathplace into separate columns
```{r}
usaCore$latBirth <- as.numeric(str_split_fixed(usaCore$birthplace, ",", n = 2)[,1])
usaCore$lngBirth <- as.numeric(str_split_fixed(usaCore$birthplace, ",", n = 2)[,2])
usaCore$latDeath <- as.numeric(str_split_fixed(usaCore$deathplace, ",", n = 2)[,1])
usaCore$lngDeath <- as.numeric(str_split_fixed(usaCore$deathplace, ",", n = 2)[,2])
usaDf <- usaCore |>
select(1:5, 13:16)
```
Create the tilemap, and add markers for each data point. lat and lng are required to position the marker; color sets different colours for the birth and death places; label allows us to display the name of the corresponding legislator when the user hovers the cursor over the marker; popup allows us to display the other variables when a marker is clicked; and group allows us to group the markers into separate layers so we can choose what to display using addLayersControl(), making the map interactive.
```{r}
leaflet(data = usaCore) |>
addTiles() |>
addCircleMarkers( lng = ~lngBirth,
lat = ~latBirth,
color = "red",
radius = 0.2,
label = ~name,
group = "Born in",
popup = ~paste0( "Name: ", name, "<br>",
"Sex: ", sex, "<br>",
"Ethnicty: ", ethnicity, "<br>",
"Religion: ", religion, "<br>")
) |>
addCircleMarkers( lng = ~lngDeath,
lat = ~latDeath,
color = "blue",
radius = 0.2,
label = ~name,
group = "Died in",
popup = ~paste0( "Name: ", name, "<br>",
"Sex: ", sex, "<br>",
"Ethnicty: ", ethnicity, "<br>",
"Religion: ", religion, "<br>")
) |>
addLayersControl(overlayGroups = c("Born in", "Died in"),
options = layersControlOptions(collapsed = FALSE))
```
##sieges dataset
In the second dataset, we display a map of some of the most important sieges in military history. We create a colour palette mapping a shade of colour to an year over the domain of the period of time encompassing the data points. We then set the colour of the marker to the one generated by the palette for the corresponding year. The size of the markers are variable, and is used to indicate the number of casualties in each siege. Labels give the war during which the siege took place.
```{r}
df1 <- read.csv("sieges.csv")
#A <- df1$Conflict
#A <- match(A, unique(A))
pal <- colorNumeric("plasma", domain = df1$Year)
leaflet(data = df1) |>
addTiles() |>
addCircleMarkers( lng = ~lon,
lat = ~lat,
color = ~pal(Year),
radius = ~(Casualties/100000),
label = ~as.character(Conflict)
)
```
##WWII dataset
In the third dataset, we display the targets of all the allied bombing raids during world war 2.We can create a pallette to use colour to indicate which nation carried out the attack, or how much ordinance was dropped, and set the other as a label.
```{r}
df2 <- read.csv("THOR_WWII_DATA_CLEAN.csv")
A <- df2$COUNTRY_FLYING_MISSION
A <- match(A, unique(A))
pal <- colorNumeric("inferno", domain = A)
pal2 <- colorNumeric("viridis", domain = df2$TOTAL_TONS)
leaflet(data = df2) |>
addTiles() |>
addCircleMarkers( lng = ~LONGITUDE,
lat = ~LATITUDE,
color = ~pal(A),
label = as.character(paste(df2$TOTAL_TONS), " tons of explosives"),
#clusterOptions = markerClusterOptions(),
weight = 1)
```
Since the number of markers is very large, we can use clustering to improve readability and load time. Clustering dynamically groups nearby markers based on the zoom level.
```{r}
leaflet(data = df2) |>
addTiles() |>
addCircleMarkers( lng = ~LONGITUDE,
lat = ~LATITUDE,
color = ~pal(A),
label = as.character(paste(df2$TOTAL_TONS), " tons of explosives"),
clusterOptions = markerClusterOptions(),
weight = 1)
```