我找到了使用此post中的edgebundleR
包在R中创建绘图的代码,我非常喜欢结果。但是,我似乎找不到找到将图例添加到情节的方法。 R中的Legend
函数会将图例添加到原始igraph
图中,但是一旦应用了edgebundle
函数,它将无法继续执行。我的目标是如下图所示,以指示每个彩色组的名称。有什么办法可以在图中添加图例?
Edgebundle Example with Legend
library(edgebundleR)
library(igraph)
library(data.table)
ID<- c("X1","X2","X4","X5","X6","X1","X2","X3","X4","X6","X2","X3","X4","X6","X1","X3","X4","X6")
Con<- c(1L,2L,4L,5L,6L,1L,2L,3L,4L,6L,2L,3L,4L,6L,1L,3L,4L,6L)
Grp<-c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L)
d <- structure(list(ID, Con, Grp),
.Names = c("ID", "Con", "Grp"), class = "data.frame",
row.names = c(NA, -18L))
#add Grp to ID
d$key <- d$ID
d$ID <- paste0(d$Grp,".",d$ID)
# Get vertex relationships
Cons <- unique(d$Con[duplicated(d$Con)])
rel <- vector("list", length(Cons))
for (i in 1:length(Cons)) {
rel[[i]] <- as.data.frame(t(combn(subset(d, d$Con ==Cons[i])$ID, 2)))
}
rel <- rbindlist(rel)
# Get the graph
g <- graph.data.frame(rel, directed=F, vertices=d)
clr <- as.factor(V(g)$Grp)
levels(clr) <- c("salmon", "sandybrown", "darkblue","mediumseagreen","cornflowerblue","black")
V(g)$color <- as.character(clr)
# Plot
plot(g, layout = layout.circle, vertex.size=degree(g)*5, vertex.label="NA")
edgebundle( g, padding = 120, fontsize = 10, nodesize = c(5,10) )->eb
eb