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]
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
## Yi J.S., Garcia Morales E.E., Reed N.S., Willink A., Nieman C.L.   0
## Li Y., Fu X., Guo X., Liang H., Cao D., Shi J.                     0
## Poe J.                                                             0
## Kang H., Lyu S.-R., Yun S.                                         0
## Brown J.R., Hilbig H.                                              0
## O'Hearn M., Lauren B.N., Wong J.B., Kim D.D., Mozaffarian D.       0
##                                                                           eig
## Yi J.S., Garcia Morales E.E., Reed N.S., Willink A., Nieman C.L. 4.906539e-18
## Li Y., Fu X., Guo X., Liang H., Cao D., Shi J.                   9.813078e-18
## Poe J.                                                           4.906539e-18
## Kang H., Lyu S.-R., Yun S.                                       9.813078e-18
## Brown J.R., Hilbig H.                                            9.813078e-18
## O'Hearn M., Lauren B.N., Wong J.B., Kim D.D., Mozaffarian D.     9.813078e-18
##                                                                  degree
## Yi J.S., Garcia Morales E.E., Reed N.S., Willink A., Nieman C.L.      1
## Li Y., Fu X., Guo X., Liang H., Cao D., Shi J.                        1
## Poe J.                                                                1
## Kang H., Lyu S.-R., Yun S.                                            1
## Brown J.R., Hilbig H.                                                 1
## O'Hearn M., Lauren B.N., Wong J.B., Kim D.D., Mozaffarian D.          1
##                                                                                                                              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
## Factors affecting environmental awareness among Head Start families in Mississippi                                         0
## Patient compliance is critical for equivalent clinical outcomes for breast cancer treated by breast-conservation therapy   0
## Francis Cecil Sumner: His Views and Influence on African American Higher Education                                         0
## Demographic predictors of eye care utilization among women                                                                 0
## Predictors of misconceptions of Alzheimer's disease among community dwelling elderly                                       0
## Beyond affirmative action: One school's experiences with a race-neutral admission process                                  0
##                                                                                                                                   eig
## Factors affecting environmental awareness among Head Start families in Mississippi                                       9.813078e-18
## Patient compliance is critical for equivalent clinical outcomes for breast cancer treated by breast-conservation therapy 9.813078e-18
## Francis Cecil Sumner: His Views and Influence on African American Higher Education                                       9.813078e-18
## Demographic predictors of eye care utilization among women                                                               9.813078e-18
## Predictors of misconceptions of Alzheimer's disease among community dwelling elderly                                     9.813078e-18
## Beyond affirmative action: One school's experiences with a race-neutral admission process                                1.471962e-17
##                                                                                                                          degree
## Factors affecting environmental awareness among Head Start families in Mississippi                                            1
## Patient compliance is critical for equivalent clinical outcomes for breast cancer treated by breast-conservation therapy      1
## Francis Cecil Sumner: His Views and Influence on African American Higher Education                                            1
## Demographic predictors of eye care utilization among women                                                                    1
## Predictors of misconceptions of Alzheimer's disease among community dwelling elderly                                          1
## Beyond affirmative action: One school's experiences with a race-neutral admission process                                     1
##                                                                                                                                                                                                                                              name
## Factors affecting environmental awareness among Head Start families in Mississippi                                                                             Factors affecting environmental awareness among Head Start families in Mississippi
## Patient compliance is critical for equivalent clinical outcomes for breast cancer treated by breast-conservation therapy Patient compliance is critical for equivalent clinical outcomes for breast cancer treated by breast-conservation therapy
## Francis Cecil Sumner: His Views and Influence on African American Higher Education                                                                             Francis Cecil Sumner: His Views and Influence on African American Higher Education
## Demographic predictors of eye care utilization among women                                                                                                                             Demographic predictors of eye care utilization among women
## Predictors of misconceptions of Alzheimer's disease among community dwelling elderly                                                                         Predictors of misconceptions of Alzheimer's disease among community dwelling elderly
## Beyond affirmative action: One school's experiences with a race-neutral admission process                                                               Beyond affirmative action: One school's experiences with a race-neutral admission process
cent$bet[1:i]<-cent$bet[1:i]/max(cent$bet[1:i])
cent$eig[1:i]<-cent$eig[1:i]/max(cent$eig[1:i])
summary(cent[1:i,])
##       bet                eig               degree         name          
##  Min.   :0.000000   Min.   :0.000000   Min.   :1.00   Length:506        
##  1st Qu.:0.000000   1st Qu.:0.000000   1st Qu.:1.00   Class :character  
##  Median :0.000000   Median :0.000000   Median :1.00   Mode  :character  
##  Mean   :0.007905   Mean   :0.001976   Mean   :1.01                     
##  3rd Qu.:0.000000   3rd Qu.:0.000000   3rd Qu.:1.00                     
##  Max.   :1.000000   Max.   :1.000000   Max.   :3.00
cent$bet[(i+1):nrow(cent)]<-cent$bet[(i+1):nrow(cent)]/max(cent$bet[(i+1):nrow(cent)])
cent$eig[(i+1):nrow(cent)]<-cent$eig[(i+1):nrow(cent)]/max(cent$eig[(i+1):nrow(cent)])
summary(cent[(i+1):nrow(cent),])
##       bet           eig               degree          name          
##  Min.   : NA   Min.   :0.000000   Min.   :1.000   Length:510        
##  1st Qu.: NA   1st Qu.:0.000000   1st Qu.:1.000   Class :character  
##  Median : NA   Median :0.000000   Median :1.000   Mode  :character  
##  Mean   :NaN   Mean   :0.002941   Mean   :1.002                     
##  3rd Qu.: NA   3rd Qu.:0.000000   3rd Qu.:1.000                     
##  Max.   : NA   Max.   :1.000000   Max.   :2.000                     
##  NA's   :510
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 
##      1016 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[1:i], 
                    " ISSN: ", V(g)$ISSN[1:i], 
                    " Publisher: ", V(g)$Publisher[1:i], sep=""), 
                    V(g)$label[(i+1):length(V(g)$label)]), 
                    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   517
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 Publisher: 
## 501                                                                         ID: Preston B.L., Warren R.C., Stewart P. ISSN: 07493797 Publisher: 
## 502                                          ID: Li B.D.L., Brown W.A., Ampil F.L., Burton G.V., Yu H., McDonald J.C. ISSN: 00034932 Publisher: 
## 503                                                                                                   ID: Sawyer T.F. ISSN: 10934510 Publisher: 
## 504                        ID: Schaumberg D.A., Christen W.G., Glynn R.J., Buring J.E. ISSN: 00257079 Publisher: Lippincott Williams and Wilkins
## 505                                                                        ID: Edwards A.B., Cherry R.L., Peterson J. ISSN: 15333175 Publisher: 
## 506                       ID: Edwards J.C., Maldonado Jr. F.G., Engelgau G.R. ISSN: 10402446 Publisher: Association of American Medical Colleges
## 507                                                                Racial and Ethnic Differences in Hearing Aid Use Among Medicare Beneficiaries
## 508                                                        Maternal preterm birth prediction in the United States: a case-control database study
## 509              Advancing Global Citizenship of Underrepresented and Hypersegregated U.S. Students in Higher Education through Virtual Exchange
## 510       Going Beyond the (Un)awakened Body: Arts-Based Collaborative Autoethnographic Inquiry of Korean Doctoral Students in the United States
## 511                                                                Locked Out of College: When Admissions Bureaucrats Do and Do Not Discriminate
## 512                                                                Trends and Disparities in Cardiometabolic Health Among U.S. Adults, 1999-2018
## 513 Impact of socio-economic factors on radiation treatment after resection of metastatic brain tumors: trends from a private insurance database
## 514                                                   Prevalence and prediction of medical distrust in a diverse medical genomic research sample
## 515         Comparison of unmet health care needs in children with intellectual disability, autism spectrum disorder and both disorders combined
## 516                  Racial and Ethnic Differences in Hospice Use and Hospitalizations at End-of-Life among Medicare Beneficiaries with Dementia
## 517                             Learning to Value Girls: Balanced Infant Sex Ratios at Higher Parental Education in the United States, 1969–2018
## 518                                                                                   Recruit to reject? Harvard and African American applicants
## 519                                                       A product of prestige?: “Race unknown” and competitive admissions in the United States
##     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
## 507   NA   NA    NA
## 508   NA   NA    NA
## 509   NA   NA    NA
## 510   NA   NA    NA
## 511   NA   NA    NA
## 512   NA   NA    NA
## 513   NA   NA    NA
## 514   NA   NA    NA
## 515   NA   NA    NA
## 516   NA   NA    NA
## 517   NA   NA    NA
## 518   NA   NA    NA
## 519   NA   NA    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 Publisher: SAGE Publications Inc.
## 2                            ID: Li Y., Fu X., Guo X., Liang H., Cao D., Shi J. ISSN: 14712431 Publisher: NLM (Medline)
## 3                                                            ID: Poe J. ISSN: 21623104 Publisher: STAR Scholars Network
## 4                                 ID: Kang H., Lyu S.-R., Yun S. ISSN: 21623104 Publisher: University Printing Services
## 5                                        ID: Brown J.R., Hilbig H. ISSN: 00071234 Publisher: Cambridge University Press
## 6              ID: O'Hearn M., Lauren B.N., Wong J.B., Kim D.D., Mozaffarian D. ISSN: 07351097 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
nodes$Author[(i+1):length(V(g)$label)] <- a$Title[match(nodes$name[(i+1):length(V(g)$label)], a$Title)]
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',
                  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$campus == "NEUNK", "#CCFFFF", ifelse(links$campus == "NEUNL", "#e3eaa7", "#abb2b9 ")),
                  colourScale = JS("d3.scaleOrdinal(d3.schemeCategory10)"))

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 'Grade course: ' + 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)*1500
netviz$x$links$campus <- links$campus
onRender(netviz, HTMLaddons)