R未在forceDirect中呈现图形

iih3973s  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(135)

遵循了Stackoverflow和YouTube中的所有建议,但仍然无法使用R Studio在NetworkD3中渲染图形。
生成节点和链接csv文件并上传相同文件,simpleDirect从链接文件渲染,但forceDirect既不指示错误,也不渲染图形。

library(networkD3)

Links <- 
  read.table(header = TRUE, stringsAsFactors = FALSE, text = "
    Source  Target  Value
    A       B       1
    B       A       1
    AB      BA      1 
    A       C       1 
    C       A       1 
    AC      C       1
  ")

Nodes <- 
  read.table(header = TRUE, stringsAsFactors = FALSE, text = "
    Id  Name  Group
    0   A     1
    1   B     1
    2   C     1
    3   AB    1
    4   AC    1
    5   BA    1
  ")

forceDirect(
  Links = Links, 
  Nodes = Nodes,
  Source = "Source", 
  Target = "Target",
  Value = "Value", 
  NodeId = "Name", 
  Group = "Group",
  opacity = 0
)

如前所述,这(Links数据集)在simpleDirect中运行良好,但即使forceDirect调用错误,也不会重新生成任何图形。

jdgnovmf

jdgnovmf1#

1.函数名是forceNetwork(),而不是forceDirect()
1.参数名称是NodeID,而不是NodeId
1.如果将opacity参数设置为0,则不会看到任何内容。

  1. Links Dataframe 的SourceTarget列中的值必须是数字,并标识它们所引用的Nodes Dataframe 的零索引行。
# your data
Links <- 
  read.table(header = TRUE, stringsAsFactors = FALSE, text = "
    Source  Target  Value
    A       B       1
    B       A       1
    AB      BA      1 
    A       C       1 
    C       A       1 
    AC      C       1
  ")

Nodes <- 
  read.table(header = TRUE, stringsAsFactors = FALSE, text = "
    Id  Name  Group
    0   A     1
    1   B     1
    2   C     1
    3   AB    1
    4   AC    1
    5   BA    1
  ")

# convert Source and Target to zero-indexed row of Nodes
Links$Source <- match(Links$Source, Nodes$Name) - 1
Links$Target <- match(Links$Target, Nodes$Name) - 1

# plot
library(networkD3)
forceNetwork(
  Links = Links, 
  Nodes = Nodes,
  Source = "Source", 
  Target = "Target",
  Value = "Value", 
  NodeID = "Name", 
  Group = "Group",
  opacity = 1
)

相关问题