r - plotly 网络: edges are drawn over the vertices (should be the opposite)

标签 r plotly igraph

当尝试使用plotly绘制网络 map 时,边缘被绘制在顶点上,造成非常困惑的外观。特别是在大图的情况下。我想要相反的东西 - 边缘在背景上,而顶点 - 在前景上。我该如何解决它?这是代码,主要基于官方的plotly manual

我几乎尝试了所有方法,包括首先绘制边,然后才通过 %>% add_markers() 函数添加顶点。尽管如此,它还是将标记(即顶点)塞在边缘下方......

  library(plotly)
  library(igraph)

  from <- c('Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0','Cer d18:0 22:0')
  to <- c('Cer d18:0 24:0','Cer d18:1 16:0','Cer d18:1 18:0','Cer d18:1 19:0','Cer d18:1 20:0','Cer d18:1 22:0','Cer d18:1 23:0','Cer d18:1 24:0','Cer d18:1 24:1','Cer d18:1 25:0','Cer d18:1 26:0','Cer d18:1 26:1','Cer d18:2 22:0','Cer d18:2 24:0','PA 34:0','PC 32:0','PC 34:0','PC 36:1','PC 40:3','PC 40:4','PE 38:2','PE 40:4','PG 38:4','PS 34:2','PS 36:1','PS 36:2','PS 36:3','PS 36:4','PS 38:1','PS 38:2')
  correlation_coef <- c(0.924242754656747, 0.526805035968068, 0.570778952347874, 0.454825728316617, 0.679678493588509, 0.668303866512371, 0.479133855661387, 0.532374142153078, 0.52517607812595, 0.483140207186826, 0.563659705068807, 0.644697305098682, 0.314289108019215, 0.310428047301357, 0.350390031690504, 0.528050509271734, 0.657752952171894, 0.460976737159669, 0.361184712230395, 0.366309295955763, 0.395518465498505, 0.332827408004377, 0.305966002867172, 0.380434734306024, 0.519959662995765, 0.417649916172369, 0.482502664315797, 0.413290542266176, 0.525183933906865, 0.518064319527777)
  weight <- c(4.67210750625538e+22, 19718.705006369, 123521.793897403, 1605.91913563112, 45672105.7416137, 21962850.5955395, 3523.15220085069, 24531.7597679316, 18511.7557022388, 4033.11033388264, 90149.3343222277, 5293479.36379668, 45.6400730945173, 42.228380515442, 98.9456657700542, 20698.8623501171, 11449620.4269023, 1948.45232274164, 126.88452470771, 143.20346944195, 296.267232658405, 67.1716450118342, 38.6474640578579, 201.896951271096, 15154.5672063363, 537.39606665137, 3946.83809823451, 476.400634445124, 18517.3809339285, 14103.19811631)

  corr_chart <- data.frame(from, to, correlation_coef, weight)

  Nodes_BD <- unique(c(corr_chart[,1], corr_chart[,2]))
  net <- graph_from_data_frame(d = corr_chart,  directed = F)#, vertices = Nodes_BD)
  net <- simplify(net, remove.multiple = F, remove.loops = T)

  shapes <- rep(x = 27, length(Nodes_BD))
  shapes[grep(pattern = "APO",x = Nodes_BD)] <- 17
  shapes[grep(pattern = "CE",x = Nodes_BD, fixed = T)] <- 17
  shapes[grep(pattern = "FC",x = Nodes_BD, fixed = T)] <- 17
  shapes[grep(pattern = "TG",x = Nodes_BD, fixed = T)] <- 17
  shapes[grep(pattern = "TP",x = Nodes_BD, fixed = T)] <- 17
  shapes[grep(pattern = "efflux",x = Nodes_BD, fixed = T)] <- 17
  shapes[grep(pattern = "AOXRate",x = Nodes_BD, fixed = F)] <- 3
  shapes[grep(pattern = "AOXPhase",x = Nodes_BD, fixed = F)] <- 4
  shapes[grep(pattern = "AOXMaxCon",x = Nodes_BD, fixed = F)] <- 17

  V(net)$shape <- shapes


  deg <- degree(net, mode='all')

  V(net)$size <- 3#V(net)$size <- deg^0.6/2

  V(net)$label <- as.character(Nodes_BD)
  labels <- ifelse(test = (V(net)$label == "efflux"| V(net)$label == "APOA1" | V(net)$label == "APOA2" | V(net)$label == "TP" | V(net)$label == "CE" | V(net)$label == "FC" | V(net)$label == "PL" | V(net)$label == "TG" | V(net)$label == "AOXMaxCon" | V(net)$label == "AOXRate2" | V(net)$label == "AOXPhase2" ), yes = V(net)$label, no = NA)

  #generating a layout with igraph
  L <- layout_with_graphopt(net) #gem is good, and graphopt too
  rownames(L) <- get.vertex.attribute(net)$name

  vs <- V(net)
  es <- as.data.frame(get.edgelist(net))

  Nv <- length(vs)
  Ne <- length(es[1]$V1)

  Xn <- L[,1]
  Yn <- L[,2]

  network <- plot_ly(x = ~Xn, y = ~Yn, mode = "markers", hoverinfo = "text", marker=list(symbol =~shapes), size =~I(6*(V(net)$size)), showlegend = T) 

  edge_shapes <- list()
  for(i in 1:Ne) {
    v0 <- es[i,]$V1
    v1 <- es[i,]$V2

    edge_shape = list(
      type = "line",
      line = list(color = "black", width = 1*(5/(5+0.1*length(Nodes_BD)))),
      x0 = L[which(v0==rownames(L)),][1],
      y0 = L[which(v0==rownames(L)),][2],
      x1 = L[which(v1==rownames(L)),][1],
      y1 = L[which(v1==rownames(L)),][2],
      opacity = 1.2*(10/(10+0.2*length(Nodes_BD)))
    )

    edge_shapes[[i]] <- edge_shape
  }

  #forming a layout of plotly p <- layout
  axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE)
  p <- layout(
    shapes = edge_shapes,
    network,
    title = paste('Lipidome of HDL'),
    xaxis = axis,
    yaxis = axis
  )
  p

Picture of the resulting chart with messed up layers

最佳答案

您需要在标记之前定义段。
可以先使用 add_segments,然后使用 add_markers,如下所示:

p <- plot_ly()
for(i in 1:Ne) {
    v0 <- es[i,]$V1
    v1 <- es[i,]$V2
    p <- add_segments(p, 
           x = L[which(v0==rownames(L)),][1], 
           y = L[which(v0==rownames(L)),][2],
           xend = L[which(v1==rownames(L)),][1],
           yend = L[which(v1==rownames(L)),][2], 
           opacity = 1.2*(10/(10+0.2*length(Nodes_BD))),
           line = list(color = "black", width = 1*(5/(5+0.1*length(Nodes_BD)))),
           inherit = F, showlegend=FALSE)
}

p <- add_markers(p, x = ~Xn, y = ~Yn,  hoverinfo = "text", name="Points",
                 marker=list(symbol =~shapes), size =~I(6*(V(net)$size)))


axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE)
p <- layout(p,
    showlegend=TRUE,
    title = paste('Lipidome of HDL'),
    xaxis = axis, yaxis = axis)
p

enter image description here

关于r - plotly 网络: edges are drawn over the vertices (should be the opposite),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49886227/

相关文章:

python - 如何在 PLOTLY 直方图中将 x 轴更改为对数

r - 通过使用 igraph (R) 组合事件顶点的属性来创建边缘属性

algorithm - 网络中自上而下的社区检测

R根据某个时间点的数据范围创建新列

r - 使用R将趋势线添加到散点图

r - 为什么 plotly sankey 图中节点的固定位置被覆盖或忽略?

r - 在r中的igraph中均匀间隔顶点

Java 和 R 集成

RODBC 连接受限行

javascript - 如何过滤 json 并获取在 js 中创建线图的平均值?