在圆形分层树状图中设置偏移和闪避

Setting offset and dodging in a circular hierarchical dendrogram

我一直在尝试使用 ggraph 包创建具有分层边缘捆绑的树状图,并且 运行 分为 2 个主要问题。

为清楚起见,问题和代码略有修改

这是一个可重现的例子:

library(ggraph)
#> Loading required package: ggplot2
library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
library(ggplot2)

# hierarchical structure
d1 <- data.frame(from="origin",
                 to=paste("group",
                          seq(1,4),
                          sep = ""))
d2 <- data.frame(from = rep(d1$to,
                          each = 9),
                 to = paste("subgroup",
                          seq(1,36),
                          sep = "_"))
hierarchy <- rbind(d1, d2)

# vertices data.frame 
vertices <- data.frame(name = unique(c(as.character(hierarchy$from),
                                       as.character(hierarchy$to)))) 

# graph object with igraph
mygraph <- graph_from_data_frame(hierarchy,
                                 vertices = vertices)

# PROBLEM 1: setting offset has no effect, and graph seems to
# always start from an arbitrary angle rather than pi/2
ggraph(mygraph,
       layout = 'dendrogram',
       circular = TRUE,
       offset = pi) + 
  geom_edge_diagonal(alpha = 0.1) +

# PROBLEM 2: overlapping connections cannot be dodged without shifting 
# the point of connection on the vertices. e.g., position_dodge() with
# any width shifts the points of connection as well.
  geom_conn_bundle(data = get_con(from = c(6, 6, 12, 20, 25, 30),
                                  to = c(41, 41, 15, 25, 32, 39)),
                   
                   position = position_dodge(width = 0.2),
                   alpha = 1,
                   width = 1,
                   colour = c(rep("#FFFF00", 100),
                              rep("#FF0000", 100),
                              rep("#0000FF", 100),
                              rep("#00FF00", 100),
                              rep("#00FFFF", 100),
                              rep("#FF00FF", 100)),
                   tension = 0.9) +
  theme_void()
#> Warning: position_dodge requires non-overlapping x intervals

reprex package (v2.0.1)

于 2021-11-03 创建

感谢阅读!

更新: 我可以手动更改圆上的顶点位置,只需使用 create_layout() 创建布局,然后做一些三角函数并更改位置值。它确实解决了问题 1,但需要大量工作。不过我想不出关于问题 2 的任何事情。

对于第一个问题,我刚刚使用 create_layout() 获取布局文件,在将文件发送到 ggraph() 之前手动修改顶点的位置(做一些基本的三角函数)。对于第二个问题,我只是找到所有重叠边并将连接数据帧拆分为非重叠边的数据帧,然后将它们分别传递给 geom_conn_bundle() 具有不同张力值的函数。如果有人能对第一个问题提出更好的答案,那就太好了! Here's 存储库。