问题描述
我正在生成一个层次化边缘图,其中边缘的颜色/透明度/厚度根据我的connect
数据框中的列(pvalue)而变化,但是该图中边缘的颜色/透明度/厚度生成的值并不总是映射到列(pvalue)中的值。例如,subgroup1和subgroup4应该具有最强的最粗连接(pvalue为E-280),而实际上却没有,而subgroup3和subgroup4之间的连接看起来最牢固。
此数据生成可重复的示例:
> 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,3L,4L),angle = c(NA,-90,-90),hjust = c(NA,1,1)),row.names = c(NA,-7L),class = "data.frame")
> dput(hierarchy)
structure(list(from = structure(c(3L,2L),"origin"),to = structure(1:6,"subgroup4"),class = "factor")),class = "data.frame",-6L))
> dput(connect)
structure(list(from = structure(c(1L,1L),.Label = c("subgroup1","subgroup3"),to = structure(c(1L,3L),.Label = c("subgroup2",pvalue = c(1.68e-204,1.59e-121,9.32e-73,1.59e-21,9.32e-50,9.32e-40,9.32e-280)),-8L))
这是我用来制作此示例图的代码:
from <- match( connect$from,vertices$name)
to <- match( connect$to,vertices$name)
col <- connect$pvalue
#Let's add @R_212_4045@ion 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,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 issue中找到了解决方案。每个组内的子组需要在层次结构和顶点文件中按字母顺序排序。此外,在连接数据框中,子组需要按照层次结构和顶点文件中的相同顺序进行排序。感谢zhuxr11