Skip to content

Commit 85b9678

Browse files
Patrick SmithPatrick Smith
authored andcommitted
Created a visualization script for use with the output of the relational permutations.R script.
1 parent 4368881 commit 85b9678

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

Network graph visualizer.R

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
library(network)
2+
library(igraph)
3+
library(dplyr)
4+
library(intergraph)
5+
6+
# This funciton pulls the end vertex from the string
7+
parseTo<-function(a){
8+
if(nchar(a)>3){substr(a,4,4)}
9+
else{substr(a,3,3)
10+
}
11+
}
12+
13+
#This function pulls the relation type from the string
14+
parseRel<-function(a){
15+
if(nchar(a)>3){substr(a,2,3)}
16+
else{substr(a,2,2)
17+
}
18+
}
19+
20+
#This function outputs a color string for the edge based on the derivation level
21+
#This scheme should be red/green colorblind accesssible
22+
edgeColor<- function(a){
23+
case_when(
24+
a == "Directly Trained" ~ "Blue",
25+
a == "Mutually Entailed"~ "Brown",
26+
a == "Combinatorially Entailed" ~ "Dark Green"
27+
)
28+
}
29+
30+
#This takes the output from the relational permutation.R v0.1 script and
31+
# parses start and end vertexes and relational types, and assigns edge color attributes.
32+
# It filters for duplicate instances of edges prior to assigning to a new data frame.
33+
# The output is a data frame formatted such that each row is an edge description and
34+
# can be coerced into the network data structure.
35+
36+
relb%>%
37+
mutate(From = unlist(substr(Relation,1,1)),
38+
To=unlist(lapply(Relation, parseTo)),
39+
Relation_Type = unlist(lapply(Relation, parseRel)),
40+
.before=Derivation_Level)%>%
41+
mutate(edge_color = unlist(lapply(Derivation_Level, edgeColor)))%>%
42+
filter(!Relation_Type=="ku")%>% #use this to supress "ku" relations. Comment this line to show "ku" relations
43+
filter(!duplicated(Relation))->
44+
edgeList
45+
46+
#This does the same for the v0.2 script, no mutate required since the newer script does the parsing already
47+
x%>%
48+
#filter(!Relation_Type=="ku")%>% #use this to supress "ku" relations. Comment this line to show "ku" relations
49+
#filter(Relation_Type=="ku")%>%#use this to supress all but "ku" relations. Comment this line to show "ku" relations
50+
#filter(!substr(Derivation_Level,1,4)=="Comb")%>% #use this to supress combinatorially derived relations. Comment this line to show combinatorially derived relations
51+
filter(!duplicated(Relation))->
52+
edgeList
53+
54+
#This coerces the data frame output above, in the form of an edge list, into network data type.
55+
#
56+
57+
relTnet<- network(edgeList,
58+
matrix.type='edgelist',
59+
ignore.eval=FALSE,
60+
loops=TRUE,
61+
#directed = FALSE ensure the edges do not overlap. TRUE overlaps edges but provides arrows.
62+
directed = FALSE,
63+
#The line below should be multiple=FALSE.
64+
#That is currently erroring because the output from the relational permutations.R
65+
# file has multiple instances of the same two vertex with a
66+
# different relation (e.g. A<B & AkuB)
67+
# This may be a product of using an incoherent(not previously vetted for consistency) start list.
68+
# That may need to be explored independently.
69+
multiple = TRUE)
70+
71+
rel_graph<- asIgraph(relTnet)
72+
73+
plot(rel_graph,
74+
vertex.size=8,
75+
vertex.label=V(rel_graph)$vertex.names,
76+
edge.label=E(rel_graph)$Relation,
77+
edge.color=E(rel_graph)$edge_color,
78+
edge.arrow.size=1
79+
)

0 commit comments

Comments
 (0)