0

我想用 visNetwork 为我闪亮的应用程序创建一个 PAG(父母祖先图)。为了做到这一点,我必须创建既有圆圈又有箭头的边缘。根据 visNetwork 包,我可以将箭头转换为这样的圆圈

visNetwork(nodes, edges) %>% 
  visEdges(arrows = list(to = list(enabled = TRUE, 
     scaleFactor = 2, type = 'circle')))

但我想要一个箭头和一个圆圈,或者像这张图片 PAG一样在一个边缘有两个圆圈

arrows.from.type 和 arrows.to.type 似乎正在工作,但我现在遇到了这个问题。我想根据邻接矩阵绘制这个图所以我有这个代码

  i = 1
  j = 1
  for(i in i:ncol(results))
  {
    j = i
    for(j in j:nrow(results))
    {
      if(results[j,i]==1)
      {
        dashBoard = c(dashBoard,TRUE)
        colorBoard = c(colorBoard, "green")
        if(results[i,j]==1)
        {
          fromtest <- c(fromtest,Cnames[i])
          totest <- c(totest,Rnames[j])
          arrfrom <-c(arrfrom,"circle")
          arrto<-c(arrto,"circle")
        }
        else if(results[i,j]==2)
        {

          fromtest<-c(fromtest,Cnames[i])
          totest<-c(totest,Rnames[j])
          arrfrom <-c(arrfrom,"circle")
          arrto<-c(arrto,"arrow")
        }}

除了 1,1 和 1,2 之外,所有可能的组合都会如此。最后,边缘会像这样打印

edgesprint <-data.frame(from = fromtest,
                          to = totest,
                          arrows.from.type=arrfrom,
                          arrows.to.type=arrto,
                          dashes = dashBoard,
                          physics = FALSE,
                          smooth = FALSE,
                          width = 3,
                          shadow = TRUE,
                          color = list(color = colorBoard, highlight =   "red", hover = "green"),
                          links = links)

这种方法效果很好,但有时在不更改任何代码的情况下我会收到此错误

data.frame 参数中的错误意味着行数不同

4

1 回答 1

1

您可以通过添加列和来设置edges数据框中的单个箭头类型:arrows.to.typearrows.from.type

library(visNetwork)
library(magrittr)

nodes <- data.frame(id=c("a","b","c","d"), label=c("a","b","c","d"))
edges <- data.frame(
  from = c("a","a","a"),
  to = c("b","c","d"),
  arrows.from.type = c(NA,"circle","circle"),
  arrows.to.type = c("arrow","circle",NA)
)

visNetwork(nodes, edges)

结果:

产生的网络

此方法适用于您可以通过visNodes和设置的所有其他属性visEdges。有关示例,请参见此处。

于 2018-05-04T15:54:50.120 回答