我有一个大的数据集,我想用igraph用网络图表示。我只是不明白如何正确选择颜色。我的数据采用以下格式:
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 |
+--------+--------+----------+---------+
这代表采访的结果。每个人都有相同的问题,然后必须以名称(或多个名称)的形式给出该问题的答案。因此,约翰回答“琳达,山姆和安娜”,琳达回答“约翰和山姆”,依此类推。
现在,我想将这些结果用颜色标记在网络图中。 “组”列中的颜色是每个人的顶点的颜色(因此,约翰是绿色,琳达和丹尼尔都是红色)。 “位置”列中的颜色是从“名称”的顶点到“答案”的顶点的箭头的颜色。例如:
这里的箭头是正确的,但是颜色是错误的。约翰和琳达之间的两个箭头应该是相同的颜色。约翰的顶点应该是绿色,而琳达和丹尼尔的顶点应该是红色。对于Sam和Anna,我没有设置颜色(我该怎么做?)
到目前为止,我的代码是:
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)
答案 0 :(得分:1)
也许我过于复杂了,但是这段代码似乎正是您想要的:
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)
编辑:我没有使用您确切的颜色编码!
答案 1 :(得分:0)
这是一个可行的解决方案:
# 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
以及如何指定顶点和边缘的颜色。