根据包含参与者之间关系的数据框的列更改边缘厚度

Change edge thickness according to a column of a dataframe containing relations between actors

此代码根据参与者和关系的数据帧绘制图表。

library(igraph)
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
                            "Esmeralda"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
                               "David", "Esmeralda"),
                        to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
                        friendship=c(4,15,5,2,11,1))
g <- graph_from_data_frame(relations, directed=TRUE, vertices=actors)

plot(g)

结果是:

我想根据 relations$friendship 的值更改弧的厚度(而不是长度)。

这可以非常简单地实现。 首先你的代码:

library(igraph)

actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
                            "Esmeralda"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
                               "David", "Esmeralda"),
                        to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
                        friendship=c(4,15,5,2,11,1))
g <- graph_from_data_frame(relations, directed=TRUE, vertices=actors)

现在让我们创建一个图形,粗细为 2,线条颜色为黑色,线条类型为 2。

plot(g, edge.width = 2, edge.color = "black", edge.lty = 2)

当然可以随意更改

plot(g, edge.width = 5, edge.color = "black", edge.lty = 1)

希望这就是你的意思。

小更新

最后,您可能想要找到元素的坐标。你可以这样做:

coords = layout_nicely(g)
coords[5,]=c(20, 20)
coords

输出

         [,1]     [,2]
[1,] 15.21285 18.97650
[2,] 15.18511 20.08411
[3,] 14.21575 19.70269
[4,] 16.17453 19.75255
[5,] 20.00000 20.00000

情节

plot(g, layout=coords, edge.width = 5, edge.color = "black", edge.lty = 1)

您还可以设置其他抠图属性

plot(g, vertex.size = 20, vertex.color = "red", vertex.shape = "square", 
     edge.width = 3, edge.color = "black", edge.lty = 3, 
     edge.label = relations$friendship, edge.curved = TRUE)

另请注意,这些参数中的任何一个也可以来自矢量。 因此厚度来自变量friendship没有障碍,例如

plot(g, vertex.size = 20, vertex.color = "red", 
     vertex.shape = c("square","circle","square","circle","rectangle"), 
     edge.width = relations$friendship, edge.color = "black", edge.lty = c(1,2,3,1,2,3), 
     edge.label = 10:20, edge.curved = TRUE)

尝试

plot(g,edge.width = E(g)$friendship, edge.arrow.size = E(g)$friendship)


请注意,E(g)$friendship 的第一个值分配给 edge.arrow.size,而不是向量。也许改进后的功能会添加到未来的 igraph 版本中。

arrow.size The size of the arrows. Currently this is a constant, so it is the same for every edge. If a vector is submitted then only the first element is used, ie. if this is taken from an edge attribute then only the attribute of the first edge is used for all arrows. This will likely change in the future.

The default value is 1.

arrow.width The width of the arrows. Currently this is a constant, so it is the same for every edge. If a vector is submitted then only the first element is used, ie. if this is taken from an edge attribute then only the attribute of the first edge is used for all arrows. This will likely change in the future.

This argument is currently only used by plot.igraph.

The default value is 1, which gives the same width as before this option appeared in igraph.