gather nodes based on group information

北战南征 提交于 2021-01-13 09:10:36

问题


-----shiny minimal example

require(shiny)
require(visNetwork)

server <- function(input, output) {
  output$network <- visNetwork::renderVisNetwork({
    edges <- data.frame(
      from = sample(1:10, 8), to = sample(1:10, 8),
      label = paste("interaction type", 1:8),
      length = c(100, 500),
      width = c(4, 1),
      arrows = c("to", "from", "middle", "middle;to"),
      dashes = c(TRUE, FALSE),
      title = paste("interaction name", 1:8),
      smooth = c(FALSE, TRUE),
      shadow = c(FALSE, TRUE, FALSE, TRUE)
    )
    
# define co-ordinates manually
    coords <- as.matrix(data.frame(
      x = c(-319,267,-641,321,-580,277,-418,166,-574,314),
      y = c(-168,169,-65,42,7,-175,88,205,-161,-82),
      stringsAsFactors = FALSE
    ))
    
    nodes <- data.frame(
      id = 1:10, group = c("A", "B"),
      label = paste("Node", 1:10),
      shape = "ellipse"
    )
    
    visNetwork::visNetwork(nodes, edges, height = "500px", width = "100%") %>%
  visNetwork::visIgraphLayout(layout = "layout.norm", layoutMatrix = coords)        
  })           
}

ui <- fluidPage(
  visNetwork::visNetworkOutput("network")      
)

shinyApp(ui = ui, server = server)

data

> nodes
   id group   label   shape
1   1     A  Node 1 ellipse
2   2     B  Node 2 ellipse
3   3     A  Node 3 ellipse
4   4     B  Node 4 ellipse
5   5     A  Node 5 ellipse
6   6     B  Node 6 ellipse
7   7     A  Node 7 ellipse
8   8     B  Node 8 ellipse
9   9     A  Node 9 ellipse
10 10     B Node 10 ellipse

> edges
  from to              label length width    arrows dashes              title smooth shadow
1    4  1 interaction type 1    100     4        to   TRUE interaction name 1  FALSE  FALSE
2    6 10 interaction type 2    500     1      from  FALSE interaction name 2   TRUE   TRUE
3    1  6 interaction type 3    100     4    middle   TRUE interaction name 3  FALSE  FALSE
4    8  3 interaction type 4    500     1 middle;to  FALSE interaction name 4   TRUE   TRUE
5   10  4 interaction type 5    100     4        to   TRUE interaction name 5  FALSE  FALSE
6    9  5 interaction type 6    500     1      from  FALSE interaction name 6   TRUE   TRUE
7    2  9 interaction type 7    100     4    middle   TRUE interaction name 7  FALSE  FALSE
8    3  8 interaction type 8    500     1 middle;to  FALSE interaction name 8   TRUE   TRUE

Current Output : In the above code the coords df with the x,y values are manually created. The values for x and y were chosen by trial and error by checking which values separate the nodes for respective groups. The 2 groups in the data are Blue nodes ( group A) and Yellow nodes ( group B). However, since I have large data set this manual approach is not possible. I am looking for a way to automate the assignment of x,y co-ordinates to each group programmatically and still get the groups separated. The desired output below is something I'm aiming towards with cell boundaries and then nodes arranged within the cell. Appreciate any inputs.

Desired Output

来源:https://stackoverflow.com/questions/65583493/gather-nodes-based-on-group-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!