簡體   English   中英

ggraph 邊緣連接錯誤?

[英]ggraph edges are connecting wrong?

我正在生成分層邊緣 plot,其中邊緣的顏色/透明度/厚度隨connect dataframe 中的列(pvalue)而變化,但是我生成的 plot 中邊緣的顏色/透明度/厚度並不總是 map 到列 (pvalue) 中的值。 例如,子組 1 和子組 4 應該具有最強最粗的連接(p 值是 E-280),而實際上它們沒有,而子組 3 和子組 4 之間的連接看起來最強。

在此處輸入圖像描述

此數據生成一個可重現的示例:

> dput(vertices)
structure(list(name = structure(c(3L, 1L, 2L, 4L, 5L, 6L, 7L), .Label = c("gp1", 
"gp2", "origin", "subgroup1", "subgroup2", "subgroup3", "subgroup4"
), class = "factor"), id = c(NA, NA, NA, 1L, 2L, 3L, 4L), angle = c(NA, 
NA, NA, 0, -90, 0, -90), hjust = c(NA, NA, NA, 1, 1, 1, 1)), row.names = c(NA, 
-7L), class = "data.frame")
> dput(hierarchy)
structure(list(from = structure(c(3L, 3L, 1L, 1L, 2L, 2L), .Label = c("gp1", 
"gp2", "origin"), class = "factor"), to = structure(1:6, .Label = c("gp1", 
"gp2", "subgroup1", "subgroup2", "subgroup3", "subgroup4"), class = "factor")), class = "data.frame", row.names = c(NA, 
-6L))
> dput(connect)
structure(list(from = structure(c(1L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L), .Label = c("subgroup1", "subgroup2", "subgroup3"), class = "factor"), 
    to = structure(c(1L, 2L, 2L, 1L, 3L, 3L, 3L, 3L), .Label = c("subgroup2", 
    "subgroup3", "subgroup4"), class = "factor"), pvalue = c(1.68e-204, 
    1.59e-121, 9.32e-73, 9.32e-73, 1.59e-21, 9.32e-50, 9.32e-40, 
    9.32e-280)), class = "data.frame", row.names = c(NA, -8L))

這是我用來制作這個例子 plot 的代碼:

from <- match( connect$from, vertices$name)
to <- match( connect$to, vertices$name)
col <- connect$pvalue


#Let's add information concerning the label we are going to add: angle, horizontal adjustement and potential flip
#calculate the ANGLE of the labels
vertices$id <- NA
myleaves <- which(is.na( match(vertices$name, hierarchy$from) ))
nleaves <- length(myleaves)
vertices$id[ myleaves ] <- seq(1:nleaves)
vertices$angle <- 90 - 360 * vertices$id / nleaves
# calculate the alignment of labels: right or left
# If I am on the left part of the plot, my labels have currently an angle < -90
vertices$hjust <- ifelse( vertices$id < 41, 1, 0)
# flip angle BY to make them readable
vertices$angle <- ifelse(vertices$angle < -90, vertices$angle+180, vertices$angle)

mygraph <- graph_from_data_frame( hierarchy, vertices=vertices )

ggraph(mygraph, layout = 'dendrogram', circular = TRUE) + 
  geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05), size = 2, alpha = 0.8) +
  geom_conn_bundle(data = get_con(from = from, to = to, col = col), aes(colour=col, alpha = col, width = col))  +
  geom_node_text(aes(x = x*1.1, y=y*1.1, filter = leaf, label=name, angle = angle, hjust=hjust), size=3.5, alpha=0.6) +scale_edge_color_continuous(trans = "log",low="red", high="yellow")+ scale_edge_alpha_continuous(trans = "log",range = c(1, 0.1)) +scale_edge_width_continuous(trans = "log", range = c(4, 1))+
  theme_void() 

我認為某處有錯誤的映射,但我不知道在哪里。 非常感謝您的投入!

我相信這個庫中有一個錯誤。 按選擇的列(在我的例子中是 pvalue)按升序重新排列輸入數據有幫助,但沒有解決問題。

connect_new <- arrange(connect, pvalue)

我在另一個用戶提交的github 問題中找到了解決方案。 每個組中的子組需要在層次結構和頂點文件中按字母順序排列。 此外,在連接 dataframe 中,需要按照層次結構和頂點文件中的相同順序對子組進行排序。 感謝 zhuxr11

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM