如何根据数据框中的列设置顶点颜色iGraph?

时间:2015-12-28 19:31:38

标签: r igraph

我正在尝试根据数据框中的额外列设置iGraph中的顶点颜色。我在搜索中没有看到这样的问题。虽然我上次想到并最终提出了一个重复的问题。我的数据如下:

Vertex1     Vertex2    Type
Chat        Forms      Other
Specials    Chat       Info
Form Sent   Linkout    Lead
Home        Chat       Home

我正在尝试根据type列的内容设置Vertex的值。这是我正在使用的代码:

V(paths.g)$Type=as.character(VDP_Path5$Type[match(V(paths.g)$Type,VDP_Path5$Node1)])
V(paths.g)$color=V(paths.g)$Type
V(paths.g)$color=gsub("Lead","forest",V(paths.g)$color)
V(paths.g)$color=gsub("Vehicle","leaf",V(paths.g)$color)
V(paths.g)$color=gsub("Home","gold",V(paths.g)$color)
V(paths.g)$color=gsub("Other","silver",V(paths.g)$color)

plot(paths.g, edge.width=VDP_Path5$weight,layout=layout.fruchterman.reingold)

当我运行绘图时,所有顶点都是默认的青色。

谢谢

1 个答案:

答案 0 :(得分:3)

你需要这样的东西:

edges <- c(1,2, 3,4, 2,4, 1,3, 1,5)
g <- make_graph(edges, directed=FALSE)
plot(g, vertex.color=1:5)

不需要添加颜色作为顶点属性,但在绘图时可以像vertex.color=V(g)$color一样。还有更简单的方法将文本转换为特定的颜色(我会选择一个命名的颜色矢量)。

如果您需要更精确的建议,请将您的数据/代码重现: - )

相关问题