0

I would like create an interactive enrichment map. I have used the emapplot function from the enrichplot package to do this. But this function returns a static plot. I would like to make it interactive using (preferably) the visNetwork package.

The emapplot function returns a ggraph object. I couldn't find a function to change the ggraph object into a igraph/visNetwork/tbl_graph object. So then I thought about extracting the node and edge information.

This can be used to create an example ggraph object.

library(igraph)
library(ggraph)
library(tidyverse)

graph <- graph_from_data_frame(highschool)
graph2 <- ggraph(graph, layout = "kk") + 
    geom_edge_link(aes(colour = factor(year))) + 
    geom_node_point()

attributes(graph2)

Output:

$names
[1] "data"        "layers"      "scales"      "mapping"     "theme"       "coordinates" "facet"       "plot_env"    "labels"     

$class
[1] "ggraph" "gg"     "ggplot"

Node positions can be obtained using graph2$data.

           x          y name .ggraph.orig_index circular .ggraph.index
1  0.7326425  2.3767052    1                  1    FALSE             1
2  0.7041666  2.9986454    2                  2    FALSE             2
3  1.5125110  2.9624914    3                  3    FALSE             3
4 -2.7467842 -1.0804763    4                  4    FALSE             4
5 -2.8196405  0.2369667    5                  5    FALSE             5
6 -2.6184244  2.1968979    6                  6    FALSE             6

Now I need to know now to get the edge information.

I saw ggraph::get_edges is used by functions like geom_edge_link to retrieve edge information. I couldn't get this to work unfortunately.

Does someone know how to get edge information from a ggraph object, or change it into an igraph object?

Edit
On request of camille, here is an example using the emapplot function.

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install("enrichplot")

library(DOSE)
library(ggnewscale)
library(ggraph)
library(igraph)
library(enrichplot)

data(geneList)
de <- names(geneList)[1:100]
x <- enrichDO(de)
x2 <- pairwise_termsim(x)
graph <- emapplot(x2)
lars22998
  • 1
  • 1
  • I'm not familiar with enrichplot, but just to clarify, you're starting with a graphic/ggplot object (from ggraph) and trying to extract the graph data from it as an igraph object? Or a data frame? It might help if you include the `emapplot` part so we know what you have at that point, or if there are alternatives that let you maintain the igraph object – camille Dec 27 '21 at 17:49
  • I've only found one R package for enrichment plots. The output of `emapplot` is a ggraph obejct. For the example, I've provided a 'simple' ggraph object. I'm trying to make it interactive using visNetwork. So I have to go from this ggraph object to a visNetwork object. I can do this if I have an igraph object, or if I have the node/edge information. But I can't figure out how to transform a ggraph object into a igraph object. I also can't figure out how to extract edge information. I'll provide an example using the emapplot function. – lars22998 Dec 28 '21 at 08:31

1 Answers1

0

Found it!

Let's say you have a ggobject called x. You can extract the node and edge info as a tbl_graph object (This is a wrapper around an igraph object) like this:

graph_info <- attributes(x$data)$graph

# If you want to plot it using visNetwork
visNetwork::visIgraph(graph_info)
lars22998
  • 1
  • 1