R visNetwork:创建新类型的边缘

时间:2018-05-04 12:51:15

标签: r graph shiny vis.js visnetwork

我想使用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参数中的

错误意味着行数不同

1 个答案:

答案 0 :(得分:0)

您可以在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)

结果:

Resulting network

此方法适用于您可以通过visNodesvisEdges设置的所有其他属性。有关示例,请参阅here