2016-07-03 59 views
2

我使用add_trace()函数在中为循环在plotly的scatter3d模式下为3d网络图创建线。每个add_trace在网络中的两个节点之间绘制一条单独的线。该方法正在工作,但有大量的循环,单个循环的速度似乎正在很快放缓。在for循环中优化add_trace()?

示例数据可以在这里下载:https://gist.github.com/pravj/9168fe52823c1702a07b

library(igraph) 
library(plotly) 

G <- read.graph("karate.gml", format = c("gml")) 
L <- layout.circle(G) 

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

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

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

network <- plot_ly(type = "scatter3d", x = Xn, y = Yn, z = rep(0, Ne), mode = "markers", text = vs$label, hoverinfo = "text", showlegend = F) 

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

    x0 <- Xn[v0] 
    y0 <- Yn[v0] 
    x1 <- Xn[v1] 
    y1 <- Yn[v1] 

    df <- data.frame(x = c(x0, x1), y = c(y0, y1), z = c(0, 0)) 
    network <- add_trace(network, data = df, x = x, y = y, z = z, type = "scatter3d", mode = "lines", showlegend = F, 
         marker = list(color = '#030303'), line = list(width = 0.5)) 
} 

这个例子是相当快的,但是当我有几百边缘或更多,各个回路的执行开始从根本上减缓。我尝试了不同的优化方法(向量化等),但似乎没有围绕add_trace函数本身的缓慢工作。

有什么建议吗?

回答

0

以图表形式添加多个线段的最有效方式不是分别作为单独的轨迹,而是仅使用包含所有线段的单个轨迹。您可以通过构建一个数据框来完成这一工作,每个节点的x,y坐标都要连接,并在每个线段之间插入NA。然后使用connectgaps=FALSE在每个NA处将轨迹分解为单独的段。你可以看到这种方法的另一个例子,适用于this answer的意大利面条。

es$breaks <- NA 
lines <- data.frame(node=as.vector(t(es)), x=NA, y=NA, z=0) 
lines[which(!is.na(lines$node)),]$x <- Xn[lines[which(!is.na(lines$node)),]$node] 
lines[which(!is.na(lines$node)),]$y <- Yn[lines[which(!is.na(lines$node)),]$node] 

network <- plot_ly(type = "scatter3d", x = Xn, y = Yn, z = rep(0, Ne), 
        mode = "markers", text = vs$label, hoverinfo = "text", 
        showlegend = F) %>% 
    add_trace(data=lines, x=x, y=y, z=z, showlegend = FALSE, 
         type = 'scatter3d', mode = 'lines+markers', 
         marker = list(color = '#030303'), line = list(width = 0.5), 
         connectgaps=FALSE) 

enter image description here

对于这个问题

为了方便重复性的数据,这里是这个问题的数据。 OP需要从github下载.gml文件,并安装库(igraph)来处理数据。

es <- structure(list(
    V1 = c(1, 1, 2, 1, 2, 3, 1, 1, 1, 5, 6, 1, 2, 3, 4, 1, 3, 3, 1, 5, 6, 1, 1, 4, 1, 2, 3, 4, 6, 7, 1, 2, 1, 2, 
    1, 2, 24, 25, 3, 24, 25, 3, 24, 27, 2, 9, 1, 25, 26, 29, 3, 9, 15, 16, 19, 21, 23, 24, 30, 31, 32, 9, 10, 14, 15, 16, 19, 20, 
    21, 23, 24, 27, 28, 29, 30, 31, 32, 33), 
    V2 = c(2, 3, 3, 4, 4, 4, 5, 6, 7, 7, 7, 8, 8, 8, 8, 9, 9, 10, 11, 11, 11, 12, 13, 13, 
    14, 14, 14, 14, 17, 17, 18, 18, 20, 20, 22, 22, 26, 26, 28, 28, 28, 29, 30, 30, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 
    33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34)), 
    .Names = c("V1", "V2"), row.names = c(NA, -78L), class = "data.frame") 

theta <- seq(0,2,length.out=35)[1:34] 
Xn <- cospi(theta) 
Yn <- sinpi(theta) 

Nv <- NROW(Xn) 
Ne <- NROW(es) 
vs <- data.frame(label = as.character(1:Nv)) 
+0

谢谢@dww - 伟大的解决方案! –

+0

@GaborSzalai,你的欢迎。作为未来的指针,如果你将问题简化为一个简单的例子,你可以帮助人们更有效地回答问题。 'igraph'与这个问题无关,所以最好只提供一些数据(就像我上面所做的那样)以及重现问题所需的最少量的代码。看看[这](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)一些很棒的提示 – dww