【问题标题】:R visNetwork: create new type of edgesR visNetwork:创建新类型的边
【发布时间】:2018-10-14 23:16:59
【问题描述】:

我想用 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 参数中的错误意味着行数不同

【问题讨论】:

  • 请在您的代码中添加一些示例数据。否则我们帮不了你
  • 刚刚做到了:)

标签: r graph shiny vis.js visnetwork


【解决方案1】:

您可以通过添加列arrows.to.typearrows.from.typeedges 数据框中设置单独的箭头类型:

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)

结果:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-26
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多