R:更改图形中节点的大小

j0pj023g  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(205)

我正在使用R编程语言。
我发现了这个R代码,它生成了一个网络图,其中节点具有不同的大小:

library(visNetwork)
library(igraph)

 my_data <- data.frame( "Col_C" = c("AAA", "AAA", "AAB", "AAB", "AAB", "ABC", "CDE", "CDE", "CDE", "AAB", "AAB", "AAE"), "Col_D" = c("AAB", "AAC", "AAC", "ABC", "CDE", "CDE", "AAB", "AAE", "ADF", "AAE", "ADF","ADF") )

 final <- data.frame(col = unique(unlist(my_data)))

 final$size = c(6,10,1,20,50,1,5) 

 graph_B <- graph_from_data_frame(my_data, directed = FALSE, vertices = final)

 visIgraph(graph_B)

**我的问题:**我尝试在这里应用此逻辑来更改以下网络图中的节点大小:

library(tidyverse)
library(visNetwork)
library(htmlwidgets)

set.seed(123)
mat <- matrix(runif(5*5), nrow = 5, ncol = 5)
mat <- round(mat, 2)

nodes <- data.frame(id = 1:5)
edges <- data.frame(from = sample(1:5, 100, replace = TRUE), to = sample(1:5, 100, replace = TRUE))

nodes$shape <- 'circle'
nodes$size <- sample(1:10, 5, replace = TRUE) # assign random sizes to nodes
edges$weight <- apply(edges, 1, function(x) mat[x["from"], x["to"]])
edges$label <- as.character(apply(edges, 1, function(x) mat[x["from"], x["to"]]))

nodes$label <- paste("Node", 1:5)

    nodes$size <- sample(1:10, 5, replace = TRUE)
    nodes$value <- nodes$size

network <- visNetwork(nodes, edges) %>%
    visEdges(arrows = list(to = list(enabled = TRUE))) %>%
    visIgraphLayout(layout = "layout_in_circle")

network

然而,当我运行上面的代码时-节点的大小没有改变:

有人能告诉我我做错了什么,我能做些什么来解决这个问题吗?
谢谢!

xkrw2x1b

xkrw2x1b1#

它不是很清楚,由于某种原因,size参数只适用于某些形状,这些形状在节点外部有标签(例如dot,它应用于顶部图像)。要控制circle等形状的大小,必须使用应用于value的缩放参数来更改标签大小。请参阅下面示例代码中的注解:

set.seed(123)
mat <- matrix(runif(5*5), nrow = 5, ncol = 5)
mat <- round(mat, 2)

nodes <- data.frame(id = 1:5)
edges <- data.frame(from = sample(1:5, 100, replace = TRUE), to = sample(1:5, 100, replace = TRUE))

nodes$label <- paste("Node", 1:5)
nodes$shape <- 'circle'
nodes$value <- 1:5 # assign label size value same as label

edges$weight <- apply(edges, 1, function(x) mat[x["from"], x["to"]])
edges$label <- as.character(apply(edges, 1, function(x) mat[x["from"], x["to"]]))

visNetwork(nodes, edges) %>%
  # use scaling to scale by label
  visNodes(shape = "circle", scaling = list(label = list(min = 10, max = 20))) %>% 
  visEdges(arrows = list(to = list(enabled = TRUE))) %>%
  visIgraphLayout(layout = "layout_in_circle")

相关问题