R ggnetwork 中的节点标签在 plotly 中。

huangapple go评论55阅读模式
英文:

R ggnetwork node labels in plotly

问题

I understand, you'd like the code portion to be translated. Here it is:

我正在尝试绘制一个`network`(一个作为`R igraph`对象的蛋白质相互作用图),它的规模相当大(111个节点,929条边),所以我正在尝试找到一个合理的方法来标记节点,这些节点可以是蛋白质的ID或其编码基因的名称,无论哪种情况,这些标签都是几个字符长。

这是获取蛋白质相互作用‘igraph’对象的代码:
```R
library(STRINGdb)
string.db <- STRINGdb$new(version="11.5",species=9606,score_threshold=200,input_directory="")
string.graph <- string.db$get_graph()
igraph::V(string.graph)$name <- gsub("^9606\\.","",igraph::V(string.graph)$name)

string.graph是整个图,下面我对它进行子集处理,只保留与感兴趣的一对蛋白质相连的节点:

protein1 <- "ENSP00000475261"
protein2 <- "ENSP00000360829"
neighbors1 <- igraph::neighbors(graph = string.graph, v = protein1, mode = "all")
neighbors1 <- neighbors1[which(!is.na(names(neighbors1)))]
neighbors2 <- igraph::neighbors(graph = string.graph, v = protein2, mode = "all")
neighbors2 <- neighbors2[which(!is.na(names(neighbors2)))]
all.neighbors <- unique(c(neighbors1, neighbors2))

string.subgraph.vs <- igraph::V(string.graph)[name %in% c(names(all.neighbors), protein1, protein2)]
string.subgraph.vs.list <- igraph::ego(string.graph, order = 1, nodes = string.subgraph.vs, mode = "all", mindist = 0)
string.subgraph <- igraph::induced_subgraph(string.graph, unlist(string.subgraph.vs.list))

nodes.df <- data.frame(id = names(igraph::V(string.subgraph))) %>%
  unique() %>% dplyr::filter(id %in% c(names(all.neighbors), protein1, protein2)) %>%
  dplyr::mutate(size = 1, color = NA)
nodes.df$color[which(nodes.df$id %in% names(neighbors1) & !(nodes.df$id %in% names(neighbors2)))] <- "cornflowerblue"
nodes.df$color[which(nodes.df$id %in% names(neighbors2) & !(nodes.df$id %in% names(neighbors1)))] <- "#ee2400"
nodes.df$color[which(nodes.df$id %in% names(neighbors1) & nodes.df$id %in% names(neighbors2))] <- "darkorchid1"
nodes.df$color[which(nodes.df$id == protein1)] <- "cornflowerblue"
nodes.df$color[which(nodes.df$id == protein2)] <- "#ee2400"
nodes.df$size[which(nodes.df$id == protein1)] <- 3
nodes.df$size[which(nodes.df$id == protein2)] <- 3

edges.df <- as.data.frame(igraph::get.edgelist(string.subgraph)) %>%
  dplyr::rename(from = V1, to = V2) %>% unique() %>%
  dplyr::filter(from %in% c(names(all.neighbors), protein1, protein2) & to %in% c(names(all.neighbors), protein1, protein2))

new.string.subgraph <- igraph::graph_from_data_frame(d = edges.df, directed = F, vertices = nodes.df)

我明白,这段代码看起来有点繁琐,但我按照这篇博客的方法对igraph对象进行了子集处理。

如果我用Rggplot2ggnetwork包绘制它,并添加节点标签:

ggplot(new.string.subgraph, aes(x = x, y = y, xend = xend, yend = yend)) +
  ggnetwork::geom_edges(color = "grey50", alpha = 0.1) +
  ggnetwork::geom_nodes(color = igraph::vertex_attr(new.string.subgraph)$color, size = igraph::vertex_attr(new.string.subgraph)$size) +
  ggnetwork::geom_nodetext(aes(label = name)) +
  ggnetwork::theme_blank()

结果节点标签比较拥挤:
R ggnetwork 中的节点标签在 plotly 中。

相对于没有节点标签的情况:

ggplot(new.string.subgraph, aes(x = x, y = y, xend = xend, yend = yend)) +
  ggnetwork::geom_edges(color = "grey50", alpha = 0.1) +
  ggnetwork::geom_nodes(color = igraph::vertex_attr(new.string.subgraph)$color, size = igraph::vertex_attr(new.string.subgraph)$size) +
  ggnetwork::theme_blank()

R ggnetwork 中的节点标签在 plotly 中。

顺便说一下,我希望set.seed(1)确实能保证绘图是可重现的。

因此,我想也许我可以使用plotlyplotly::ggplotly())来以悬停特性的形式查看节点标签。但是,我只看到了x和y坐标作为悬停信息,如果我将label分配给plotly::ggplotlytooltip参数,我根本看不到任何悬停信息。

您有没有办法让plotly在图中不显示节点标签但能显示它们作为悬停信息?


<details>
<summary>英文:</summary>
I&#39;m trying to plot a `network` (a protein-protein interaction graph as an `R igraph` object), which is moderately large in size (111 nodes with 929 edges), so I&#39;m trying to find a reasonable way of labeling the nodes, which are either the protein IDs or the names of their encoding genes, where in either case these labels are several characters long.
Here&#39;s the code to obtain the protein-protein interaction &#39;igraph&#39; object:

library(STRINGdb)
string.db <- STRINGdb$new(version="11.5",species=9606,score_threshold=200,input_directory="")
string.graph <- string.db$get_graph()
igraph::V(string.graph)$name <- gsub("^9606\.","",igraph::V(string.graph)$name)


`string.graph` is the entire graph and below I subset it to keep only the nodes connected to a pair of proteins of interest:

protein1 <- "ENSP00000475261"
protein2 <- "ENSP00000360829"
neighbors1 <- igraph::neighbors(graph = string.graph, v = protein1, mode = "all")
neighbors1 <- neighbors1[which(!is.na(names(neighbors1)))]
neighbors2 <- igraph::neighbors(graph = string.graph, v = protein2, mode = "all")
neighbors2 <- neighbors2[which(!is.na(names(neighbors2)))]
all.neighbors <- unique(c(neighbors1, neighbors2))

string.subgraph.vs <- igraph::V(string.graph)[name %in% c(names(all.neighbors), protein1, protein2)]
string.subgraph.vs.list <- igraph::ego(string.graph, order = 1, nodes = string.subgraph.vs, mode = "all", mindist = 0)
string.subgraph <- igraph::induced_subgraph(string.graph, unlist(string.subgraph.vs.list))

nodes.df <- data.frame(id = names(igraph::V(string.subgraph))) %>%
unique() %>% dplyr::filter(id %in% c(names(all.neighbors), protein1, protein2)) %>%
dplyr::mutate(size = 1, color = NA)
nodes.df$color[which(nodes.df$id %in% names(neighbors1) & !(nodes.df$id %in% names(neighbors2)))] <- "cornflowerblue"
nodes.df$color[which(nodes.df$id %in% names(neighbors2) & !(nodes.df$id %in% names(neighbors1)))] <- "#ee2400"
nodes.df$color[which(nodes.df$id %in% names(neighbors1) & nodes.df$id %in% names(neighbors2))] <- "darkorchid1"
nodes.df$color[which(nodes.df$id == protein1)] <- "cornflowerblue"
nodes.df$color[which(nodes.df$id == protein2)] <- "#ee2400"
nodes.df$size[which(nodes.df$id == protein1)] <- 3
nodes.df$size[which(nodes.df$id == protein2)] <- 3

edges.df <- as.data.frame(igraph::get.edgelist(string.subgraph)) %>%
dplyr::rename(from = V1, to = V2) %>% unique() %>%
dplyr::filter(from %in% c(names(all.neighbors), protein1, protein2) & to %in% c(names(all.neighbors), protein1, protein2))

new.string.subgraph <- igraph::graph_from_data_frame(d = edges.df, directed = F, vertices = nodes.df)


A bit cumbersome but I followed [this blog][1] for subsetting an `igraph` object.
If I plot it, using `R`&#39;s `ggplot2` and `ggnetwork` packages, adding the node labels:

ggplot(new.string.subgraph, aes(x = x, y = y, xend = xend, yend = yend)) +
ggnetwork::geom_edges(color = "grey50", alpha = 0.1) +
ggnetwork::geom_nodes(color = igraph::vertex_attr(new.string.subgraph)$color, size = igraph::vertex_attr(new.string.subgraph)$size) +
ggnetwork::geom_nodetext(aes(label = name)) +
ggnetwork::theme_blank()


The outcome is quite crowded with the node labels:
[![enter image description here][2]][2]
Relative to what it looks like without the node labels:

ggplot(new.string.subgraph, aes(x = x, y = y, xend = xend, yend = yend)) +
ggnetwork::geom_edges(color = "grey50", alpha = 0.1) +
ggnetwork::geom_nodes(color = igraph::vertex_attr(new.string.subgraph)$color, size = igraph::vertex_attr(new.string.subgraph)$size) +
ggnetwork::theme_blank()


[![enter image description here][3]][3]
BTW, I&#39;m hoping that the `set.seed(1)` indeed guarantees that the plot is reproducible.  
So, I thought perhaps I&#39;d use `plotly` (`plotly::ggplotly()`) to be able to see the node labels as a hover-over feature. But, all I&#39;m seeing are the x and y coordinates as hover-over and if I assign `label` to the `tooltip` parameter of `plotly::ggplotly` I don&#39;t see any hover-over information at all.
Any idea how to get `plotly` to display the node labels without actually seeing them in the plot?
[1]: https://ourednik.info/maps/2018/09/21/create-a-subgraph-from-the-neighborhood-of-specific-vertices-in-igraph/
[2]: https://i.stack.imgur.com/nsbkk.png
[3]: https://i.stack.imgur.com/OcsT6.png
</details>
# 答案1
**得分**: 1
I wasn't able to load the `STRINGdb` package for the latest version of R (4.3.0). However, have you tried modifying your `plotly` object using `plotly::style`? I'm assuming it would look something like the following:
```{r tag}
p <- ggplot(new.string.subgraph, aes(x = x, y = y, xend = xend, yend = yend)) +
ggnetwork::geom_edges(color = "grey50", alpha = 0.1) +
ggnetwork::geom_nodes(color = igraph::vertex_attr(new.string.subgraph)$color, size = igraph::vertex_attr(new.string.subgraph)$size) +
ggnetwork::theme_blank()
p <- p %>% style(hoverinfo = "name")
ggplotly(p)

You may have to play around with the style arguments. See this plotly page for more details.

英文:

I wasn't able to load the STRINGdb package for the latest version of R (4.3.0). However, have you tried modifying your plotly object using plotly::style? I'm assuming it would look something like the following:

p &lt;- ggplot(new.string.subgraph, aes(x = x, y = y, xend = xend, yend = yend)) +
ggnetwork::geom_edges(color = &quot;grey50&quot;, alpha = 0.1) +
ggnetwork::geom_nodes(color = igraph::vertex_attr(new.string.subgraph)$color, size = igraph::vertex_attr(new.string.subgraph)$size) +
ggnetwork::theme_blank()
p &lt;- p %&gt;% style(hoverinfo = &quot;name&quot;)
ggplotly(p)

You may have to play around with the style arguments. See this plotly page for more details.

huangapple
  • 本文由 发表于 2023年5月11日 06:39:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定