分层边束:geom_node_text 仅可视化来自连接的文本

问题描述 投票:0回答:1

我有两张图,第一张图是分类图,第二张图代表第二级连接。 完美的图是分层边束。 但是,我只需要可视化第二级连接中的文本 我的数据:

hierarchy=data.frame(from=c("1","1","1","1","1.1","1.1","1.2","1.2","1.3","1.3","1.4","1.4"),
                    to=c("1.1","1.2","1.3","1.4","1.1.1","1.1.2","1.2.1","1.2.2","1.3.1","1.3.2","1.4.1","1.4.2"))
rel = data.frame(from=c("1.3.1","1.3.1","1.4.2","1.4.2"),
                 to=c("1.1.2","1.3.2","1.2.1","1.1.1"))

通常使用“cactustree”布局制作“分层边缘束”的代码是:

hierarchy_gr = as_tbl_graph(hierarchy)
vertices_hierarchy = hierarchy_gr %>% V()
from <- match( rel$from, vertices_hierarchy$name)
to <- match( rel$to, vertices_hierarchy$name)
ggraph(hierarchy_gr, 'cactustree',upright=TRUE) + 
#  geom_node_circle(aes(fill = depth), size = 0.25, alpha = 0.2) + 
  geom_conn_bundle(data = get_con(from = from, to = to), colour="red",linewidth=1,tension = 1) +
  geom_node_text(aes(label = name, size=0.5),check_overlap = TRUE) +
  theme(legend.position = "none") +
  coord_fixed()

结果是:

但我想要这样的东西:

我不知道怎么做这个。我从 stackoverflows 上读到了一些,但也许我现在已经溢出了!

谢谢你

r hierarchy ggraph
1个回答
0
投票

我发现这有效:

ggraph(hierarchy_gr, 'cactustree',upright=TRUE) + 
  geom_conn_bundle(data = get_con(from = from, to = to, n = n, attr = attr),aes(edge_color=attr,edge_width=n),tension = 1) +
  geom_node_text(aes(label = name,filter=name=="1.3.1"|name=="1.4.2", size=0.5),check_overlap = TRUE) +
  theme(legend.position = "none") +
  coord_fixed()

好的:

ggraph(hierarchy_gr, 'cactustree',upright=TRUE) + 
  geom_conn_bundle(data = get_con(from = from, to = to, n = n, attr = attr),aes(edge_color=attr,edge_width=n),tension = 1) +
  geom_node_text(aes(label = name,filter=name %in% rel$from, size=0.5),check_overlap = TRUE) +
  geom_node_text(aes(label = name,filter=name %in% rel$to, size=0.5),check_overlap = TRUE) +
  theme(legend.position = "none") +
  coord_fixed()

ça 马尔凯!!你呸呸呸!

© www.soinside.com 2019 - 2024. All rights reserved.