library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
a <- read.csv("/Users/Akwelle/Desktop/scopus-coauthor-deliverables.csv")
g <- graph.data.frame(a[,c("Authors", "Title", "Affiliations","Year", "Source.title")])
V(g)$type <- V(g)$name %in% a[,c("Authors")] 
table(V(g)$type)[2]
## TRUE 
##  506
i<-table(V(g)$type)[2]
mat <- t(get.incidence(g))

mat <- mat%*%t(mat)
dim(mat)
## [1] 506 506
g<-graph.adjacency(mat, mode = "undirected", weighted = TRUE, diag=F)
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$name<-rownames(cent) #Ids in this case
head(cent);tail(cent)
##                                                                  bet eig degree
## Yi J.S., Garcia Morales E.E., Reed N.S., Willink A., Nieman C.L. NaN   1      0
## Li Y., Fu X., Guo X., Liang H., Cao D., Shi J.                   NaN   1      0
## Poe J.                                                           NaN   1      0
## Kang H., Lyu S.-R., Yun S.                                       NaN   1      0
## Brown J.R., Hilbig H.                                            NaN   1      0
## O'Hearn M., Lauren B.N., Wong J.B., Kim D.D., Mozaffarian D.     NaN   1      0
##                                                                                                                              name
## Yi J.S., Garcia Morales E.E., Reed N.S., Willink A., Nieman C.L. Yi J.S., Garcia Morales E.E., Reed N.S., Willink A., Nieman C.L.
## Li Y., Fu X., Guo X., Liang H., Cao D., Shi J.                                     Li Y., Fu X., Guo X., Liang H., Cao D., Shi J.
## Poe J.                                                                                                                     Poe J.
## Kang H., Lyu S.-R., Yun S.                                                                             Kang H., Lyu S.-R., Yun S.
## Brown J.R., Hilbig H.                                                                                       Brown J.R., Hilbig H.
## O'Hearn M., Lauren B.N., Wong J.B., Kim D.D., Mozaffarian D.         O'Hearn M., Lauren B.N., Wong J.B., Kim D.D., Mozaffarian D.
##                                                                      bet eig
## Preston B.L., Warren R.C., Stewart P.                                NaN   1
## Li B.D.L., Brown W.A., Ampil F.L., Burton G.V., Yu H., McDonald J.C. NaN   1
## Sawyer T.F.                                                          NaN   1
## Schaumberg D.A., Christen W.G., Glynn R.J., Buring J.E.              NaN   1
## Edwards A.B., Cherry R.L., Peterson J.                               NaN   1
## Edwards J.C., Maldonado Jr. F.G., Engelgau G.R.                      NaN   1
##                                                                      degree
## Preston B.L., Warren R.C., Stewart P.                                     0
## Li B.D.L., Brown W.A., Ampil F.L., Burton G.V., Yu H., McDonald J.C.      0
## Sawyer T.F.                                                               0
## Schaumberg D.A., Christen W.G., Glynn R.J., Buring J.E.                   0
## Edwards A.B., Cherry R.L., Peterson J.                                    0
## Edwards J.C., Maldonado Jr. F.G., Engelgau G.R.                           0
##                                                                                                                                      name
## Preston B.L., Warren R.C., Stewart P.                                                               Preston B.L., Warren R.C., Stewart P.
## Li B.D.L., Brown W.A., Ampil F.L., Burton G.V., Yu H., McDonald J.C. Li B.D.L., Brown W.A., Ampil F.L., Burton G.V., Yu H., McDonald J.C.
## Sawyer T.F.                                                                                                                   Sawyer T.F.
## Schaumberg D.A., Christen W.G., Glynn R.J., Buring J.E.                           Schaumberg D.A., Christen W.G., Glynn R.J., Buring J.E.
## Edwards A.B., Cherry R.L., Peterson J.                                                             Edwards A.B., Cherry R.L., Peterson J.
## Edwards J.C., Maldonado Jr. F.G., Engelgau G.R.                                           Edwards J.C., Maldonado Jr. F.G., Engelgau G.R.
cent$bet<-cent$bet/max(cent$bet)
#cent$eig<-cent$eig/max(cent$eig)
summary(cent)
##       bet           eig        degree      name          
##  Min.   : NA   Min.   :1   Min.   :0   Length:506        
##  1st Qu.: NA   1st Qu.:1   1st Qu.:0   Class :character  
##  Median : NA   Median :1   Median :0   Mode  :character  
##  Mean   :NaN   Mean   :1   Mean   :0                     
##  3rd Qu.: NA   3rd Qu.:1   3rd Qu.:0                     
##  Max.   : NA   Max.   :1   Max.   :0                     
##  NA's   :506
actors<- a[!duplicated(a$Authors), c("Authors", "Title", "ISSN", "Publisher", "Year")]
V(g)$label<-V(g)$name
V(g)$name<-1:length(V(g))
links<-as.data.frame(cbind(get.edgelist(g), E(g)$Year, E(g)$Affiliations))
links$V1<-as.numeric(as.character(links$V1))

links$V2<-as.numeric(as.character(links$V2))

links$V3<-round(as.numeric(as.character(links$V3)),3)
colnames(links)<-c("source","target", "value", "affiliations")
links[,1:2]<-(links[,1:2]-1)
V(g)$ISSN <- actors$ISSN[match(V(g)$label, actors$Authors)]
V(g)$Publisher <- actors$Publisher[match(V(g)$label, actors$Authors)]
V(g)$Year <- actors$Year[match(V(g)$label, actors$Authors)]
summary(V(g)$Publisher)
##    Length     Class      Mode 
##       506 character character
summary(actors$Year)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2000    2011    2016    2015    2020    2022
summary(V(g)$Year[1:i])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2000    2011    2016    2015    2020    2022
nodes <- data.frame(name= c(paste("ID: ", V(g)$label, 
                    ", ISSN: ", V(g)$ISSN, 
                    ", First Gen. College: ", V(g)$firstgen, 
                    ", Publisher: ", V(g)$Publisher, sep="")), 
                Year = abs(V(g)$Year), 
                    size=abs(V(g)$Year)) 
nodes$name<-as.character(nodes$name)
nodes$group<-NA
nodes$group[1:i]<-cut(nodes$Year[1:i], c(2000,2005,2010,2015,max(nodes$Year[1:i])), right=TRUE)
table(is.na(nodes$group))
## 
## FALSE  TRUE 
##   499     7
table(nodes$group)
## 
##   1   2   3   4 
##  36  79 125 259
head(nodes[is.na(nodes$group),],20)
##                                                                                                                                               name
## 500                                      ID: Sternfeld B., Cauley J., Harlow S., Liu G., Lee M., ISSN: 00029262, First Gen. College: , Publisher: 
## 501                                                   ID: Preston B.L., Warren R.C., Stewart P., ISSN: 07493797, First Gen. College: , Publisher: 
## 502                    ID: Li B.D.L., Brown W.A., Ampil F.L., Burton G.V., Yu H., McDonald J.C., ISSN: 00034932, First Gen. College: , Publisher: 
## 503                                                                             ID: Sawyer T.F., ISSN: 10934510, First Gen. College: , Publisher: 
## 504  ID: Schaumberg D.A., Christen W.G., Glynn R.J., Buring J.E., ISSN: 00257079, First Gen. College: , Publisher: Lippincott Williams and Wilkins
## 505                                                  ID: Edwards A.B., Cherry R.L., Peterson J., ISSN: 15333175, First Gen. College: , Publisher: 
## 506 ID: Edwards J.C., Maldonado Jr. F.G., Engelgau G.R., ISSN: 10402446, First Gen. College: , Publisher: Association of American Medical Colleges
##     Year size group
## 500 2000 2000    NA
## 501 2000 2000    NA
## 502 2000 2000    NA
## 503 2000 2000    NA
## 504 2000 2000    NA
## 505 2000 2000    NA
## 506 2000 2000    NA
nodes$group<-ifelse(is.na(nodes$group), "Year", ifelse(nodes$group==1, "2000-2005", ifelse(nodes$group==2, "2005-2010", ifelse(nodes$group==3, "2010=2015", "after 2015"))))
counts<-data.frame(table(nodes$group))

counts$labels <- paste(counts$Var1, ", N= ", counts$Freq, sep="")
nodes$groups <- counts$labels[match(nodes$group, counts$Var1)] 
head(nodes)
##                                                                                                                                            name
## 1 ID: Yi J.S., Garcia Morales E.E., Reed N.S., Willink A., Nieman C.L., ISSN: 08982643, First Gen. College: , Publisher: SAGE Publications Inc.
## 2                            ID: Li Y., Fu X., Guo X., Liang H., Cao D., Shi J., ISSN: 14712431, First Gen. College: , Publisher: NLM (Medline)
## 3                                                            ID: Poe J., ISSN: 21623104, First Gen. College: , Publisher: STAR Scholars Network
## 4                                 ID: Kang H., Lyu S.-R., Yun S., ISSN: 21623104, First Gen. College: , Publisher: University Printing Services
## 5                                        ID: Brown J.R., Hilbig H., ISSN: 00071234, First Gen. College: , Publisher: Cambridge University Press
## 6              ID: O'Hearn M., Lauren B.N., Wong J.B., Kim D.D., Mozaffarian D., ISSN: 07351097, First Gen. College: , Publisher: Elsevier Inc.
##   Year size      group             groups
## 1 2022 2022 after 2015 after 2015, N= 259
## 2 2022 2022 after 2015 after 2015, N= 259
## 3 2022 2022 after 2015 after 2015, N= 259
## 4 2022 2022 after 2015 after 2015, N= 259
## 5 2022 2022 after 2015 after 2015, N= 259
## 6 2022 2022 after 2015 after 2015, N= 259
library(networkD3)
library(magrittr)
library(htmlwidgets)
## 
## Attaching package: 'htmlwidgets'
## The following object is masked from 'package:networkD3':
## 
##     JS
library(htmltools)
netviz<-forceNetwork(Links = links, Nodes = nodes,
                  Source = 'source', Target = 'target',
                  NodeID = 'name',
                  Group = "groups", # color nodes by group calculated earlier
                  charge = -30, # node repulsion
                  linkDistance = JS("function(d) { return d.linkDistance; }"),#JS("function(d){return d.value}"),
                  linkWidth = JS("function(d) { return Math.sqrt(d.value)*2; }"),
                  opacity = 0.8,
                  Value = "value",
                  Nodesize = 'size', 
                  radiusCalculation = JS("Math.sqrt(d.nodesize*30)+4"),
                  zoom = T, 
                  fontSize=14,
                  bounded= F,
                  legend= TRUE,
                  linkColour = ifelse(links$value == 1, "#CCFFFF", ifelse(links$value == 2, "#e3eaa7", "#abb2b9 ")),
                  colourScale = JS("d3.scaleOrdinal(d3.schemeCategory10)"))
## Warning: It looks like Source/Target is not zero-indexed. This is required in
## JavaScript and so your plot may not render.
HTMLaddons <- 
"function(el, x) { 
d3.select('body').style('background-color', ' #212f3d ')
d3.selectAll('.legend text').style('fill', 'white') 
 d3.selectAll('.link').append('svg:title')
      .text(function(d) { return 'No. of courses common: ' + d.value + ', Campus: ' + d.campus ; })
  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
netviz$x$links$campus <- links$value
onRender(netviz, HTMLaddons)

We should care about this because it allows us to see how the different actors in the network interact with one another based on a variety of characteristics, which can help us better understand the implications of the network.