在图

时间:2017-09-05 05:29:52

标签: r igraph

在从论文的元数据中提取作者之后,我们可以建立一个可以揭示学术关系的共同作者网络。作者列表(auth)就像下面一样,每一行都是论文,每一栏都是作者:

> dput(auth)
structure(list(`author 1` = c("Zhang XiaoLin", "Zhang XL", "Zhang ZQ", "Jiansheng Qu", 
"Wang Q", "Wang, XueMei", "ZHANG Lihua", "Jiansheng Qu", "Hui Zhang", "Wang XM"), `author 2` = c("Liu, XiWen", 
"Zhang DR", "Qu JS", "Jingjing Zeng", "Zhang B", "Ma Mingguo", "zhang zhiqiang", 
"Tek Maraseni", "zhang zhiqiang", "Li X"), `author 3` = c("LI Lin", "Zhao Y", 
"Zeng JJ", "Yan Li", "Zhang ZQ", "Li Xin", NA, "Lina Liu", NA, 
"Ma MG"), `author 4` = c("Ceng Yan", "Liang N", NA, "Qin Wang", "Zhang XF", 
"zhang zhiqiang", NA, "Zhiqiang Zhang", NA, "Zhang ZQ")), .Names = c("author 1", "author 2", "author 3", 
"author 4"), class = "data.frame", row.names = c(NA, 10L))

我想用这个数据框架建立一个共同作者网,我的程序就像这样:

# author co-occurrence
library(igraph)
# prepare authors data from metadata of papers
n <- max_n_au
cname <- paste("author", 1:n)
auth <- au_t[, cname]
# function that build the edges of Co-author Relations(CAR)
CAR_edge <- function(au_e) {
  n <- length(au_e)
  if (is.na(auth[,cname])) {
    skip
  } else {
    q <- combn(n,2)
    au_n <- auth[q]
    #dim(au_n) <- dim(q)
  }
  return(au_n)
}
# build author relations for each paper
arp <- lapply(auth, CAR_edge)
Warning messages:
1: In if (is.na(auth[, cname])) { :
  the condition has length > 1 and only the first element will be used
2: In if (is.na(auth[, cname])) { :
  the condition has length > 1 and only the first element will be used
3: In if (is.na(auth[, cname])) { :
  the condition has length > 1 and only the first element will be used
4: In if (is.na(auth[, cname])) { :
  the condition has length > 1 and only the first element will be used

我没有制作复杂功能的经验。即便如此,当我测试函数中的句子时,我仍然发现“q”可以为作者建立一个队列,但它无法正确传递给作者。它不适合行但是适合列,也不适合。 &#34; combn()&#34;的机制是什么?用于构建具有数据帧的序列的功能。 我试图改变combn()函数的方法。结果附加作者,但只有上面的方式可以运行没有错误。所以我希望我能就这个问题得到一些建议,非常感谢。

1 个答案:

答案 0 :(得分:1)

我认为这可以更简单。您只需要从列表中构建所有可能的作者和共同作者对。完成后,使图表变得简单。

使用上面的变量auth

## First restructure into a list of pairs
AuthorPairs = data.frame(author = character(), coauthor = character())
for(i in 1:3) { 
    for(j in (i+1):4) {
        AuthorPairs = rbind(AuthorPairs, 
            data.frame(author = auth[,i], coauthor = auth[,j]))
    }
}
AuthorPairs = AuthorPairs[!is.na(AuthorPairs[,2]),]

# Turn it into a graph and plot
library(igraph)

AuthorGraph = graph_from_edgelist(as.matrix(AuthorPairs), directed=FALSE)
par(mar=rep(1,4))
plot(AuthorGraph, vertex.label.cex=0.8)

Author Network