【问题标题】:Repel text from edges in network排斥网络边缘的文本
【发布时间】:2019-08-23 04:04:48
【问题描述】:

在绘制网络时,如果节点的标签也能避开网络边缘,那就太好了。例如。在下面的示例中,可以将所有标签移出网络。我已经尝试了几个包,但到目前为止还没有找到一个 hacky 方法来做到这一点。有办法吗?示例如下:

library(ggraph)
library(tidygraph)
reprex <- tibble(to = sample(1:10, 100,replace=T),
                 from = sample(1:10, 100,replace=T)
                 ) %>%
  as_tbl_graph()
V(reprex)$label1 <- rep("label",10)

reprex_plot <- reprex %>%
  ggraph() +
  geom_node_point() +
  geom_edge_link(color="grey")+
  geom_node_text(aes(label=label1),repel=T,force=100)+
  theme_bw()

reprex_plot

【问题讨论】:

    标签: r visualization graph-visualization ggrepel ggraph


    【解决方案1】:

    据我了解这里的问题,ggrepelgeom_node_text 使用的包,只能访问节点所在的层,而不能“看到”边缘。这使得ggrepel 不太适合网络(或者我遗漏了一些东西)。

    不幸的是,我也没有一个很好的解决方案来解决这个问题,虽然我一直在寻找一个。以下是您(或任何人)如何使用ggraph() 更好地标记标签的两个建议:

    1:文本作为节点

    所以我的一个想法是让网络布局算法为我们完成工作。我制作了另一组仅包含标签的节点。标签节点仅连接到它们标记的网络中的一个相应节点。我们开始:

    library(dplyr)
    library(ggraph)
    library(tidygraph)
    
    set.seed(123)
    
    reprex <- tibble(from = sample(1:10, 100, replace = TRUE),
                     to = sample(1:10, 100, replace = TRUE)) %>%
      as_tbl_graph() %>% 
      activate(edges) %>% 
      mutate(color = "grey")
    

    我在这里添加边缘颜色灰色,因为我们将在最终图中有两种不同的颜色。

    nodes <- reprex %>% 
      activate(nodes) %>% 
      as_tibble() # extract data.frame of nodes
    
    # create new graph with just the lables
    labels <- tibble(from = 1:10,
                     to = 11:20) %>% 
      as_tbl_graph() %>% 
      activate(nodes) %>% 
      mutate(label1 = "label",
             is_label = !name %in% nodes$name) %>% 
      activate(edges) %>% 
      mutate(color = "black")
    
    # join graph and labels
    new_graph <- graph_join(labels, reprex, by = "name")
    

    现在我们有了带有标签节点的新图,我们可以绘图了。请注意,我在新图中添加了一个变量is_label,这样我们就可以使用不同的节点形状并确保只有标签节点被标记:

    reprex_plot <- new_graph %>% 
      ggraph() +
      geom_edge_link(aes(color = color)) +
      geom_node_point(aes(filter = !is_label, shape = "circle"), show.legend = FALSE) +
      scale_edge_color_identity() +
      geom_node_text(aes(filter = is_label, label = label1), hjust = -0.1) +
      theme_void()
    reprex_plot
    

    显然,还有很大的改进空间。标签现在离节点很远。它们仍然与自己的边缘重叠(尽管我认为这可以通过提供更好的 hjust 值来解决)。虽然这适用于自动布局,但其他布局可能会做一些奇怪的事情,具体取决于您的数据。我真的希望其他人提出更好的解决方案。但我想我不妨把它放在这里。也许有人会受到启发。

    2:标签而不是文本

    解决此问题的另一种方法是在文本上使用白色背景。这个解决方案的灵感来自于网络绘图的 GUI 程序如何处理这个问题。我们可以使用ggplot2geom_label 来实现这一点,尽管geom_node_label() 会实现同样的效果。这个解决方案要简单得多,但也有限。这是一个管道中的全部内容:

    tibble(from = sample(1:10, 100, replace = TRUE),
           to = sample(1:10, 100, replace = TRUE))  %>%
      as_tbl_graph() %>% 
      activate(nodes) %>% 
      mutate(label1 = "label") %>%
      ggraph() +
      geom_edge_link(color = "grey") +
      geom_node_point() +
      geom_label(aes(x = x, y = y, label = label1), nudge_y = 0.1, label.size = NA) +
      theme_void()
    

    我删除了标签上的边框并将它们直接放在它们的节点上方 (nudge_y = 0.1)。您的结果可能会因绘图的大小而异,因此您可能需要更改该值。

    在较大的网络上,标签的白框可能会覆盖其他节点。

    【讨论】:

      猜你喜欢
      • 2020-12-16
      • 2018-11-04
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 2021-11-27
      相关资源
      最近更新 更多