visNetwork / visLegend颜色不匹配

时间:2017-02-22 19:40:44

标签: r rstudio

我正在RStudio制作一个情节:

library(visNetwork)
palette("default")
nodes<-data.frame(id=rep(1:10, 1), group=rep(1:5,2), color=rep(c("red","green", "blue", "black", "cyan"), 2))
nodes$label <- nodes$id
edges <- data.frame(from=round(runif(10)*10, 0), to=round(runif(10)*10, 0), arrows="to")
graphPlot <- visNetwork(nodes, edges, main = "System Dependency Diagram")
visLegend(graphPlot, stepY=25)

绘制图例和图形时,类别是正确的,但图例颜色与图表上的颜色不同。我尝试将特定的调色板分配给具有相同结果的节点和边缘。

1 个答案:

答案 0 :(得分:2)

visLegend中的两种可能性:

1 - 使用组定义,(默认)。在这种情况下,必须使用visGroups而不是直接在节点data.frame

中设置所有组参数
library(visNetwork)
palette("default")
nodes<-data.frame(id=rep(1:10, 1), group=rep(1:5,2))
nodes$label <- nodes$id
edges <- data.frame(from=round(runif(10)*10, 0), to=round(runif(10)*10, 0), arrows="to")
graphPlot <- visNetwork(nodes, edges, main = "System Dependency Diagram") %>%
  visGroups(groupname = "1", color = "red") %>%
  visGroups(groupname = "2", color = "green") %>%
  visGroups(groupname = "3", color = "blue") %>%
  visGroups(groupname = "4", color = "black") %>%
  visGroups(groupname = "5", color = "cyan") %>%
  visLegend(stepY=25)
graphPlot

1 - 定义自定义图例

library(visNetwork)
palette("default")
nodes<-data.frame(id=rep(1:10, 1), group=rep(1:5,2), color=rep(c("red","green", "blue", "black", "cyan"), 2))
nodes$label <- nodes$id
edges <- data.frame(from=round(runif(10)*10, 0), to=round(runif(10)*10, 0), arrows="to")

lnodes <- data.frame(id = 1:5, label = c(1:5), color = c("red","green", "blue", "black", "cyan")) 

visNetwork(nodes, edges, main = "System Dependency Diagram") %>%
  visLegend(useGroups = FALSE, addNodes = lnodes, stepY=25)