我试图在层次结构边缘捆绑上运行一个简单的代码,并收到以下错误:
library(ggraph)
library(igraph)
library(tidyverse)
# create a data frame giving the hierarchical structure of your individuals
d1=data.frame(from="origin", to=paste("group", seq(1,10), sep=""))
d2=data.frame(from=rep(d1$to, each=10), to=paste("subgroup", seq(1,100), sep="_"))
hierarchy=rbind(d1, d2)
# create a dataframe with connection between leaves (individuals)
all_leaves=paste("subgroup", seq(1,100), sep="_")
connect=rbind( data.frame( from=sample(all_leaves, 100, replace=T) , to=sample(all_leaves, 100, replace=T)), data.frame( from=sample(head(all_leaves), 30, replace=T) , to=sample( tail(all_leaves), 30, replace=T)), data.frame( from=sample(all_leaves[25:30], 30, replace=T) , to=sample( all_leaves[55:60], 30, replace=T)), data.frame( from=sample(all_leaves[75:80], 30, replace=T) , to=sample( all_leaves[55:60], 30, replace=T)) )
connect$value=runif(nrow(connect))
# create a vertices data.frame. One line per object of our hierarchy
vertices = data.frame(
name = unique(c(as.character(hierarchy$from), as.character(hierarchy$to))) ,
value = runif(111)
)
# Let's add a column with the group of each name. It will be useful later to color points
vertices$group = hierarchy$from[ match( vertices$name, hierarchy$to ) ]
# Create a graph object
mygraph <- graph_from_data_frame( hierarchy, 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)
# Basic graph
p=ggraph(mygraph, layout = 'dendrogram', circular = TRUE) +
geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05)) +
theme_void()
p + geom_conn_bundle(data = get_con(from = from, to = to, value=connect$value), aes(colour=value, alpha=value))
然后我运行此代码,我得到:错误:data
必须具有唯一的名称,但是具有重复的列
此代码是以下示例代码:https://www.r-graph-gallery.com/310-custom-hierarchical-edge-bundling/ 但这不起作用! 我在github上找到了另一个类似的代码 (网址为https://github.com/thomasp85/ggraph/issues/122),就可以了! 因此,任何可以帮助我在上述r-graph-gallery上修复代码的人。我是使用R的初学者。非常感谢!
,如果代码有效,则图形应如下所示: enter image description here