不同颜色的节点并删除 visNetwork 中的标签

Color nodes differently and remove label in visNetwork

尝试在 R 中可视化网络,我有一个边和节点列表。节点列表如下所示 - “id”、“label”和“type”:

我想根据类型为节点着色。到目前为止,我通过嵌入 if 语句进行了尝试,但这似乎不起作用。

visNetwork(nodes, edges) %>% 
  visIgraphLayout(layout = "layout_in_circle") %>% 
  visNodes(label = NULL,(
    if (nodes$type ="gimn") { 
      color= "slategrey"
    } else if (nodes$type ="szakgimn") {
      color="pink"
    } else if  (nodes$type ="ált_isk") {
      color="black"
    } else {
      color ="tomato"
    })) %>% 
  visEdges(arrows = "middle")

如何根据类型给节点分配颜色?还有如何从情节中删除标签? (上面的代码似乎也不起作用。)

我猜你可以尝试嵌套 ifelse 来定义基于类型的颜色,如下所示

visNetwork(nodes, edges) %>%
  visIgraphLayout(layout = "layout_in_circle") %>%
  visNodes(
    label = NULL,
    color = ifelse(nodes$type == "gimn",
      "slategrey",
      ifelse(nodes$type == "szakgimn",
        "pink",
        ifelse(nodes$type == "alt_isk",
          "black", "tomato"
        )
      )
    )
  ) %>%
  visEdges(arrows = "middle")

另一种方法是在使用管道之前将color添加到nodes,例如

nodes$color <- ifelse(nodes$type == "gimn",
          "slategrey",
          ifelse(nodes$type == "szakgimn",
            "pink",
            ifelse(nodes$type == "alt_isk",
              "black", "tomato"
            )
          )
        )
visNetwork(nodes, edges) %>%
      visIgraphLayout(layout = "layout_in_circle") %>%
      visEdges(arrows = "middle")