问题
I'm trying to use the example code here for doing iGraph network graphs in plotly and shoehorn in my own data.frames
instead of using the example karate club data. When the graph is plotted, it seems to ignore the edge list and a bunch of random connections are being made. I think either the labels or edges are wrong but I can't tell.
library(igraph)
library(plotly)
setwd('C:/Users/Andrew Riffle/Documents/MEGAsync/code/R/link_analysis')
ID <- c(1:50)
nodes <- data.frame(ID)
Source <- c(23, 24, 36, 20, 36, 41, 12, 8, 18, 28)
Target <- c(5, 7, 9, 35, 23, 12, 38, 29, 33, 45)
links <- data.frame(Source, Target)
net <- graph_from_data_frame(d=links, vertices=nodes, directed=FALSE)
net <- simplify(net, remove.multiple = F, remove.loops = T)
tkplot(net, vertex.label=nodes$id, vertex.label.color='white', layout=layout.fruchterman.reingold)
#####Begin plotly example code unmodified unless commented#####
G <- upgrade_graph(net) #put my iGraph object instead of the karate club one
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(x = ~Xn, y = ~Yn, mode = "markers", text = vs$name, hoverinfo = "text")
edge_shapes <- list()
for(i in 1:Ne) {
v0 <- es[i,]$V1
v1 <- es[i,]$V2
edge_shape = list(
type = "line",
line = list(color = "#030303", width = 0.3),
x0 = Xn[v0],
y0 = Yn[v0],
x1 = Xn[v1],
y1 = Yn[v1]
)
edge_shapes[[i]] <- edge_shape
}
axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE)
p <- layout(
network,
title = 'Test Network', #Changed the title
shapes = edge_shapes,
xaxis = axis,
yaxis = axis
)
p #added a call to display p instead of upload to plot.ly
When I run this, I get this nice pretty iGraph that has been plotted by Plotly. However, the edges are incorrect. It appears that only the ID's 1-10 are connecting, and only to other ID's less than 10. None of these connections is in the edge list, which is below.
Source Target
1 24 35
2 12 23
3 41 12
4 23 7
5 18 5
6 20 9
7 28 29
8 36 45
9 8 33
10 36 38
Does anyone see what I'm doing wrong? Help appreciated.
回答1:
Discovered that the tutorial is actually wrong. If you plot the same karate network using plot
compared to using plotly
, there are many connections in plot
that aren't in plotly
.
I've given up trying to make this work, visNetwork is an excellent alternative that I've had a much easier time figuring out. Recommend that for anyone with a similar issue reading this.
回答2:
I know it's a bit too late for the OP, but for the others who stumbled upon this same problem (as did I).
May I suggest the following five changes in the tutorial source code:
G <- upgrade_graph(net)
L <- layout.circle(G)
rownames(L) <- get.vertex.attribute(G)$name #added line (#1 out of 5)
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(x = ~Xn, y = ~Yn, mode = "markers", text = vs$name, hoverinfo = "text")
edge_shapes <- list()
for(i in 1:Ne) {
v0 <- es[i,]$V1
v1 <- es[i,]$V2
edge_shape = list(
type = "line",
line = list(color = "#030303", width = 0.3),
x0 = L[which(v0==rownames(L)),][1], #changed line (#2 out of 5)
y0 = L[which(v0==rownames(L)),][2], #changed line (#3 out of 5)
x1 = L[which(v1==rownames(L)),][1], #changed line (#4 out of 5)
y1 = L[which(v1==rownames(L)),][2] #changed line (#5 out of 5)
)
edge_shapes[[i]] <- edge_shape
}
axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE)
p <- layout(
network,
title = 'Test Network', #Changed the title
shapes = edge_shapes,
xaxis = axis,
yaxis = axis
)
p
Works fine now.
来源:https://stackoverflow.com/questions/45701184/igraph-plotly-creates-random-connections