-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotids.R
204 lines (184 loc) · 6.27 KB
/
plotids.R
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
library(DiagrammeR)
library(tidyverse)
#allb.names is the list of wikidata items generated by joining the SPARQL queries and filtering
#on unique wikidata ids
#generate the frequency tables for ids and intersects between different ids
st = select(allb.names,ipni_id,bhl_id,zoo_id,entom_id,harv_id,article,orcid,viaf,isni)[1,]
for (i in c(3:6,12:16)) {
z = dim(allb.names)[1] -
dim(allb.names[is.na(allb.names[,i]),i])[1]
print(paste0(colnames(allb.names)[i],": ",z))
st[1,colnames(st)==colnames(allb.names)[i]] = z
}
twoways = as_tibble(t(combn(seq(1:9),2))) #this is probably suboptimal, still to fix
twoways = twoways+2
twoways$n = NA
allb.ids = select(allb.names,item,itemLabel,ipni_id,bhl_id,zoo_id,entom_id,harv_id,article,orcid,viaf,isni)
for (i in 1:dim(twoways)[1]) {
z = dim(allb.ids[is.na(allb.ids[,twoways[i,1]])==F&
is.na(allb.ids[,twoways[i,2]])==F,3])[1]
print(paste0(colnames(allb.ids)[twoways[i,1]],
", ",
colnames(allb.ids)[twoways[i,2]],
": ",
z,
", ",
round(100*z/as.numeric(st[1,colnames(st)==colnames(allb.ids)[twoways[i,1]]]),2),
", ",
round(100*z/as.numeric(st[1,colnames(st)==colnames(allb.ids)[twoways[i,2]]]),2)))
twoways$n[i] = z
}
#create the lists for all filtered ids (not viaf, orcid, isni)
dots = st[1,c(1:6)]
dots[2,] = round(12*as.numeric(dots[1,])/sum(as.numeric(dots[1,])),2) #percentages
links.t = filter(twoways,V1<9,V2<9)
links.t$names = colnames(allb.ids)[links.t$V1]
links.t$names2 = colnames(allb.ids)[links.t$V2]
links.t$wd = round(12*as.numeric(links.t$n)/sum(as.numeric(dots[1,])),2)
#orcid, isni, viaf
dots.b = st
dots.b[2,] = round(12*as.numeric(dots.b[1,])/sum(as.numeric(dots.b[1,])),2) #percentages
links.b = filter(twoways,V1>8|V2>8)
links.b$names = colnames(allb.ids)[links.b$V1]
links.b$names2 = colnames(allb.ids)[links.b$V2]
links.b$wd = round(12*as.numeric(links.b$n)/sum(as.numeric(dots.b[1,])),2)
#selected links
links = links.t[-c(2,3,6,10,11,13),]
###
####functions here
###
dotGen <- function(name,label,
shape="circle",
fillcolor="green",
style="filled",
fixedsize="true",
width="1") {
dot = paste0("node_",name,
" [shape=",shape,
",fillcolor=\"",fillcolor,
"\",style=",style,
",label=\"",label,
"\",fixedsize=",fixedsize,
",width=\"",width,
"\"];")
return(dot)
}
schemGen <- function(name1,name2) {
schem = paste0("node_",
name1,
" -- node_",
name1,
"_",
name2,
" -- node_",
name2,
";\n")
return(schem)
}
graphGen <- function(dots,links,wdt = F,engine = "dot",sort=F) {
#wdt set to true if to use custom sizes
#engine to set the engine (here, because engine argument ignored in grViz function!)
#sort as TRUE to reorder the nodes and edges; doesn't work very well
if (wdt == F) {
dots[2,] = 1
links$wd = 0.5
}
dots.all = enframe(seq(1,length(dots)),name=NULL)
dots.all$id = seq(1,length(dots)*100,by=100)
for (i in 1:length(dots)) {
dots.all$value[i] = paste0(dotGen(
colnames(dots)[i],
paste0(colnames(dots)[i],
"\n",
dots[1,i]),
width=dots[2,i]),
"\n")
}
links.all = enframe(seq(1,dim(links)[1]),name=NULL)
links.all$id = NA
for (i in 1:dim(links)[1]) {
links.all$value[i] = paste0(dotGen(
paste0(links$names[i],"_",links$names2[i]),
paste0(links$names[i]," ",links$names2[i],"\n",links$n[i]),
shape="circle",
width=links$wd[i],
fillcolor="yellow"),
"\n")
links.all$id[i] = dots.all$id[grepl(links$names[i],dots.all$value)] +
dots.all$id[grepl(links$names2[i],dots.all$value)]
}
dots.all = rbind(dots.all,links.all)
schem.all = enframe(seq(1,dim(links)[1]),name=NULL)
for (i in 1:dim(links)[1]) {
schem.all$value[i] = schemGen(links$names[i],links$names2[i])
}
schem.all$id = links.all$id
if (sort==T) {
schem.all = arrange(schem.all,id)
dots.all = arrange(dots.all,id)
}
grap <- paste0('graph G {\n\n graph [layout = ',
engine,
']\n[overlap = false]\n\n',
paste(dots.all$value,collapse="\n"),
"\n\n",
paste(schem.all$value,collapse="\n"),
"\n}")
return(grap)
}
##Graphs for six source ids
#biggest intersects
try = graphGen(dots,links,wdt=T,engine="neato")
try = gsub("article","wikispecies",try)
try = gsub("_id","",try)
grViz(try)
#Rod's graph
links2 = links.t[-c(3,9,11,13:15),]
try = graphGen(dots,links2,wdt=T,engine="neato")
try = gsub("article","wikispecies",try)
try = gsub("_id","",try)
grViz(try)
#another graph
links3 = links2[c(1,5,2,3,7,8,6,9,4),]
try = graphGen(dots,links3,wdt=T,engine="neato")
try = gsub("article","wikispecies",try)
try = gsub("_id","",try)
grViz(try)
#every link (horriblespiderweb.png)
try = graphGen(dots,links.t,wdt=T,engine="neato")
try = gsub("article","wikispecies",try)
try = gsub("_id","",try)
grViz(try)
##graphs for orcid et al
#all
try = graphGen(dots.b,links.b,wdt=T,engine="neato")
try = gsub("article","wikispecies",try)
try = gsub("_id","",try)
grViz(try)
#bigger than 500
links.b2 = filter(links.b,n>500)
try = graphGen(dots.b,links.b2,wdt=T,engine="neato")
try = gsub("article","wikispecies",try)
try = gsub("_id","",try)
grViz(try)
#orcid onlu
links.orcid = filter(links.b,names2=="orcid",names!="viaf",names!="isni")
dots.orcid = select(dots.b,-isni,-viaf)
try = graphGen(dots.orcid,links.orcid,wdt=T,engine="neato")
try = gsub("article","wikispecies",try)
try = gsub("_id","",try)
grViz(try)
#isni only
links.isni = filter(links.b,names2=="isni",names!="viaf",names!="orcid")
dots.isni = select(dots.b,-orcid,-viaf)
try = graphGen(dots.isni,links.isni,wdt=T,engine="neato")
try = gsub("article","wikispecies",try)
try = gsub("_id","",try)
grViz(try)
#viaf only
links.viaf = filter(links.b,names2=="viaf",names!="isni",names!="orcid")
dots.viaf = select(dots.b,-orcid,-isni)
try = graphGen(dots.viaf,links.viaf,wdt=T,engine="neato")
try = gsub("article","wikispecies",try)
try = gsub("_id","",try)
grViz(try)