在visNetwork中添加边缘颜色的图例

时间:2016-07-27 01:20:40

标签: r visualization legend edges

我正在使用visNetwork来可视化图形,但我需要根据边缘颜色引入图例。边缘颜色取决于边缘属性及其动态性质。我尝试使用visGroups / visLegend,但遇到了多个错误。 PFB可重复的例子。

    library(igraph)
    library(visNetwork)
    gg <- graph.atlas(711)
    V(gg)$name=1:7
    gg=set_edge_attr(gg,"Department",E(gg)[1:10],c("A","B","C","A","E","C","G","B","C","A"))
    E(gg)$label=E(gg)$Department
    F2 <- colorRampPalette(c("red", "blue","orange","violet","cyan"), bias = length(unique(E(gg)$Department)), space = "rgb", interpolate = "linear")
    colCodes <- F2(length(unique(E(gg)$Department)))
    edges_col <- sapply(E(gg)$Department,function(x) colCodes[which(sort(unique(E(gg)$Department)) == x)])
    E(gg)$color <-edges_col
    datatest = toVisNetworkData(gg)
    visNetwork(datatest$nodes,datatest$edges) %>% visIgraphLayout(layout="layout_in_circle") 

我需要传说为红色 - A,蓝色 - C等。

请帮助。

1 个答案:

答案 0 :(得分:4)

visLegend首先基于节点组,但您也可以手动设置节点和/或边缘图例。 (?visLegend

例如:

# data.frame from edges legend
ledges <- data.frame(color = unique(edges_col), 
                     label = unique(names(edges_col))) 

visNetwork(datatest$nodes,datatest$edges) %>% visIgraphLayout(layout="layout_in_circle") %>%
  visLegend(useGroups = F, addEdges = ledges)