[英]How do I color edges and vertices in igraph?
我有一个大型数据集,我想使用 igraph 用网络图表示。 我只是不明白如何正确获取 colors。 我的数据是这种格式:
df <- data.frame(name = c("john", "john", "john", "linda", "linda", "daniel"), answer = c("linda", "sam", "anna", "john", "sam", "anna"), location = c("#000000", "#000000", "#343434", "#000000", "#000000", "#343434"), group = c("#00FF00", "#00FF00", "#00FF00", "#FF0000", "#FF0000", "#FF0000"))
+--------+--------+----------+---------+
| name | answer | location | group |
+--------+--------+----------+---------+
| john | linda | #000000 | #00FF00 |
| john | sam | #000000 | #00FF00 |
| john | anna | #343434 | #00FF00 |
| linda | john | #000000 | #FF0000 |
| linda | sam | #000000 | #FF0000 |
| daniel | anna | #343434 | #FF0000 |
+--------+--------+----------+---------+
这代表了采访的结果。 每个人都得到了同样的问题,然后不得不以一个名字(或多个名字)的形式给出这个问题的答案。 所以约翰回答“琳达、山姆和安娜”,琳达回答“约翰和山姆”等等。
现在我想在网络图中用颜色表示这些结果。 “组”列中的颜色是每个人顶点的颜色(所以 john 是绿色,linda 和 daniel 都是红色)。 “位置”列中的颜色是从“名称”的顶点到“答案”的顶点的箭头的颜色。 例如:
这里的箭头是正确的,但是 colors 是错误的。 john 和 linda 之间的两个箭头应该是相同的颜色。 john 的顶点应该是绿色的,linda 和 daniel 的顶点应该是红色的。 对于山姆和安娜,我没有设置颜色(我该怎么做?)
到目前为止,我的代码是:
g <- graph.data.frame(df)
V(g)[df$answer]$color <- df$location
V(g)[df$name]$color <- df$group
plot(g, vertex.color = V(g)[df$name]$color, edge.color = V(g)[df$answer]$color)
也许我过于复杂了,但这段代码似乎是你正在寻找的:
df <- data.frame(name = c("john", "john", "john", "linda", "linda", "daniel"), answer = c("linda", "sam", "anna", "john", "sam", "anna"), location = c("pink", "pink", "red", "pink", "pink", "red"), group = c("yellow", "yellow", "yellow", "blue", "blue", "blue"))
g <- graph.data.frame(df)
#assign to each edge its colour. this works since all the rows in your
#dataframe represent an edge in the resulting graph
E(g)$color <- as.character(df$location)
#then loop through the number of nodes in the graph
for (vrt in 1:length(V(g))){
#since the names in the first column are only a part of all the nodes check if it belongs to that sublist
if(V(g)$name[vrt] %in% df$name) {
#then find the first occurrence of that name in the list and get its related color
#assign it to that node
V(g)$color[vrt] <- as.character(df$group[which(df$name==V(g)$name[vrt])[1]])
}
#otherwise the node will be white (e.g. for anna and sam)
else {
V(g)$color[vrt] <- "white"
}
}
#eventually plot it
plot(g, vertex.color = V(g)$color, edge.color = E(g)$color)
编辑:我没有使用您的确切颜色编码!
这是一个有效的解决方案:
# Load the igraph library
library(igraph)
# Create a simple network
df <- data.frame(name = c("john", "john", "john", "linda", "linda", "daniel"),
answer = c("linda", "sam", "anna", "john", "sam", "anna"),
location = c("#000000", "#000000", "#343434", "#000000", "#000000", "#343434"),
group = c("#00FF00", "#00FF00", "#00FF00", "#FF0000", "#FF0000", "#FF0000"),
stringsAsFactors=FALSE)
# Build a network graph
graph <- graph.data.frame(df)
# Assign colours to vertices
V(graph)$colour <- sapply(V(graph)$name,
function(x, df){
return(df[which(df$name == x)[1], "group"])
}, df)
# Assign colours to the edges
E(graph)$colour <- df$location
# Plot the graph
plot(g, vertex.color=V(graph)$colour, edge.color=E(graph)$colour)
上面要注意的重要事项是stringsAsFactors=FALSE
以及如何分配顶点和边的颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.