-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metrorail.Rmd
74 lines (50 loc) · 2.21 KB
/
Metrorail.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
---
title: "Metrorail Chord Diagram"
author: "John"
date: "March 18, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
Quick overview on how to create a Chord Diagram. In this example we'll use data from Metrorail in Washington D.C.
Metro provides a number of different data sets on their website here: https://planitmetro.com/data
For this example you need data with an origin and a destination. Chord Diagrams are also famous for showing migration patterns between countries. Let's now get to the code.
For this Chord Diagram we're going to use R and the D3
Step 1 - read in your datatset
```{r}
library(networkD3)
library(igraph)
library(readr)
metro <- read_csv("C:/Users/John/Desktop/Data Science/transportation/metro.csv")
#explore the data a little bit using the summary commands and str commands #str(metro)
summary(metro)
```
Step 2 - you need to subset your data a little bit because it will have too many records for the Chord Diagram to work.
There's no magic number here, but start subsetting from the top quartile. Once we get to about 50-70 records, we can produce a manageable Chord Diagram that will show clear connections between boardings and exits.
```{r}
metro_sub <- metro[metro$Riders >= 550,]
```
Step 3 - organize your columns. In this case 1 represents your origin column and 2 represents your destination (exit) ###column. 3 represents your numeric data, in this case Riders.
```{r}
met <- metro_sub[c(1,2,3)]
```
Step 4 - let's transform things so we're working with a graph.
```{r}
nw <- graph.data.frame(met, directed=TRUE)
```
Step 5 - let's plot the graph and create the an adjacency matrix. The matrix is a key step for making a Chord Diagram
```{r}
plot(nw, edge.label = E(nw)$Riders)
```
Time to create our matrix
```{r}
ad <- as_adjacency_matrix(nw,type="both",names=TRUE,sparse=FALSE,attr="Riders");
V(nw)$name
```
Step 6 - let's plot our chord diagram.
```{r}
chordNetwork(Data = ad, width = 700, height = 700, fontSize = 14, labels = V(nw)$name, labelDistance = 100)
```
If you have to many or too few connections, go back to step two and adjust your subset to add or subtract how many stations (records) get included in the diagram.