简体   繁体   English

使用visEdges进行R visNetwork + igraph加权网络可视化

[英]R visNetwork + igraph weighted network visualization with visEdges

I have a very simple question regarding combining igraph with visNetwork. 关于将igraph与visNetwork结合起来,我有一个非常简单的问题。 I want to weight the edges with visEdges(value=E(graph)$weight), but that does not work. 我想用visEdges(值= E(图形)$ weight)来加权边缘,但这不起作用。 Here is a toy example to illustrate the problem: 这是一个说明问题的玩具示例:

     test
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    1    3    7    1
[2,]    4    0    8    9    5
[3,]   10    3    0    8    3
[4,]    5    1    5    0    7
[5,]    8    2    7    4    0

library(igraph); library(visNetwork)

test.gr <- graph_from_adjacency_matrix(test, mode="undirected", weighted=T)

If I now try to visualize it as a weighted graph, it doesn't plot it: 如果我现在尝试将其可视化为加权图形,则不会绘制它:

test.gr %>%
  visIgraph(layout = "layout_in_circle") %>%
  visEdges(value = E(test.gr)$weight)

If I use 如果我使用

test.gr %>%
  visIgraph(layout = "layout_in_circle") %>%
  visEdges(value = 10)

instead, I get a plot: 相反,我得到一个情节:

在此输入图像描述

but this is of course not what I want. 但这当然不是我想要的。 I want different edge widths according to E(test.gr)$weigth. 根据E(test.gr)$ weigth,我想要不同的边缘宽度。

Can you tell me how I can do this? 你能告诉我怎么做吗?

With visNodes and visEdges you can set global options for all nodes and edges. 使用visNodesvisEdges您可以为所有节点和边设置全局选项 With eg visNodes(shape = "square") , your make all nodes squares. 使用例如visNodes(shape = "square") ,您可以使所有节点成为正方形。 You understand that this is not the right route to set individual edge widths. 您了解这不是设置单个边缘宽度的正确途径。

To achieve what you want, you could convert the igraph object to a visNetwork -list. 要实现您的目标,您可以将igraph对象转换为visNetwork -list。 Then, you could add an 'expected' column with the name "value". 然后,您可以添加名为“value”的“预期”列。 visNetwork will then use that column to give weight to the edges. 然后, visNetwork将使用该列来赋予边缘权重。

Maybe this is not thé best solution, but it works. 也许这不是最好的解决方案,但它确实有效。

test <- as.matrix(read.table(header = FALSE, text = "
    0    1    3    7    1
    4    0    8    9    5
   10    3    0    8    3
    5    1    5    0    7
    8    2    7    4    0"))

library(igraph)
library(visNetwork)

## make igraph object
test.gr <- graph_from_adjacency_matrix(test, mode="undirected", weighted=T)

## convert to VisNetwork-list
test.visn <- toVisNetworkData(test.gr)
## copy column "weight" to new column "value" in list "edges"
test.visn$edges$value <- test.visn$edges$weight

test.visn$edges
# from to weight value
#   V1 V2      4     4
#   V1 V3     10    10
#   V1 V4      7     7
#   V1 V5      8     8
#   V2 V3      8     8
#   V2 V4      9     9
#   V2 V5      5     5
#   V3 V4      8     8
#   V3 V5      7     7
#   V4 V5      7     7

visNetwork(test.visn$nodes, test.visn$edges) %>%
  visIgraphLayout(layout = "layout_in_circle") 

This yields the following graph: 这会产生以下图表:

在此输入图像描述

Please, let me know whether this is what you want. 请告诉我这是否是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM