This repository has been archived by the owner on Jan 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
214 lines (156 loc) · 4.76 KB
/
index.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
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
207
208
209
210
211
212
213
214
---
title: "Neo4j gRaphs"
author: 'Bea Hernández'
date: '`r Sys.Date()`'
output:
revealjs::revealjs_presentation:
css: rladies.css
highlight: pygments
transition: slide
fig_width: 6
fig_height: 4
---
## About
![](images/bea.JPG)
Data Scientist focused on finding patterns and relations between online consumers. She also co-organizes [R-Ladies Madrid](rladies.org) and is a member of the [NASADatanauts](https://open.nasa.gov/explore/datanauts/2017/fall/#datanaut-beatriz-hernandez).
Twitter / Github : @Chucheria
## About
1. About Graphs
2. About Neo4j data in R
3. About analyzing and visualizing Neo4j data in R
# Graphs + Neo4j
## Graphs + Neo4j {.title-h2}
**A graph G is a collection of entities (nodes) and the relationships (edges) that connect those entities**
**_G = {N, E}_**
```{r, include=FALSE}
# Connector
library(RNeo4j)
# Manipulate data
library(dplyr)
# Work with graphs
library(igraph)
# Visualize
library(ggplot2)
library(ggthemes)
library(visNetwork)
graph <- startGraph("http://localhost:7474/db/data/", username="neo4j", password="root")
```
```{r kevin, echo=FALSE}
q <- ("MATCH (a:Actor { name: 'Kevin Bacon' })-[r]-(m:Movie)
RETURN a.name AS from, m.title AS to, m.releaseDate AS epoch")
bacon <- cypher(graph, q) %>%
mutate(epoch = as.numeric(epoch),
year = format(as.POSIXct(epoch/1000, origin="1970-01-01"), format="%Y"),
recent = ifelse(year >= 2000, TRUE, FALSE))
ig <- graph_from_data_frame(bacon)
data <- toVisNetworkData(ig)
visNetwork(nodes = data$nodes, edges = data$edges, height = "500px")
```
## Graphs + Neo4j {.title-h2}
A graph database stores your data in a graph.
Tables and foreign keys are nodes and relationships.
- _What does math say?_
Graphs can be undirected
- _What does Neo4j say?_
You have to make directed relationships.
Forget about them when you make your queries.
## Graphs + Neo4j {.title-h2}
Nodes can have properties.
Relationships can have properties.
Node labels are the best. Gives us set advantages.
```{r properties}
q <- ("MATCH (n:Movie { title: 'Footloose' })
RETURN properties(n) AS properties")
properties <- cypherToList(graph, q)
purrr::map(properties, ~names(.x$properties))
```
# Neo4j + R
## Neo4j + R {.title-h2}
### Set your environment
```{r environment, eval=FALSE}
# Connector
library(RNeo4j)
# Manipulate data
library(dplyr)
# Work with graphs
library(igraph)
# Visualize
library(ggplot2)
library(ggthemes)
library(visNetwork)
## If you want to use the Grammar of graphics
# library(ggraph)
graph <- startGraph("http://localhost:7474/db/data/",
username="",
password="")
```
## Neo4j + R {.title-h2}
```{r most_movies}
q <- ("MATCH (a:Actor)-[:ACTS_IN]->(m:Movie)
RETURN m.genre AS genre, COUNT(m) AS movies
ORDER BY movies DESC
LIMIT 20")
most_movies <- cypher(graph, q)
```
## Neo4j + R {.title-h2}
```{r}
ggplot(most_movies, aes(reorder(genre, -movies), movies)) +
geom_bar(stat="identity") +
theme_tufte() +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1)) +
ggtitle("Most played genres")
```
## Neo4j + R {.title-h2}
```{r most_genres}
q <- ("MATCH (a:Actor)-[:ACTS_IN]->(m:Movie)
WHERE m.genre IN ['Comedy', 'Drama', 'Action']
RETURN a.name AS name, m.genre AS genre, COUNT(m) AS count
ORDER BY count DESC
LIMIT 30")
most_genres <- cypher(graph, q)
```
## Neo4j + R {.title-h2}
### Visualize
```{r}
ggplot(most_genres, aes(name, genre)) +
geom_tile(aes(fill = genre, alpha = count)) +
theme_tufte() +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1)) +
ggtitle("Actors with most movies by genre ")
```
# RStats
## RStats {.title-h2}
### Betweenness centrality
The betweenness centrality for each vertex is the number of these shortest paths that pass through the vertex. It represents the degree of which nodes stand between each other.
$$betweenness(v) = \sum_{x \neq y \in V} \frac{\sigma_{xy}(v)}{\sigma_{xy}}$$
```{r betweenness}
q <- ("MATCH (a1:Actor)-[:ACTS_IN]->(m:Movie),
(a2:Actor)-[:ACTS_IN]->(m)
RETURN a1.name AS from, a2.name AS to, COUNT(*) AS weight
ORDER BY weight DESC
LIMIT 10000")
betweenness <- cypher(graph, q)
ig <- graph_from_data_frame(betweenness)
```
## RStats {.title-h2}
### Betweenness centrality
```{r}
b <- betweenness(ig)
b[1:5]
```
## RStats {.title-h2}
### Betweenness centrality
```{r fig.height=5}
V(ig)$label <- NA
V(ig)$size = 4
V(ig)$color = "cyan"
plot(ig, edge.arrow.size = 0.1)
```
# Let's Kahoot!
## Come and play
[Kahoot](https://play.kahoot.it/#/k/cc1a145d-977c-4654-9e73-339018c13870)
![](images/kahoot.png)