Create your collaborator network in R

      No Comments on Create your collaborator network in R

I created a collaborator network last year in R, but based that on an excel input file that I created manually, and edited the names using Inkscape. Funny doing it once, but too much effort to repeat that every year.

I have a larger number of collaborators now, so I looked into finding an automated way of getting the papers into a format from which I can plot the network. Unfortunately, I haven’t been too successful with the scholar() R package, and I decided to walk into the office 3 meters down the hall, where Sacha Epskamp waved his gsub() wand. The R code below is by no means a proper solution, and I’m largely sharing this to see whether others have found easy ways to export papers from a Google Scholar profile that contains all author names — not just the first 5 — into a reasonably clean database.

What we do to create my collaborator network is to export all citations in APA format into a text file; you can easily do that with your reference manager in a few seconds if you know what you’re doing. Here is the file to reproduce the analyses below.

In R, we load qgraph and dplyr, clean up the refs.txt file via a number of gsub() commands (all hail Sacha the gsub() king), and connect these via the dplyr %>% operator that allows us to put functions together without lots of nested parentheses.

library("qgraph")
library("dplyr")
 
# Load references:  
refs <- readLines("refs.txt")
 
authors <- refs %>% 
  # First remove everything from date onwards:
  gsub("\\(.*","",.) %>% 
  # Remove asterix:
  gsub("\\*","",.) %>%
  # Make lowercase:
  tolower %>%
  # Remove spaces:
  gsub("\\s*","",.) %>%
  # Remove &
  gsub("\\&","",.) %>%
  # Split on .,:
  strsplit(split="(?<=\\.),",perl=TRUE) %>%
  # Remove periods and everything following:
  lapply(function(x)gsub("\\..*","",x))

You want to adapt this depending on the reference format you have your papers in.

Next up, we create a matrix where we have authors as rows and papers as columns. The numbers in this adjacency matrix indicate how often authors have been together on a given paper.

# Collect unique authors:
allAuthors <- unique(unlist(authors))
 
# Create list 
SymXDis <- do.call(cbind,lapply(authors,function(x){
  1*(allAuthors %in% x)
}))
 
# Make adjacency:
adj <- SymXDis %*% t(SymXDis)

The last step is a trick that’ll save you a ton of time, multiplying the adjacency matrix by its inverse to obtain what we need to plot the network.

Finally, we define author names as last names, and then plot the network in different ways.

# Labels:
labs <- gsub(",.*","",allAuthors)
substr(labs,0,1) <- toupper(substr(labs,0,1))
 
# Plot:
sha <- ifelse(allAuthors == "fried,e","square", "circle")
names<-c(1:71)
 
pdf("1network_full_numbers.pdf", width=6, height=4.5)
qgraph(adj, labels = names, nodeNames = labs, color = "#eeeeee",  shape=sha, vsize = 4, diag = FALSE,
       layout = "spring", edge.color = "#666666", repulsion = 0.90, 
       border.width=2, border.color='#444444', label.color="#555555", legend.cex=.15)
dev.off()
 
pdf("2network_full_names.pdf")
qgraph(adj, labels = labs,color = "#eeeeee",  vsize = 5, diag = FALSE, shape=sha,
       layout = "spring", edge.color = "#666666", repulsion = 0.90, 
       border.width=2, border.color='#444444', label.color="#555555")
dev.off()

This gives us:

We can also plot the networks excluding the focal node; this better highlights relationships among your co-authors (obviously, only for your papers, i.e. your co-authors might be unconnected although they published a paper together without you):

# Without Eiko:
adj2 <- adj[-1,-1]
 
pdf("3network_empty_numbers.pdf", width=6, height=4.5)
qgraph(adj2, labels = names[-71], nodeNames = labs[-1], color = "#eeeeee",  vsize = 4, diag = FALSE,
       layout = "spring", edge.color = "#666666", repulsion = 0.90,
       border.width=2, border.color='#444444', label.color="#555555", legend.cex=.15)
dev.off()
 
pdf("4network_empty_names.pdf")
qgraph(adj2, labels = labs[-1],color = "#eeeeee",  vsize = 5, diag = FALSE,
       layout = "spring", edge.color = "#666666", repulsion = 0.90,
       border.width=2, border.color='#444444', label.color="#555555")
dev.off()

To make this really pretty, you’d probably have to add a number of fixes (e.g. Dutch names such as van Loo are currently garbled into Vanloo), and identical last author names will eventually lead to issues.

If you have more elegant solutions based on crawling papers from Google Scholar, let us know in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *