如何在R中的图形中为顶点添加属性名称

时间:2018-02-10 05:58:26

标签: r social-networking

这是我的代码

d <- read.csv("dataset.csv")

s_network <- graph_from_data_frame(d[,2:3], directed = FALSE)

plot(s_network)
2:3 are source:target nodes

我是R的新手我想知道如何添加属于我数据集第4列的属性名称

1 个答案:

答案 0 :(得分:0)

如果您只是想在图表中添加标签,请按照mikeck的建议查看set_vertex_attr帮助文件,这是给出的示例。

library(igraph)
g <- make_ring(10) %>%
  set_vertex_attr("label", value = LETTERS[1:10])
g
plot(g)

导致:

enter image description here

同样来自graph_from_data_frame帮助文件的改编是另一种选择:

relations <- data.frame(from = c("Bob", "Cecil", "Cecil", "David",
  "David", "Esmeralda"), to = c("Alice", "Bob", "Alice", "Alice", "Bob", 
"Alice") )

my_names <- c("Alice", "Bob", "Cecil", "David", "Esmeralda")

g <- graph_from_data_frame(relations, directed = FALSE, vertices = my_names)
plot(g)

导致:

enter image description here