First, we load the required packages.
library(RNeo4j)
## Warning: package 'RNeo4j' was built under R version 3.2.4
library(visNetwork)
## Warning: package 'visNetwork' was built under R version 3.2.3
Next, we establish the connection to Neo4j and clear the previously existing data.
graph = startGraph("http://localhost:7474/db/data/", username = "neo4j", password = "cometha271")
clear(graph, input = F)
Now, we create all of the appropriate nodes with the apporpriate labels.
danny = createNode(graph, "Person", name = "Danny DeVito")
rhea = createNode(graph, "Person", name = "Rhea Perlman")
swank = createNode(graph, "Person", name = "Hillary Swank")
dempsey = createNode(graph, "Person", name = "Patrick Dempsey")
pam = createNode(graph, "Person", name = "Pam Ferris")
wisdom = createNode(graph, "Person", name = "Robert Wisdom")
ferrell = createNode(graph, "Person", name = "Will Ferrell")
black = createNode(graph, "Person", name = "Jack Black")
staunton = createNode(graph, "Person", name = "Imelda Staunton")
radcliffe = createNode(graph, "Person", name = "Daniel Radcliffe")
matilda = createNode(graph, "Movie", name = "Matilda")
freedom = createNode(graph, "Movie", name = "Freedom Writers")
beethoven = createNode(graph, "Movie", name = "Beethoven's Big Break")
harry1 = createNode(graph, "Movie", name = "Harry Potter and the Prisoner of Azkaban")
harry2 = createNode(graph, "Movie", name = "Harry Potter and the Order of the Phoenix")
harry3 = createNode(graph, "Movie", name = "Harry Potter and the Deathly Hallows")
cuckoo = createNode(graph, "Movie", name = "One Flew Over the Cuckoo's Nest")
anchor = createNode(graph, "Movie", name = "Anchorman")
oldschool = createNode(graph, "Movie", name = "Old School")
gulliver = createNode(graph, "Movie", name = "Gulliver's Travels")
lorax = createNode(graph, "Movie", name = "The Lorax")
panda = createNode(graph, "Movie", name = "Kung-Fu Panda")
paddington = createNode(graph, "Movie", name = "Paddington")
mars = createNode(graph, "Movie", name = "Mars Attacks!")
Now, we create the appropriate relations.
createRel(danny, "DIRECTED", matilda)
## < Relationship >
## DIRECTED
createRel(danny, "ACTED_IN", matilda)
## < Relationship >
## ACTED_IN
createRel(danny, "DIRECTED", freedom)
## < Relationship >
## DIRECTED
createRel(danny, "ACTED_IN", cuckoo)
## < Relationship >
## ACTED_IN
createRel(rhea, "ACTED_IN", matilda)
## < Relationship >
## ACTED_IN
createRel(rhea, "ACTED_IN", beethoven)
## < Relationship >
## ACTED_IN
createRel(swank, "ACTED_IN", freedom)
## < Relationship >
## ACTED_IN
createRel(dempsey, "ACTED_IN", freedom)
## < Relationship >
## ACTED_IN
createRel(pam, "ACTED_IN", matilda)
## < Relationship >
## ACTED_IN
createRel(pam, "ACTED_IN", harry1)
## < Relationship >
## ACTED_IN
createRel(wisdom, "ACTED_IN", freedom)
## < Relationship >
## ACTED_IN
createRel(ferrell, "ACTED_IN", anchor)
## < Relationship >
## ACTED_IN
createRel(ferrell, "ACTED_IN", oldschool)
## < Relationship >
## ACTED_IN
createRel(black, "ACTED_IN", anchor)
## < Relationship >
## ACTED_IN
createRel(black, "ACTED_IN", gulliver)
## < Relationship >
## ACTED_IN
createRel(black, "STARRED_IN", panda)
## < Relationship >
## STARRED_IN
createRel(danny, "STARRED_IN", lorax)
## < Relationship >
## STARRED_IN
createRel(staunton, "ACTED_IN", freedom)
## < Relationship >
## ACTED_IN
createRel(staunton, "ACTED_IN", harry2)
## < Relationship >
## ACTED_IN
createRel(staunton, "ACTED_IN", harry3)
## < Relationship >
## ACTED_IN
createRel(radcliffe, "ACTED_IN", harry1)
## < Relationship >
## ACTED_IN
createRel(radcliffe, "ACTED_IN", harry2)
## < Relationship >
## ACTED_IN
createRel(radcliffe, "ACTED_IN", harry3)
## < Relationship >
## ACTED_IN
createRel(staunton, "STARRED_IN", paddington)
## < Relationship >
## STARRED_IN
createRel(black, "ACTED_IN", mars)
## < Relationship >
## ACTED_IN
createRel(danny, "ACTED_IN", mars)
## < Relationship >
## ACTED_IN
createRel(danny, "MARRIED", rhea)
## < Relationship >
## MARRIED
Now we create the following node and edge queries and cypher them.
node_query = "
MATCH n
RETURN n.name AS id,
n.name AS label,
LABELS(n)[0] AS group
"
edge_query = "
MATCH (n) - [r] -> (m)
RETURN n.name AS from,
m.name AS to,
TYPE(r) AS label
"
nodes = cypher(graph, node_query)
edges = cypher(graph, edge_query)
nodes
## id
## 1 Danny DeVito
## 2 Rhea Perlman
## 3 Hillary Swank
## 4 Patrick Dempsey
## 5 Pam Ferris
## 6 Robert Wisdom
## 7 Will Ferrell
## 8 Jack Black
## 9 Imelda Staunton
## 10 Daniel Radcliffe
## 11 Matilda
## 12 Freedom Writers
## 13 Beethoven's Big Break
## 14 Harry Potter and the Prisoner of Azkaban
## 15 Harry Potter and the Order of the Phoenix
## 16 Harry Potter and the Deathly Hallows
## 17 One Flew Over the Cuckoo's Nest
## 18 Anchorman
## 19 Old School
## 20 Gulliver's Travels
## 21 The Lorax
## 22 Kung-Fu Panda
## 23 Paddington
## 24 Mars Attacks!
## label group
## 1 Danny DeVito Person
## 2 Rhea Perlman Person
## 3 Hillary Swank Person
## 4 Patrick Dempsey Person
## 5 Pam Ferris Person
## 6 Robert Wisdom Person
## 7 Will Ferrell Person
## 8 Jack Black Person
## 9 Imelda Staunton Person
## 10 Daniel Radcliffe Person
## 11 Matilda Movie
## 12 Freedom Writers Movie
## 13 Beethoven's Big Break Movie
## 14 Harry Potter and the Prisoner of Azkaban Movie
## 15 Harry Potter and the Order of the Phoenix Movie
## 16 Harry Potter and the Deathly Hallows Movie
## 17 One Flew Over the Cuckoo's Nest Movie
## 18 Anchorman Movie
## 19 Old School Movie
## 20 Gulliver's Travels Movie
## 21 The Lorax Movie
## 22 Kung-Fu Panda Movie
## 23 Paddington Movie
## 24 Mars Attacks! Movie
edges
## from to label
## 1 Danny DeVito Rhea Perlman MARRIED
## 2 Danny DeVito Mars Attacks! ACTED_IN
## 3 Danny DeVito The Lorax STARRED_IN
## 4 Danny DeVito One Flew Over the Cuckoo's Nest ACTED_IN
## 5 Danny DeVito Freedom Writers DIRECTED
## 6 Danny DeVito Matilda ACTED_IN
## 7 Danny DeVito Matilda DIRECTED
## 8 Rhea Perlman Beethoven's Big Break ACTED_IN
## 9 Rhea Perlman Matilda ACTED_IN
## 10 Hillary Swank Freedom Writers ACTED_IN
## 11 Patrick Dempsey Freedom Writers ACTED_IN
## 12 Pam Ferris Harry Potter and the Prisoner of Azkaban ACTED_IN
## 13 Pam Ferris Matilda ACTED_IN
## 14 Robert Wisdom Freedom Writers ACTED_IN
## 15 Will Ferrell Old School ACTED_IN
## 16 Will Ferrell Anchorman ACTED_IN
## 17 Jack Black Mars Attacks! ACTED_IN
## 18 Jack Black Kung-Fu Panda STARRED_IN
## 19 Jack Black Gulliver's Travels ACTED_IN
## 20 Jack Black Anchorman ACTED_IN
## 21 Imelda Staunton Paddington STARRED_IN
## 22 Imelda Staunton Harry Potter and the Deathly Hallows ACTED_IN
## 23 Imelda Staunton Harry Potter and the Order of the Phoenix ACTED_IN
## 24 Imelda Staunton Freedom Writers ACTED_IN
## 25 Daniel Radcliffe Harry Potter and the Deathly Hallows ACTED_IN
## 26 Daniel Radcliffe Harry Potter and the Order of the Phoenix ACTED_IN
## 27 Daniel Radcliffe Harry Potter and the Prisoner of Azkaban ACTED_IN
Finally we can visualize the entire network.
visNetwork(nodes,edges)
One advantage of storing the data in Neo4j is that we would be able to extract information in a quicker way and see relationships between the people and the different movies listed. We would also be able to visualize it in a network as shown above.