Explain why you selected to map the co-authors or start with the two-mode form and the get the co-authors vial network transformation:
I decided to start with the two-mode network connecting authors through their publications, and then retain the authors information to form the one-mode network. This is because after initial data cleaning I found there are quite a number of solo authors in the dataset, and I did not want to leave out those authors’ information from the network to be plotted. Transforming a two-mode network to a one-mode network can avoid losing those solo authors, hence making the graph more complete.
Attributes at the actor level:
author name
author ID
number of publications
Attributes at the connection level:
Significance:
Plotting a co-authorship network graph can help us visualize who the more central authors are in this topic of “educational technology usage in classroom”. From the network rendered, we can see that most authors do not produce more than one article in this field, implying this is a relatively less investigated topic in education, hence may have more potential in the future. Those represented by a dot with a non-blue color, who have more than one publication, also stand out from the rest, making it easier for us to locate these authors and see their connections with other authors. We can also notice from the graph that the co-publication connections are mostly limited to that one paper that several authors co-wrote, since the “co-authorship groups” (those small groups of connected authors) are scattered and mostly isolated from one another, as opposed to forming a large, interconnected web.
source <- read.csv("/Users/joyceshuai/Desktop/SSNA/classtech_tenyrs.csv")
# clean the data, drop the rows where authorsID is not available
source <- source[source$Author.s..ID!="[No author id available]",]
###solo authors do exist in this dataset, so build a two-mode network first###
#Creating adjacency list from authors' IDs
df_id <- as.data.frame(source[,3])
colnames(df_id) <- "ID"
library(splitstackshape)
#split IDs in the column into separate columns
id_split <- cSplit(df_id, splitCols = "ID", sep = ";", direction = "wide", drop = TRUE)
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
#turn id_split into matrix form
mat_id_split <- as.matrix(id_split)
dim(mat_id_split)
## [1] 154 11
#attach each paper's ID (EID) to its authors' IDs in the same row
eid_id <- cbind(source$EID, mat_id_split)
mat_eid_id <- as.matrix(eid_id)
#cbind the EID in each row with authors' IDs in the same row, one by one, into an edgelist
edgelist_two_mode <- cbind(mat_eid_id[, 1], c(mat_eid_id[, -1]))
#drop rows without authors
edgelist_two_mode <- edgelist_two_mode[!is.na(edgelist_two_mode[,2]), ]
dim(edgelist_two_mode)
## [1] 436 2
# plot the two-mode edgelist into a graph, with the 2nd column(authorIDs) sending connections to the 1st column(paper EIDs), let column 2 select column1 for the connection, i.e. authors selecting papers
library(igraph)
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
g2 <- graph.edgelist(edgelist_two_mode[, 2:1], directed = FALSE)
g2
## IGRAPH 154ea76 UN-- 579 436 --
## + attr: name (v/c)
## + edges from 154ea76 (vertex names):
## [1] 57209211014--2-s2.0-85118353799 57209977398--2-s2.0-85069208777
## [3] 57206698047--2-s2.0-85120980960 57222036217--2-s2.0-85130859202
## [5] 57672771100--2-s2.0-85129722779 56285357900--2-s2.0-85092481468
## [7] 8247022600 --2-s2.0-85141740589 57204936425--2-s2.0-85132312213
## [9] 35409037500--2-s2.0-85094896026 50261568800--2-s2.0-85058508859
## [11] 58044794200--2-s2.0-85146963015 7004594995 --2-s2.0-85044028507
## [13] 57198347616--2-s2.0-85036472155 57194013851--2-s2.0-85109103540
## [15] 57195758433--2-s2.0-85029746212 57222635157--2-s2.0-85149058232
## + ... omitted several edges
# to distinguish eids and ids in the edges, create a new attribute called type
V(g2)$type <- V(g2)$name %in% edgelist_two_mode[ , 1]
# count the number of Trues (EIDs) and Falses (authorIDs) in the type attribute
table(V(g2)$type)
##
## FALSE TRUE
## 425 154
# Create an adjacency list of authors' names
df_au <- as.data.frame(source$Authors)
colnames(df_au) <- "AU"
#separate authors' names into each column
au_split <- cSplit(df_au, splitCols = "AU", sep = ";", direction = "wide", drop = TRUE)
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
#trim the whitespace
au_split <- data.frame(lapply(au_split, trimws), stringsAsFactors = FALSE)
#turn au_split into matrix form
mat_au_split <- as.matrix(au_split)
dim(mat_id_split)
## [1] 154 11
#Decompose authors and IDs and put them into a dataframe with 2 columns
df_id_au_unlisted <- data.frame(ID = unlist(id_split), Names = unlist(au_split))
#drop the rows where the "ID" is NA, i.e. those with no more co-authors
df_id_au_unlisted <- df_id_au_unlisted[!is.na(df_id_au_unlisted$ID),]
#drop duplicates in the column "id" so that we don't count the same authors multiple times
df_id_au_unlisted <- df_id_au_unlisted[!duplicated(df_id_au_unlisted$ID),]
dim(df_id_au_unlisted)
## [1] 425 2
#count the number of publications by each author
pub_count <- as.data.frame(table(unlist(id_split)))
###Transform the two-mode network to one-mode to retain actors###
# get.incidence retrieves the matrix of the graph
mat_g2_incidence <- get.incidence(g2)
dim(mat_g2_incidence)
## [1] 425 154
# multiply to retain actors in a one-mode network
mat_g2_to_1 <- mat_g2_incidence%*%t(mat_g2_incidence)
dim(mat_g2_to_1)
## [1] 425 425
diag(mat_g2_to_1)<-0
# a weighted matrix to count for the number of co-publications
g <- graph.adjacency(mat_g2_to_1, mode = "undirected", weighted = TRUE, diag=F)
g
## IGRAPH b44fbdd UNW- 425 589 --
## + attr: name (v/c), weight (e/n)
## + edges from b44fbdd (vertex names):
## [1] 57209211014--55505853200 57209211014--57320369800 57206698047--36154796800
## [4] 57206698047--57685428900 57222036217--57202216646 57222036217--57203923749
## [7] 57672771100--57194830879 56285357900--56575051400 8247022600 --6507838357
## [10] 57204936425--57200916290 57204936425--57750635500 35409037500--16237824500
## [13] 50261568800--57205094161 58044794200--57209485440 58044794200--39362289600
## [16] 58044794200--55560959100 7004594995 --7006464833 7004594995 --7006544785
## [19] 57198347616--57190220967 57198347616--57198345447 57198347616--24173606100
## [22] 57198347616--57685148200 57198347616--57204127021 57198347616--57203895162
## + ... omitted several edges
#attach the authors' names to their IDs in the graph
V(g)$author <- df_id_au_unlisted$Names[match(V(g)$name, df_id_au_unlisted$ID)]
# the following code returns the number of publications as True, because only actor vertices have labels
table(is.na(V(g)$author))
##
## FALSE
## 425
g
## IGRAPH b44fbdd UNW- 425 589 --
## + attr: name (v/c), author (v/c), weight (e/n)
## + edges from b44fbdd (vertex names):
## [1] 57209211014--55505853200 57209211014--57320369800 57206698047--36154796800
## [4] 57206698047--57685428900 57222036217--57202216646 57222036217--57203923749
## [7] 57672771100--57194830879 56285357900--56575051400 8247022600 --6507838357
## [10] 57204936425--57200916290 57204936425--57750635500 35409037500--16237824500
## [13] 50261568800--57205094161 58044794200--57209485440 58044794200--39362289600
## [16] 58044794200--55560959100 7004594995 --7006464833 7004594995 --7006544785
## [19] 57198347616--57190220967 57198347616--57198345447 57198347616--24173606100
## [22] 57198347616--57685148200 57198347616--57204127021 57198347616--57203895162
## + ... omitted several edges
#Get centrality measures
cent<-data.frame(bet=betweenness(g, normalized=T, directed = FALSE)/max(betweenness(g, normalized=T, directed = FALSE)),eig=evcent(g)$vector, degree=degree(g, mode="total"))
cent$au_id<-rownames(cent)
#Gets time invariant actor attributes, so that people can retrieve any of these attributes in the graph later
actors<- df_id_au_unlisted$ID[c("author_id", "author_name", "pub_count")]
#Interactive Visualization
V(g)$label<-V(g)$name # create a copy of the author ids to be stored in the attribute label
V(g)$name<-1:length(V(g)) #replace the ids with numbers starting from 1
V(g)$label
## [1] "57209211014" "57209977398" "57206698047" "57222036217" "57672771100"
## [6] "56285357900" "8247022600" "57204936425" "35409037500" "50261568800"
## [11] "58044794200" "7004594995" "57198347616" "57194013851" "57195758433"
## [16] "57222635157" "57219026300" "57729253800" "55796169600" "56435413000"
## [21] "25927074700" "55560767700" "49961483900" "57198425768" "57808207400"
## [26] "6506641162" "9734661600" "6504542122" "57201364487" "57367923800"
## [31] "57353313700" "53363101000" "8334493200" "55898653500" "57188728335"
## [36] "37123493900" "26534181500" "57209302762" "57212209187" "57190220967"
## [41] "57220782938" "57205223816" "57211460720" "57192314401" "57213743031"
## [46] "56486644300" "57202586657" "57213823521" "26429421100" "35210562200"
## [51] "57207690066" "57211713050" "57193535940" "57193329538" "57214879511"
## [56] "36020199700" "57497828500" "55911711400" "36773797400" "57200436332"
## [61] "57312105000" "57653013200" "56943396200" "57215673939" "57220726620"
## [66] "57201981873" "57699862700" "35733208000" "57213410700" "57194725972"
## [71] "23670734000" "57207575202" "56502898700" "56612803400" "57218558004"
## [76] "57194521002" "15065422900" "57170065400" "57213873871" "54993944900"
## [81] "57198345447" "42960983900" "55510328300" "57204979805" "35832796300"
## [86] "57750164800" "57218995640" "57201109236" "35099135800" "57218850948"
## [91] "57188696285" "57210372946" "56558834000" "56225640200" "56896058300"
## [96] "57217530316" "56027519700" "25621682700" "7202758982" "56830491300"
## [101] "6603872406" "57203454589" "57189308223" "16480644500" "57160362400"
## [106] "57205541567" "24168867000" "9336303100" "56532638100" "57196414588"
## [111] "16741697900" "55934856500" "36491906600" "6506462455" "57163740200"
## [116] "25654949700" "56497205400" "57317375200" "56682689600" "56538719100"
## [121] "57221201743" "56761973100" "56748371500" "56367689500" "56823477200"
## [126] "57194772897" "55212507800" "57213340319" "43261043000" "23050937200"
## [131] "56333809000" "15027469200" "57209919056" "36728218300" "54581173100"
## [136] "36447484600" "56436228100" "7006615869" "13003277400" "56586033900"
## [141] "57195616124" "36344661900" "23984145600" "7006762185" "36509276900"
## [146] "56742597800" "35204604500" "22037160300" "57188819004" "55337974200"
## [151] "24460851700" "55505853200" "36154796800" "57202216646" "57194830879"
## [156] "56575051400" "6507838357" "57200916290" "16237824500" "57205094161"
## [161] "57209485440" "7006464833" "24173606100" "57195759333" "55317265200"
## [166] "57685148200" "57780137200" "57729853600" "57210283938" "35786658800"
## [171] "57201687563" "57191982026" "6603182543" "7006727005" "6506050805"
## [176] "57219619828" "25654986100" "57190399517" "7403557458" "57216991359"
## [181] "36504308400" "44861227200" "6602603940" "17233901600" "57216951730"
## [186] "57220781538" "57205225654" "56672834000" "57209639241" "57200678643"
## [191] "36167859900" "26028604100" "57197497788" "57811310600" "56849817300"
## [196] "57225466762" "55513838100" "56430817500" "57222254667" "57226280909"
## [201] "57190975144" "57188740759" "56712456700" "6507720943" "54415525900"
## [206] "55321241300" "55376947000" "57191256989" "36570330000" "44461010100"
## [211] "57660598900" "24528554900" "57203679182" "57222598872" "57207574078"
## [216] "57200163449" "55343897100" "57202442692" "57219311954" "56708797700"
## [221] "57225048378" "56437698900" "35182547500" "57191493247" "57749485300"
## [226] "11438901000" "7005070736" "55354066400" "57200935038" "7004913092"
## [231] "57972411800" "57208544088" "7003375339" "7003538222" "24463319500"
## [236] "7004355294" "57203462111" "26030924200" "57205541667" "55470679000"
## [241] "7203044800" "7401710282" "16022396600" "57191160735" "14830043400"
## [246] "7006694214" "55576275900" "18933354000" "57213763362" "56275797600"
## [251] "23978497700" "49561301400" "56749118900" "7102451228" "36167315400"
## [256] "55903827500" "23388909200" "35775265900" "56733021700" "26431968900"
## [261] "57192306786" "7006721381" "57193775692" "57190124302" "16308829800"
## [266] "55119860600" "57192188551" "55496621600" "6508240586" "7003338101"
## [271] "55500630000" "26537674200" "57196346943" "35224622600" "56543263900"
## [276] "56740942100" "24301065700" "56712264600" "57320369800" "57685428900"
## [281] "57203923749" "57750635500" "39362289600" "7006544785" "6507519353"
## [286] "57204127021" "57486696700" "57210286113" "57216220493" "50261550300"
## [291] "7004258903" "56319369100" "57225244819" "57327105300" "6602950597"
## [296] "57203895162" "57132936100" "35857853600" "57193622644" "55347229700"
## [301] "55273574800" "57200408929" "57221631315" "57226338830" "57188672243"
## [306] "36543834800" "57226290437" "56896123600" "55926560900" "34975670700"
## [311] "57655214000" "36142116800" "6507809565" "57202021599" "57222227538"
## [316] "55337045200" "57209471545" "7003663130" "57225033756" "57220023325"
## [321] "56458923600" "57861494300" "8352122200" "57219878761" "57205316000"
## [326] "56099752400" "57208544045" "23005943500" "57205545258" "7003592437"
## [331] "55786562000" "56447932100" "23388423500" "14035458600" "16238356400"
## [336] "15123849500" "56411901500" "6504672332" "6602520543" "56438641200"
## [341] "57204215575" "56341072200" "37065367600" "57199377238" "55958294200"
## [346] "57196002482" "36457541000" "55534318400" "56055285100" "55560959100"
## [351] "36495891000" "57730006700" "57210286833" "7501312845" "57201366650"
## [356] "57843002600" "58090105700" "57205576058" "57189890594" "57470704400"
## [361] "7202919226" "56204521500" "57197833255" "17435886300" "58052982400"
## [366] "57192255088" "27467665600" "7103215953" "35369323900" "57190010219"
## [371] "57225037230" "57196484076" "17342010800" "57226319422" "57222366280"
## [376] "22836127500" "14031188000" "6701795946" "56411314000" "44761334100"
## [381] "57183868400" "55778980000" "57195610842" "55089205700" "6603707134"
## [386] "22634717800" "56712937400" "55758929700" "57695858900" "57208512814"
## [391] "56505875800" "57216952098" "56900790700" "57226343494" "19639011200"
## [396] "6506226046" "7801364165" "57654670300" "57784144500" "57225054459"
## [401] "57219878019" "6701768847" "55926214600" "7004390248" "57195613097"
## [406] "55729449300" "6603165496" "7004977110" "57638878000" "57297562800"
## [411] "57200675555" "26422343700" "57225034232" "57219878425" "6506135720"
## [416] "57730314200" "14632198100" "57200679515" "24470147200" "57638878200"
## [421] "57200679563" "57729254000" "57200673519" "35605425800" "6505739936"
V(g)$name
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## [91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
## [109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## [127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## [145] 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
## [163] 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
## [181] 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
## [199] 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
## [217] 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
## [235] 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
## [253] 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
## [271] 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
## [289] 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
## [307] 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
## [325] 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
## [343] 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
## [361] 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
## [379] 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
## [397] 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
## [415] 415 416 417 418 419 420 421 422 423 424 425
#Gets edgelist from graph, also any other attribute at the edge level to be included in the mapping
links<-as.data.frame(cbind(get.edgelist(g), E(g)$weight))
head(links) #V3 is an attribute of the connection, in this case is the weight
## V1 V2 V3
## 1 1 152 1
## 2 1 279 1
## 3 3 153 1
## 4 3 280 1
## 5 4 154 1
## 6 4 281 1
#Needs to be numeric
links$V1<-as.numeric(as.character(links$V1))
links$V2<-as.numeric(as.character(links$V2))
str(links)
## 'data.frame': 589 obs. of 3 variables:
## $ V1: num 1 1 3 3 4 4 5 6 7 8 ...
## $ V2: num 152 279 153 280 154 281 155 156 157 158 ...
## $ V3: num 1 1 1 1 1 1 1 1 1 1 ...
links$V3<-round(as.numeric(as.character(links$V3)),3)
head(links)
## V1 V2 V3
## 1 1 152 1
## 2 1 279 1
## 3 3 153 1
## 4 3 280 1
## 5 4 154 1
## 6 4 281 1
#rename the columns
colnames(links)<-c("source","target", "value")
head(links)
## source target value
## 1 1 152 1
## 2 1 279 1
## 3 3 153 1
## 4 3 280 1
## 5 4 154 1
## 6 4 281 1
#Counts begin at zero in computer programming
links[,1:2]<-(links[,1:2]-1)
dim(links)
## [1] 589 3
#add publication count, author names, and centrality attributes at the vertex level
V(g)$pub_count <- pub_count$Freq[match(pub_count$Var1, V(g)$label)]
V(g)$author_name <-V(g)$author[match(df_id_au_unlisted$ID, V(g)$label)]
V(g)$size <- cent$bet[match(cent$au_id, V(g)$label)]
V(g)$eig <- cent$eig[match(cent$au_id, V(g)$label)]
V(g)$deg <- cent$degree[match(cent$au_id, V(g)$label)]
nodes <- data.frame(name= c(paste("ID: ", V(g)$label,
", Pub Count: ", V(g)$pub_count,
"Name: ", V(g)$author_name,
sep="")),
EV_cent = abs(V(g)$eig),
deg_cent=abs(V(g)$deg),
size=abs(V(g)$size)
)
nodes$name<-as.character(nodes$name)
head(nodes);tail(nodes)
## name EV_cent
## 1 ID: 57209211014, Pub Count: 1Name: Xue S. 7.492705e-17
## 2 ID: 57209977398, Pub Count: 1Name: Volchenkova K.N. 1.119102e-16
## 3 ID: 57206698047, Pub Count: 1Name: Gray A.C. 6.421315e-17
## 4 ID: 57222036217, Pub Count: 1Name: Denisenko A.V. 1.085190e-16
## 5 ID: 57672771100, Pub Count: 1Name: Liu W. 4.413486e-17
## 6 ID: 56285357900, Pub Count: 1Name: Drozdikova-Zaripova A.R. 6.701879e-17
## deg_cent size
## 1 2 0
## 2 0 0
## 3 2 0
## 4 2 0
## 5 1 0
## 6 1 0
## name EV_cent deg_cent
## 420 ID: 57638878200, Pub Count: 1Name: Velásquez W.L.V. 4.512312e-16 8
## 421 ID: 57200679563, Pub Count: 1Name: Alexander C. 1.000000e+00 10
## 422 ID: 57729254000, Pub Count: 1Name: Suaña G.M.D. 4.398240e-16 8
## 423 ID: 57200673519, Pub Count: 1Name: Niceler B. 1.000000e+00 10
## 424 ID: 35605425800, Pub Count: 1Name: Viera A.J. 1.000000e+00 10
## 425 ID: 6505739936, Pub Count: 1Name: Zakrajsek T. 1.000000e+00 10
## size
## 420 0
## 421 0
## 422 0
## 423 0
## 424 0
## 425 0
nodes$group <-cut(V(g)$pub_count, c(0,1,2,3,4,max(V(g)$pub_count)), right=TRUE, labels = FALSE)
table(is.na(nodes$group))
##
## FALSE
## 425
head(nodes[is.na(nodes$group),],20)
## [1] name EV_cent deg_cent size group
## <0 rows> (or 0-length row.names)
nodes$group<-ifelse(is.na(nodes$group), "", ifelse(nodes$group==1, "pub count=1", ifelse(nodes$group==2, "pub count=2", ifelse(nodes$group==3, "pub count =3", ifelse(nodes$group==4, "pub count=4", ifelse(nodes$group==5, "pub count=5","pub count=6"))))))
counts<-data.frame(table(nodes$group))
counts$labels <- paste(counts$Var1, ", N= ", counts$Freq, sep="")
nodes$groups <- counts$labels[match(nodes$group, counts$Var1)]
library(networkD3)
library(magrittr)
library(htmlwidgets)
##
## Attaching package: 'htmlwidgets'
## The following object is masked from 'package:networkD3':
##
## JS
library(htmltools)
# further modify the visualization
netviz<-forceNetwork(Links = links,
Nodes = nodes,
Source = 'source',
Target = 'target',
NodeID = 'name',
Group = "groups",
charge = -30,
opacity = 0.8,
Value = "value",
Nodesize = 'size',
zoom = T,
fontSize=14,
bounded= F,
legend= TRUE)
HTMLaddons <-
"function(el, x) {
d3.select('body').style('background-color', ' #EDFFF8 ')
d3.selectAll('.legend text').style('fill', 'white')
d3.selectAll('.link').append('svg:title')
.text(function(d) { return 'No. of co-publication: ' + d.value })
var options = x.options;
var svg = d3.select(el).select('svg')
var node = svg.selectAll('.node');
var link = svg.selectAll('link');
var mouseout = d3.selectAll('.node').on('mouseout');
function nodeSize(d) {
if (options.nodesize) {
return eval(options.radiusCalculation);
} else {
return 6;
}
}
d3.selectAll('.node').on('click', onclick)
function onclick(d) {
if (d3.select(this).on('mouseout') == mouseout) {
d3.select(this).on('mouseout', mouseout_clicked);
} else {
d3.select(this).on('mouseout', mouseout);
}
}
function mouseout_clicked(d) {
node.style('opacity', +options.opacity);
link.style('opacity', +options.opacity);
d3.select(this).select('circle').transition()
.duration(750)
.attr('r', function(d){return nodeSize(d);});
d3.select(this).select('text').transition()
.duration(1250)
.attr('x', 0)
.style('font', options.fontSize + 'px ');
}
}
"
netviz$x$links$linkDistance <- (1/links$value)*50 #inverse, as the value grows, nodes get closer. i.e. people taken more classes together are going to be placed closer to each other
#netviz$x$links$campus <- links$value
onRender(netviz, HTMLaddons)