我一直在嘗試使用ggraph包創建具有分層邊緣捆綁的樹狀圖,但遇到了兩個主要問題。
為清晰起見,問題和代碼略有修改
- 首先,圖形似乎總是從圓上的任意點開始,而默認應該是 90 度(12 點鐘方向)。為偏移引數設定不同的值,應該傳遞給
[layout_tbl_graph_dendrogram()]2也沒有效果。由紅色和黃色邊緣連接的頂點 6 和 41 應該是第一個和最后一個頂點,并且接近 90 度。 - 第二個問題是我希望重疊邊緣(那些連接相同頂點的邊緣)稍微偏移,但通常的 ggplot2 函式,例如
position_dodge()移動整個邊緣。我不希望邊連接到頂點的任何移動,但沒有position_dodge()或類似的功能(或有position_dodge(width=0))紅色邊完全覆寫黃色,因為它們共享相同的兩個頂點(6 和 41)。
這是一個可重現的示例:
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 包(v2.0.1)于 2021 年 11 月 3 日創建
謝謝閱讀!
Update: I can change vertex positions on the circle manually, just creating the layout using the create_layout() and then doing some trigonometry and changing the position values. It does solve problem1 but takes quite a bit of work. I can't think of anything regarding problem2 though.
uj5u.com熱心網友回復:
對于第一個問題,我最終使用了布局檔案create_layout(),在將檔案發送到ggraph(). 對于第二個問題,我只是找到了所有重疊的邊并將連接資料幀拆分為非重疊邊的資料幀,然后將它們geom_conn_bundle()分別傳遞給具有不同張力值的函式。不過,如果有人能對第一個問題提出更好的答案,那就太好了!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/350048.html
標籤:r ggplot2 dendrogram ggraph
上一篇:R:按條件和平均圖疊加密度圖
