将属性添加到具有复杂值的图形

时间:2017-04-16 17:20:18

标签: r igraph

我试图使用igraph在R中构建开源软件开发网络的模型。该图表是二分的,开发人员是一组节点,项目是第二组。

每个项目可能具有零个或多个与之关联的已知环境,以及与之关联的零个或多个编程语言。目前,我以列表列表的形式拥有这两个字段,表示项目列表如何具有环境/语言列表。由于缺少数据,任一列表中的某些值可能为空。

我希望项目环境和语言都是图形的属性,希望以后我可以过滤掉除特定环境/语言之外的所有项目。但我找不到任何方法允许属性将列表作为其值。有没有办法让属性像列表一样是复杂的值,如果没有,我怎样才能根据特定的环境/语言过滤图形呢?

到目前为止我已经包含了我的源代码:

library(igraph)
library(RMySQL)
# second library?

#Extracting data from the server
con <- dbConnect(dbDriver("MySQL"), user = "root", password = "root", dbname = "ow")
edgeList <- dbGetQuery(con, "SELECT dev_loginname, proj_unixname FROM ow_developer_projects")
people <- dbGetQuery(con, "SELECT DISTINCT dev_loginname FROM ow_developer_projects")[,1]
projects <- dbGetQuery(con, "SELECT DISTINCT proj_unixname FROM ow_developer_projects")[,1]

#Data Cleaning/Prep
edgeListPeople = edgeList[,1]
edgeListProjects = edgeList[,2]

envVector = NULL
langVector = NULL
for (i in seq(1, length(people))) {
  #Note that the first length(people) vertices are people, and have no associated environment or language
  envVector <- c(envVector, list(NULL))
  langVector <- c(langVector, list(NULL))
  edgeListPeople[edgeListPeople==people[i]] <- i
}


for (i in seq(1, length(projects))) {
  edgeListProjects[edgeListProjects==projects[i]] <- i+length(people)
  envVector <- c(envVector, dbGetQuery(con, paste("SELECT description FROM ow_project_environment WHERE proj_unixname =", "'", projects[i], "'", sep="")))
  langVector <- c(langVector, dbGetQuery(con, paste("SELECT description FROM ow_project_programming_language WHERE proj_unixname = ", "'", projects[i], "'", sep="")))
}
dbDisconnect(con)

edgeList = data.frame(edgeListPeople, edgeListProjects)

g <- graph_from_data_frame(edgeList, directed = FALSE)
V(g)$type <- V(g)$name %in% edgeList[,1]
V(g)$color <- V(g)$type + 1

# Here is where I'd like to set the environment and language attributes, but can't.
g <- set_vertex_attr(g, "environment", envVector)
g <- set_vertex_attr(g, "language", langVector)

l <- layout.fruchterman.reingold(g)*0.08
plot(g, 
     layout=l, 
     vertex.size=5,
     edge.arrow.size=.2,
     vertex.label=NA, 
     rescale=F)

0 个答案:

没有答案