在R(NetworkD3)中使用ForceNetwork通过相关性对边着色?

cgvd09ve  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(190)

我使用igraph_to_networkD3()转换了我的igraph对象,以便能够使用forceNetwork绘图。然而,我的V3列中有-1和1,表示与我的igraph对象的负相关和正相关,我不再能像在simpleNetwork中那样通过相关性为边缘着色。
我尝试将列添加到links data.frame中,但是链接现在的顺序与它们在原始igraph对象中的顺序不同,因此相关性不再与正确的边匹配。另外,我的igraph对象中的原始边列表不是数字的,所以我不能在forceNetwork中使用它。
如何通过相关性来着色,而无需手动查看每个边缘?我希望能够使用forceNetwork而不是simpleNetwork来更改节点大小并添加图例。
这是我的原始igraph对象的样子:

IGRAPH 6384c93 UN-- 52 88 -- 
+ attr: name (v/c), X3 (e/c), color (e/c)
+ edges from 6384c93 (vertex names):
[1] k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rickettsiales;f__mitochondria;__--k__Bacteria;p__Firmicutes;c__Bacilli;o__Bacillales;f__Paenibacillaceae;__                                        
[2] k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rickettsiales;f__mitochondria;__--k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Xanthomonadales;f__Xanthomonadaceae;g__Stenotrophomonas  
[3] k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rickettsiales;f__mitochondria;__--k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rhizobiales;__;__                                        
[4] k__Bacteria;p__Cyanobacteria;c__Chloroplast;o__Streptophyta;f__;g__                     --k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Sphingomonadales;f__Sphingomonadaceae;__                 
[5] k__Bacteria;p__Cyanobacteria;c__Chloroplast;o__Streptophyta;f__;g__                     --k__Bacteria;p__Bacteroidetes;c__Sphingobacteriia;o__Sphingobacteriales;f__Sphingobacteriaceae;g__Mucilaginibacter
X1
1                           k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rickettsiales;f__mitochondria;__
2                           k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rickettsiales;f__mitochondria;__
3                           k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rickettsiales;f__mitochondria;__
4                                                k__Bacteria;p__Cyanobacteria;c__Chloroplast;o__Streptophyta;f__;g__
5                                                k__Bacteria;p__Cyanobacteria;c__Chloroplast;o__Streptophyta;f__;g__

X2 X3
1                                          k__Bacteria;p__Firmicutes;c__Bacilli;o__Bacillales;f__Paenibacillaceae;__ -1
2    k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Xanthomonadales;f__Xanthomonadaceae;g__Stenotrophomonas -1
3                                          k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rhizobiales;__;__ -1
4                   k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Sphingomonadales;f__Sphingomonadaceae;__ -1
5  k__Bacteria;p__Bacteroidetes;c__Sphingobacteriia;o__Sphingobacteriales;f__Sphingobacteriaceae;g__Mucilaginibacter -1

这是我的新networkd 3边缘列表的样子:

source target
22  31          
29  31          
8   19          
3   36          
6   36          
20  36          
12  36          
37  43          
26  43          
38  43

这是我的代码

# Create a networkD3 graph object from igraph object, group by bacteria vs fungi
netd3 <- igraph_to_networkD3(sparcc.graph, group = ifelse(grepl("Bacteria", V(sparcc.graph)$name), "Bacteria", "Fungi"))

# Add node size based on betweenness to nodes
netd3$nodes <- data.frame(cbind(netd3$nodes, nodesize = sqrt(betweenness(sparcc.graph)/3)))

# Create a color scale for plotting
color_func <- ('d3.scaleOrdinal().range(["#AC88FF", "#E7861B"]);')

# Generate an interactive visualization using forceNetwork
forceNetwork(Links = netd3$links, Nodes = netd3$nodes, Source = "source", Target = "target", NodeID = "name", Group = "group", legend = TRUE, colourScale = JS(color_func), Nodesize = "nodesize", zoom = TRUE,)
zujrkrfu

zujrkrfu1#

igraph::get.edge.attribute()可能会有帮助?

library(igraph)
library(networkD3)

mydata <-
  data.frame(
    X1 = c(
      "k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rickettsiales;f__mitochondria;__",
      "k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rickettsiales;f__mitochondria;__",
      "k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rickettsiales;f__mitochondria;__",
      "k__Bacteria;p__Cyanobacteria;c__Chloroplast;o__Streptophyta;f__;g__",
      "k__Bacteria;p__Cyanobacteria;c__Chloroplast;o__Streptophyta;f__;g__"
    ),
    X2 = c(
      "k__Bacteria;p__Firmicutes;c__Bacilli;o__Bacillales;f__Paenibacillaceae;__",
      "k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Xanthomonadales;f__Xanthomonadaceae;g__Stenotrophomonas",
      "k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rhizobiales;__;__",
      "k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Sphingomonadales;f__Sphingomonadaceae;__",
      "k__Bacteria;p__Bacteroidetes;c__Sphingobacteriia;o__Sphingobacteriales;f__Sphingobacteriaceae;g__Mucilaginibacter"
    ),
    X3 = c(
      -1,
      -1,
      1,
      1,
      1
    )
  )

sparcc.graph <- graph.data.frame(mydata, directed = FALSE)

netd3 <- igraph_to_networkD3(sparcc.graph, group = ifelse(grepl("Bacteria", V(sparcc.graph)$name), "Bacteria", "Fungi"))

netd3$links$X3 <- get.edge.attribute(sparcc.graph, "X3")

netd3
#> $links
#>   source target value X3
#> 1      1      6     1 -1
#> 2      0      2    -1 -1
#> 3      0      4     1  1
#> 4      1      5     1  1
#> 5      0      3    -1  1
#> 
#> $nodes
#>                                                                                                                name
#> 1                          k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rickettsiales;f__mitochondria;__
#> 2                                               k__Bacteria;p__Cyanobacteria;c__Chloroplast;o__Streptophyta;f__;g__
#> 3                                         k__Bacteria;p__Firmicutes;c__Bacilli;o__Bacillales;f__Paenibacillaceae;__
#> 4   k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Xanthomonadales;f__Xanthomonadaceae;g__Stenotrophomonas
#> 5                                         k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rhizobiales;__;__
#> 6                  k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Sphingomonadales;f__Sphingomonadaceae;__
#> 7 k__Bacteria;p__Bacteroidetes;c__Sphingobacteriia;o__Sphingobacteriales;f__Sphingobacteriaceae;g__Mucilaginibacter
#>      group
#> 1 Bacteria
#> 2 Bacteria
#> 3 Bacteria
#> 4 Bacteria
#> 5 Bacteria
#> 6 Bacteria
#> 7 Bacteria

相关问题