#visOptions用法
visOptions(
graph,
width = NULL,
height = NULL,
highlightNearest = FALSE,
nodesIdSelection = FALSE,
selectedBy = NULL,
collapse = FALSE,
autoResize = NULL,
clickToUse = NULL,
manipulation = NULL
)
#参数
highlightNearest:
只是一个布尔值,或者一个命名列表;点击某个节点时,突出该节点周围的连接节点
- "enabled":布尔。默认为false。激活与否?
- "degree" :节点周围要突出节点的度,如果是1,只是突出与选中节点连接的节点(间隔1条边),2是突出与选中节点间接连接的一个节点(间隔2条边)。对于"hierarchical" ,可以使用 list(from = 1, to = 1) 。
- "hover":布尔值;当鼠标悬停在节点上是否突出该节点。
- "algorithm":字符串。highlightNearest算法。“all”突出显示所有节点,而不获取方向信息。“hierarchical”只关注输入/输出节点。
- hideColor":字符串。隐藏节点/边的颜色。使用rgba定义。Defaut rgba (200200200, 0.5)
- "labelOnly":布尔。选中节点时,只保留节点周围degree+ 1的节点的标签?默认为TRUE。
nodesIdSelection:
只是一个布尔值,或者一个命名列表。默认为false。添加一个id节点选择,创建一个HTML select元素
- "enabled" :布尔。默认为false。激活与否?
- "values : 可供选择的节点id,因此顺序保持不变。获取节点数据。默认是所有id。
- "selected":整数/字符。最初的id选择。
- "style":默认,HTML风格的列表。默认宽度:150px;高度:26 px”。
- "useLabels":布尔。使用节点labels而不是id ?默认为TRUE。
- "main":选择总结点的名称,Default to "Select by id"。
collapse
一个布尔值,或者一个命名列表。使用双击删除节点。
- "enabled" :布尔。默认为false。激活与否?
- "fit":布尔。默认为FALSE。在collapse/uncollapse节点后,调用fit方法?
- “resetHighlight”: 布尔。默认值为TRUE,collapse/uncollapse节点后重置突出显示的节点。
- “clusterOptions”: 列表。Defaut为NULL。要传递给集群collapsed节点的所有设置参数列表
- “keepCoord”: 布尔。默认值为TRUE,在collapse时保持节点坐标
- “labelSuffix”:字符。使用节点标签+后缀或只是后缀。默认为'(cluster)'。
selectedBy
字符或命名列表。根据节点data.frame的列添加多个选择,创建一个HTML选择元素。
- “变量”:字符。选择变量的列名。
- “values”: 可选的。可选择节点的选择值得向量。默认获取节点的所有值。
- "selected":整数/字符。最初的id选择。
- "style":默认,HTML风格的列表。默认宽度:150px;高度:26 px”。
- 布尔。默认为FALSE。如果为真,可以使用逗号("gr1,gr2")影响每个节点的多个组
- hideColor":字符串。隐藏节点/边的颜色。使用rgba定义。Defaut rgba (200200200, 0.5)
- "main":选择总结点的名称,Default to "Select by id"。
- “sort”: 可选的。如果没有设置values,排序所有可能的值?Defaut为真
- “highlight”:可选的。布尔。在每个选定节点上运行highlightNearest?Defaut为假
#示例
nodes <- data.frame(id = 1:15, label = paste("Label", 1:15),
group = sample(LETTERS[1:3], 15, replace = TRUE))
edges <- data.frame(from = trunc(runif(15)*(15-1))+1,
to = trunc(runif(15)*(15-1))+1)
###################
# highlight nearest
###################
visNetwork(nodes, edges) %>% visOptions(highlightNearest = TRUE)
visNetwork(nodes, edges) %>% visOptions(highlightNearest = list(enabled = TRUE, degree = 2))
# also when hover a node ?
visNetwork(nodes, edges) %>% visOptions(highlightNearest = list(enabled = TRUE, hover = TRUE))
# don't show nodes/edges
visNetwork(nodes, edges) %>% visOptions(highlightNearest = list(enabled = TRUE,
hover = TRUE, hideColor = 'rgba(200,200,200,0)'))
# Using hierarchical information
nodes = data.frame(id = 1:6, level = c(1, 2, 3, 3, 4, 2))
edges = data.frame(from = c(1, 2, 2, 4, 6), to = c(2, 3, 4, 5, 4))
visNetwork(nodes, edges) %>% visHierarchicalLayout() %>% visEdges(arrows = "to") %>%
visOptions(highlightNearest = list(enabled = TRUE, algorithm = "hierarchical"))
visNetwork(nodes, edges) %>% visHierarchicalLayout() %>% visEdges(arrows = "to") %>%
visOptions(highlightNearest = list(enabled = TRUE, algorithm = "hierarchical",
degree = list(from = 0, to = 2)))
##########################
# nodesIdSelection
##########################
visNetwork(nodes, edges) %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
# add a default selected node ?
visNetwork(nodes, edges) %>%
visOptions(highlightNearest = TRUE,
nodesIdSelection = list(enabled = TRUE, selected = "1"))
# subset on id values & don't use labels ?
visNetwork(nodes, edges) %>%
visOptions(highlightNearest = TRUE,
nodesIdSelection = list(enabled = TRUE,
selected = "2", values = c(2:10), useLabels = FALSE))
# some style
visNetwork(nodes, edges) %>%
visOptions(highlightNearest = TRUE,
nodesIdSelection = list(enabled = TRUE, style = 'width: 200px; height: 26px;
background: #f8f8f8;
color: darkblue;
border:none;
outline:none;'))
##########################
# collapse
##########################
nodes <- data.frame(id = 1:15, label = paste("Label", 1:15),
group = sample(LETTERS[1:3], 15, replace = TRUE))
edges <- data.frame(from = trunc(runif(15)*(15-1))+1,
to = trunc(runif(15)*(15-1))+1)
# keeping all parent node attributes
visNetwork(nodes, edges) %>% visEdges(arrows = "to") %>%
visOptions(collapse = TRUE)
# setting some properties
visNetwork(nodes, edges) %>% visEdges(arrows = "to") %>%
visOptions(collapse = list(enabled = TRUE, clusterOptions = list(shape = "square")))
# enable / disable open cluster (proxy only) :
# visEvents(type = "off", doubleClick = "networkOpenCluster")
# visEvents(type = "on", doubleClick = "networkOpenCluster")
##########################
# selectedBy
##########################
nodes <- data.frame(id = 1:15, label = paste("Label", 1:15),
group = sample(LETTERS[1:3], 15, replace = TRUE))
edges <- data.frame(from = trunc(runif(15)*(15-1))+1,
to = trunc(runif(15)*(15-1))+1)
visNetwork(nodes, edges) %>%
visOptions(selectedBy = "group")
# add a default value ?
visNetwork(nodes, edges) %>%
visOptions(selectedBy = list(variable = "group", selected = "A"))
# subset on values ?
visNetwork(nodes, edges) %>%
visOptions(selectedBy = list(variable = "group",
selected = "C",
values = c("A", "C")))
# highlight also
visNetwork(nodes, edges) %>%
visOptions(selectedBy = list(variable = "group",
highlight = TRUE), highlightNearest = TRUE)
# add some style
visNetwork(nodes, edges) %>%
visOptions(selectedBy = list(variable = "group", style = 'width: 200px; height: 26px;
background: #f8f8f8;
color: darkblue;
border:none;
outline:none;'))
# can also be on new column
nodes$sample <- sample(c("sample 1", "sample 2"), nrow(nodes), replace = TRUE)
visNetwork(nodes, edges) %>%
visOptions(selectedBy = "sample")
# and with multiple groups ?
nodes$group <- sample(c("group 1", "group 2", "group 1, group 2, group 3"),
nrow(nodes), replace = TRUE)
visNetwork(nodes, edges) %>%
visOptions(selectedBy = list(variable = "group", multiple = TRUE))
##########################
# manipulation
##########################
visNetwork(nodes, edges) %>%
visOptions(manipulation = TRUE)
visNetwork(nodes, edges) %>%
visOptions(manipulation = list(enabled = TRUE, addNode = FALSE, addEdge = FALSE))
visNetwork(nodes, edges) %>%
visOptions(manipulation = list(enabled = TRUE, deleteNode = FALSE, deleteEdge = FALSE))
visNetwork(nodes, edges) %>%
visOptions(manipulation = list(enabled = TRUE, editNode = FALSE, editEdge = FALSE))
visNetwork(nodes, edges) %>%
visOptions(manipulation = list(enabled = TRUE,
editEdge = htmlwidgets::JS("function(data, callback) {
console.info('edit edge')
}")
)
)
##########################
# collapse
##########################
visNetwork(nodes, edges) %>%
visEdges(arrows = "to") %>%
visOptions(collapse = list(enabled = TRUE,
clusterOptions = list(shape = "square")))
系列文章:
R语言进行网络分析的基础包 igraph
networkD3 绘制动态网络
网络-visNetwork包绘制炫酷的动态网络图
网络-调用R包构建交互式网络可视化的Shiny App
网友评论