gpt4 book ai didi

r - 使用 visNetwork R 包的类似思维导图的布局(使用 vis.js javascript 库的网络可视化)

转载 作者:行者123 更新时间:2023-12-04 09:43:57 27 4
gpt4 key购买 nike

是否可以调整 visNetwork 的选项(例如 visLayout、visOptions 或 visPhysics)以获得类似于思维导图的网络可视化?

我想获得这样的东西:
Produced using Mindomo

这是我在 R 中可重现的示例使用 visNetwork绘制相同的数据:

nodes <- structure(list(id = 1:22, label = structure(c(14L, 20L, 19L, 
16L, 12L, 18L, 2L, 17L, 22L, 8L, 13L, 3L, 4L, 5L, 6L, 7L, 21L,
15L, 9L, 1L, 10L, 11L), .Label = c("A seemengly impossible mission\n",
"Another \n", "Detail 1\n", "Detail 2\n", "Detail 3\n", "Detail 4\n",
"Detail 5\n", "Do you know where is Dover?\n", "Dover Castle\n",
"Finally, I'm the fifth\n", "I'm alone", "I'm relatively short\n",
"Let's say there is a third one\n", "Main topic\n", "Operation Dynamo\n",
"or, I'm even longer and perhaps I need some more space\n", "Running out of imagination\n",
"Shorter\n", "Some longer text goes here\n", "Thing 1\n", "Thing number 4\n",
"What can happen?\n"), class = "factor"), shape = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), class = "factor", .Label = "box")), .Names = c("id",
"label", "shape"), row.names = c(NA, -22L), class = "data.frame")

edges <- structure(list(from = c(1L, 2L, 2L, 2L, 2L, 1L, 7L, 7L, 7L, 1L,
11L, 11L, 11L, 11L, 11L, 1L, 17L, 17L, 17L, 1L, 21L), to = 2:22,
arrows = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "to", class = "factor")), .Names = c("from",
"to", "arrows"), row.names = c(NA, 21L), class = "data.frame")
library(visNetwork)
visNetwork(nodes, edges) %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
visLayout(randomSeed = 1)

此代码生成此可视化:

enter image description here

所以你可以看到第一个图更清晰,更容易阅读和使用。是否可以调整 visNetwork参数(vis.js 参数),从而使结果与这里的第一个图比较相似?

基本上它就像有一个中心主题,然后下一级主题围绕主题呈放射状排列,进一步的级别相互关联(类似于列表)。

最佳答案

您可以将坐标传递给节点 data.frame,然后禁用物理。

您可以将节点放置在您想要的位置并使用 Shiny 的应用程序获取坐标,然后在您的网络中使用它,例如:

mynetwork <- visNetwork(nodes, edges) %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
visLayout(randomSeed = 1) %>%
visPhysics(enabled = FALSE) # disable physics to move nodes

require(shiny)
server <- function(input, output) {
output$network <- renderVisNetwork({
mynetwork
})

vals <- reactiveValues(coords=NULL)

output$view <- renderPrint({
write.table(vals$coords, file = "save_coordinates.csv", sep = ";")
vals$coords
})

observe({
input$getcoord
visNetworkProxy("network") %>% visGetPositions()
vals$coords <- if (!is.null(input$network_positions))
do.call(rbind, input$network_positions)
})
}

ui <- fluidPage(
visNetworkOutput("network", height = "800px"),
actionButton("getcoord", "View & Save Coordinates"),
verbatimTextOutput("view")
)

shinyApp(ui = ui, server = server)


# and after, put coordinnates into nodes data.frame and use visNodes(fixed = TRUE)
# if you want

coord <- read.csv("save_coordinates.csv", sep = ";")
nodes <- cbind(nodes, coord)

visNetwork(nodes, edges) %>%
visNodes(fixed = T) %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
visLayout(randomSeed = 1) %>%
visPhysics(enabled = FALSE)

你也可以玩 levelvisHierarchicalLayout :
nodes <- data.frame(id = 1:9, level = c(1,1,2,3,3, 4, 4, 4, 4))
edges <- data.frame(from = c(3, 3, 3, 3, 4, 4, 5, 5),
to = c(1, 2, 4, 5, 6, 7, 8, 9))


visNetwork(nodes, edges) %>%
visHierarchicalLayout(direction = "LR")

关于r - 使用 visNetwork R 包的类似思维导图的布局(使用 vis.js javascript 库的网络可视化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39560194/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com