如何在R中使用visNetwork包时更改所选节点或边的颜色?

huangapple go评论70阅读模式
英文:

How to change colour of selected node or edge when using the visNetwork package in R?

问题

以下是您要翻译的内容:

"这里"1有类似的问题,但我似乎无法将其适应我的代码。我尝试的是使用R中的visNetwork包更改节点和边的颜色。我希望在以下情况下应用颜色更改:

1:鼠标悬停在节点/边上时

2:单击节点或边时

以下是一些创建非常简单网络的代码。

# 加载所需的包
library(visNetwork)

# 创建示例数据
nodes <- data.frame(id = 1:3, label = c("Node 1", "Node 2", "Node 3"))
edges <- data.frame(from = c(1, 1, 2), to = c(2, 3, 3), value = c(10, 20, 30))

# 创建具有自定义节点和边颜色的visNetwork对象
visNetwork(nodes, edges) %>%
  visNodes(color = list(background = "red", 
                        border = "black")) %>%
  visEdges(color = "blue", smooth = FALSE) %>%
  visOptions(highlightNearest = list(enabled = TRUE, hover = TRUE),
             nodesIdSelection = TRUE)

目前,我有节点着色为红色,并且当鼠标悬停/单击节点时,它会自动更改为蓝色。我想以某种方式控制该颜色。此外,在选择边时,它似乎只是使边变粗,但保持相同的颜色。我想尝试在选择边时更改边的颜色。

我已经尝试了visOptionsvisEdgesvisNodes参数,但似乎无法弄清楚它。
1: https://stackoverflow.com/questions/39922095/adding-color-and-hover-options-to-visnetwork-igraph

英文:

A similar question has been asked here, but I cant seem to adapt it to my code. What I'm trying to do is change the colour of the nodes and edges using the visNetwork package in R. I want to apply this colour change when either

1: the mouse is hovering over a node/edge and

2: when a node or edge is clicked on.

Below is some code to create a very simple network.

# Load required packages
library(visNetwork)

# Create example data
nodes &lt;- data.frame(id = 1:3, label = c(&quot;Node 1&quot;, &quot;Node 2&quot;, &quot;Node 3&quot;))
edges &lt;- data.frame(from = c(1, 1, 2), to = c(2, 3, 3), value = c(10, 20, 30))

# Create visNetwork object with customized node and edge colors
visNetwork(nodes, edges) |&gt;  
  visNodes(color = list(background = &quot;red&quot;, 
           border = &quot;black&quot;)) |&gt; 
  visEdges(color = &quot;blue&quot;, smooth = FALSE)|&gt;
  visOptions(highlightNearest = list(enabled = T, hover = T),
                                    nodesIdSelection = T)

At the moment, I have the nodes coloured red and when hovering over/clicking the node with the mouse, it automatically changes colour to a blue. I would like to control that colour somehow. Also, when selecting an edge, it just seems to make the edge thicker but keeps the same colour. I want to try and change the edge colour when selecting it.

I have tried playing with the visOptions, visEdges, and visNodes arguments, but I just can't seem to figure it out.

答案1

得分: 1

?visEdges(或?visNodes)中,参数color

命名列表或字符串。默认为命名列表。在任何情况下边的颜色信息。可以是'rgba(120,32,14,1)','#97C2FC'(不带透明度的7个字符的十六进制表示法)或'red'。

  • "color":字符串。默认为'#848484'。当未选择或悬停时边的颜色(假设交互模块中启用了悬停)。
  • "highlight":字符串。默认为'#848484'。选中边时的颜色。
  • "hover":字符串。默认为'#848484'。当鼠标悬停在边上时的颜色(假设启用了悬停交互模块)。
  • ...
# 加载所需的包
library(visNetwork)

# 创建示例数据
nodes <- data.frame(id = 1:3, label = c("Node 1", "Node 2", "Node 3"))
edges <- data.frame(from = c(1, 1, 2), to = c(2, 3, 3), value = c(10, 20, 30))

# 使用自定义节点和边颜色创建visNetwork对象
visNetwork(nodes, edges) %>  
  visNodes(color = list(background = "red", border = "black", highlight = "yellow",
                        hover = "yellow")) %> 
  visEdges(color = list(color = "blue", highlight = "yellow", 
                        hover = "yellow"), smooth = FALSE) %> 
  visOptions(highlightNearest = list(enabled = T, hover = T),
             nodesIdSelection = T)
英文:

From ?visEdges (or ?visNodes), argument color:

> Named list or String. Default to named list. Color information of the edge in every situation. Can be 'rgba(120,32,14,1)', '#97C2FC' (hexa notation on 7 char without transparency) or 'red'.
> * "color" : String. Default to '#848484. The color of the edge when it is not selected or hovered over (assuming hover is enabled in the interaction module).
> * "highlight " : String. Default to '#848484'. The color the edge when it is selected.
> * "hover" : String. Default to '#848484'. The color the edge when the mouse hovers over it (assuming hover is enabled in the interaction module).
> * ...

# Load required packages
library(visNetwork)

# Create example data
nodes &lt;- data.frame(id = 1:3, label = c(&quot;Node 1&quot;, &quot;Node 2&quot;, &quot;Node 3&quot;))
edges &lt;- data.frame(from = c(1, 1, 2), to = c(2, 3, 3), value = c(10, 20, 30))

# Create visNetwork object with customized node and edge colors
visNetwork(nodes, edges) |&gt;  
  visNodes(color = list(background = &quot;red&quot;, border = &quot;black&quot;, highlight = &quot;yellow&quot;,
                        hover = &quot;yellow&quot;)) |&gt; 
  visEdges(color = list(color = &quot;blue&quot;, highlight = &quot;yellow&quot;, 
                        hover = &quot;yellow&quot;), smooth = FALSE)|&gt;
  visOptions(highlightNearest = list(enabled = T, hover = T),
             nodesIdSelection = T)

huangapple
  • 本文由 发表于 2023年4月13日 22:34:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定