如何将geom_conn_bundle生成的坐标与图顶点名称关联映射?
问题描述
我正在尝试找到一种方法,将通过geom_conn_bundle生成的坐标与图的顶点名称进行映射。以下是我的示例数据与实现代码:
d1 <- data.frame(from="origin", to=paste("Dept", seq(1,5), sep="")) d1 <- rbind(d1,c('origin','application')) d2<-data.frame(from=c('Dept1','Dept2','Dept3','Dept4','Dept5','application','application','application'),to=c('example1@abc.com','example2@abc.com','example3@abc.com','example4@abc.com','example5@abc.com','App1','App2','App3')) edges <-rbind(d1,d2) d1<- data.frame(from='origin',to=paste("Dept",seq(1,5),sep="")) d_1 <- data.frame(from=rep(d1$to,each=5),to=paste(paste('email',seq(1,100),sep=""),'@abc.com',sep="")) d_1<-sample_n(d_1, 20) d_2 <- data.frame(from=rep('application',each=5),to=paste('App',seq(1,100),sep="")) d_2<-sample_n(d_2, 20) d1 <- rbind(d1,c('origin','application')) d2<-rbind(d_1,d_2) edges <-rbind(d1,d2) all_leaves<- unique(d2$to) connect<- rbind( data.frame(from=sample(head(d_1$to),15,replace=T), to=sample(tail(d_2$to),15,replace=T)), data.frame(from=sample(tail(d_1$to),15,replace=T), to=sample(head(d_2$to),15,replace=T)), data.frame(from=sample(d_1$to,15,replace=T), to=sample(d_2$to,15,replace=T)), data.frame(from=sample(d_1$to,15,replace=T), to=sample(d_2$to,15,replace=T)) ) connect$value <- runif(nrow(connect)) #connect vertices <- data.frame( name = unique(c(as.character(edges$from), as.character(edges$to))) ) vertices$value <-runif(nrow(vertices)) vertices$group <-edges$from[ match( vertices$name, edges$to ) ] vertices$id <- NA myleaves <- which(is.na(match(vertices$name, edges$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$angle < -90, 1, 0) # flip angle BY to make them readable vertices$angle <- ifelse(vertices$angle < -90, vertices$angle+180, vertices$angle) mygraph <- igraph::graph_from_data_frame( edges, vertices=vertices ) # The connection object must refer to the ids of the leaves: from <- match( connect$from, vertices$name) to <- match( connect$to, vertices$name) options(repr.plot.width=15, repr.plot.height=8) # Basic usual argument p<-ggraph(mygraph, layout = 'dendrogram', circular = TRUE) + geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05,colour=group, size=value, alpha=0.2)) + geom_conn_bundle(data = get_con(from = from, to = to), alpha=0.2, colour="skyblue", width=0.9) + geom_node_text(aes(x = x*1.1, y=y*1.1, filter = leaf, label=name, angle = angle, hjust=hjust), size=4, alpha=1.5) + theme_void() + theme( legend.position="none", plot.margin=unit(c(0,0,0,0),"cm"), ) + expand_limits(x = c(1, 1), y = c(1, 1)) #expand_limits(x = c(-1.2, 1.2), y = c(-1.2, 1.2)) p pg <- ggplot_build(p)
我希望获取通过geom_conn_bundle计算出的每个坐标对应的起点和终点顶点名称,这些坐标可通过pg[[1]][[2]]访问。
解决方案
可以通过以下步骤实现坐标与顶点名称的映射:
- 关联连接数据与坐标数据:
pg[[1]][[2]]中的group列对应每条连接的索引,和connect数据框的行索引一一对应,以此作为关联键合并数据。 - 具体代码实现:
# 给connect添加分组列,对应geom_conn_bundle中的group值 connect$group <- seq_len(nrow(connect)) # 提取geom_conn_bundle生成的坐标数据 bundle_coords <- pg[[1]][[2]] # 合并坐标数据与连接信息,得到带顶点名称的坐标数据集 coords_with_names <- merge(bundle_coords, connect, by = "group") # 查看结果,每条坐标点都附带对应起点和终点的名称 head(coords_with_names)
- 说明:合并后的数据集包含坐标(x、y)、分组标识、起点名称(from)、终点名称(to)等信息,每条曲线的所有坐标点都会关联到对应的顶点名称,满足后续分析或可视化的需求。
内容的提问来源于stack exchange,提问作者SAL
相关产品推荐
相关产品推荐

