如何在igraph中为边和顶点着色?

如何在igraph中为边和顶点着色?,r,igraph,R,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", "#0

我有一个大的数据集,我想用一个网络图来表示它。我只是不知道如何把颜色弄对。我的数据采用以下格式:

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都是红色的。列位置中的颜色是从名称顶点到答案顶点的箭头的颜色。例如:

这里的箭头是对的,但颜色是错的。约翰和琳达之间的两支箭应该是同一种颜色。约翰的顶点应该是绿色的,琳达和丹尼尔的顶点应该是红色的。对于山姆和安娜,我还没有设定颜色,我该怎么做

到目前为止,我的代码是:

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)

编辑:我没有使用你确切的颜色编码

也许我把它复杂化了,但这段代码似乎正是您想要的:

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以及顶点和边的颜色是如何分配的


以下是一个可行的解决方案:

# 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以及顶点和边的颜色是如何分配的