Reputation: 1
I converted my igraph object using igraph_to_networkD3() to be able to plot using forceNetwork. However, my V3 column which had -1 and 1 indicating negative and positive correlation from my igraph object went away and I can no longer color edges by correlation as I could in simpleNetwork.
I tried adding the column to the links data.frame, but the links are now in a different order as they were in my original igraph object, so the correlation no longer matches with the correct edge. Also, the original edge list from my igraph object is not numerical, so I cannot use it in forceNetwork.
How can I color by correlation without manually looking at every edge? I want to be able to use forceNetwork instead of simpleNetwork to change node size and add a legend.
This is how my original igraph object looks like:
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
This is what my new networkd3 edge list looks like:
source target
22 31
29 31
8 19
3 36
6 36
20 36
12 36
37 43
26 43
38 43
This is my code
# 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,)
Upvotes: 0
Views: 79
Reputation: 8848
Maybe igraph::get.edge.attribute()
will help?
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
Upvotes: 0