简体   繁体   English

R 中的 VisNetwork:调整边的长度

[英]VisNetwork in R: Adjust length of edges

Is there a way to adjust the length of the edges in VisNetwork so that they are overall a bit longer?有没有办法调整 VisNetwork 中边的长度,使它们整体更长一点? Some arrows are a bit hard to see right now especially with the thicker edges.有些箭头现在有点难以看到,尤其是边缘较厚的时候。

在此处输入图像描述

This is my edge list and nodes list这是我的边缘列表和节点列表

nodes_t <- structure(list(ahaid = c("6140002", "6140005", "6140007", "6140008", 
"6140010", "6140011", "6140012", "6140013", "6140016", "6140017", 
"6140065", "6140080", "6140090", "6140215", "6140255", "6140270", 
"6140310", "6140420", "6140428", "6140430", "6140465", "6140583", 
"6140620", "6140630", "6140690", "6140780", "6140850", "6140900", 
"6140923", "6140980", "6141010", "6141020", "6141095", "6141110", 
"6141130", "6141170", "6141300", "6141355", "6141395", "6141410", 
"6141430", "6141450", "6141500", "6141530", "6141570", "6141630", 
"6141640", "6141660", "6141705", "6141720", "6141890", "6141900", 
"6141940", "6141955", "6142000", "6142200", "6142280", "6142350"
), weight = c(129L, 67L, 72L, 111L, 193L, 56L, 32L, 601L, 406L, 
151L, 19L, 56L, 25L, 1909L, 22L, 6L, 38L, 6L, 264L, 1416L, 169L, 
133L, 98L, 87L, 387L, 53L, 63L, 101L, 91L, 32L, 11L, 55L, 456L, 
34L, 225L, 62L, 31L, 158L, 185L, 24L, 6L, 43L, 47L, 112L, 93L, 
94L, 12L, 46L, 175L, 161L, 30L, 452L, 234L, 379L, 86L, 19L, 85L, 
135L)), row.names = c(NA, -58L), class = "data.frame")

edges_t <- structure(list(from = c("6140002", "6140008", "6140010", "6140012", 
"6140013", "6140013", "6140013", "6140016", "6140017", "6140080", 
"6140215", "6140215", "6140215", "6140215", "6140215", "6140215", 
"6140215", "6140215", "6140215", "6140215", "6140215", "6140215", 
"6140428", "6140430", "6140430", "6140430", "6140923", "6141020", 
"6141095", "6141095", "6141170", "6141660", "6141940"), to = c("6140430", 
"6141095", "6140215", "6140430", "6140630", "6141450", "6141720", 
"6140215", "6140690", "6140215", "6140310", "6140428", "6140430", 
"6140583", "6140690", "6141095", "6141130", "6141395", "6141530", 
"6141630", "6141900", "6142000", "6140690", "6140850", "6141530", 
"6141900", "6141130", "6141955", "6141300", "6142350", "6141355", 
"6141955", "6141955"), weight = c(19L, 22L, 7L, 8L, 9L, 17L, 
9L, 15L, 19L, 12L, 22L, 7L, 27L, 90L, 14L, 20L, 13L, 32L, 9L, 
11L, 34L, 8L, 7L, 11L, 17L, 16L, 6L, 16L, 7L, 5L, 10L, 17L, 18L
)), row.names = c(NA, -33L), class = "data.frame")

This is my code这是我的代码

vis.nodes <- nodes_t
vis.links <- edges_t
vis.nodes$borderWidth <- 2 
vis.nodes <- vis.nodes %>% mutate(size = weight/16)

vis.links$width <- 1+edges_t$weight/4 
vis.links$arrows <- "to" 

library('visNetwork') 
p <- visNetwork(vis.nodes, vis.links, width="400%", height="1600px")

You could use the visEdges function with argument length to change the length of the edges.您可以使用带参数长度的visEdges函数来更改边的长度。 Since I can't reproduce your plot, I created a reproducible example:由于我无法重现您的情节,因此我创建了一个可重现的示例:

library(visNetwork)
library(dplyr)
# Data
nodes <- data.frame(id = 1:2)
edges <- data.frame(from = 1, to = 2)

# Plot with length 100
visNetwork(nodes, edges) %>%
  visEdges(arrows = 'to', length = 100) 

# Plot with length 400
visNetwork(nodes, edges) %>%
  visEdges(arrows = 'to', length = 400) 

Created on 2022-12-18 with reprex v2.0.2创建于 2022-12-18,使用reprex v2.0.2

As you can see the first plot has an edge length of 100 pixels and the second plot has an edge length of 400 pixels.如您所见,第一个图的边长为 100 像素,第二个图的边长为 400 像素。


If you want to have variable lengths, you could add an variable length to your edge dataframe like this:如果你想有可变长度,你可以添加一个可变长度到你的边缘数据框,如下所示:

library(visNetwork)
library(dplyr)
# Data
nodes <- data.frame(id = 1:2)
edges <- data.frame(from = 1, to = 2, length = c(100, 400))

# Plot
visNetwork(nodes, edges) %>%
  visEdges(arrows = "to")

Created on 2022-12-18 with reprex v2.0.2创建于 2022-12-18,使用reprex v2.0.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM