Reputation: 623
So I have been tasked with recreating a specific network as seen here:
While I have been able to figure out how to play around with the nodes, I am unsure how to assign each edge to a width. I am assuming it should be done similarly to node color/shape that is store as a vector. However how do I know exactly which edge is what?
library(sna)
hw2 <- rbind(
# 1 2 3 4 5 6 7 8 932 1 2 3 4 8 0 2
c(0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
c(1,0,1,1,0,0,0,1,0,0,0,0,0,1,1,1,1),
c(1,1,0,1,0,0,0,1,1,0,0,0,0,1,0,0,0),
c(1,1,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0),
c(1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0),
c(1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0),
c(1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0),
c(1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
c(1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
c(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
c(1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0),
c(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
c(1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
c(1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
c(1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
c(1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
c(1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
)
#Labels
rownames(hw2) <- c("a1","a2","a3","a4","a5","a6","a7","a8","a9","a32","a11","a12","a13","a14","a18","a20","a22")
colnames(hw2) <- c("a1","a2","a3","a4","a5","a6","a7","a8","a9","a32","a11","a12","a13","a14","a18","a20","a22")
#Colors
net_col <- c(rep("steelblue",8),rep("lightcoral",2),rep("steelblue",8))
edge_col <- c(rep("blue4",) )
#Shape
net_size <-c(rep(4,8),rep(50,2),rep(4,8))
#Network
net_hw2 <- network(hw2,matrix.type="adjacency")
#Graph 1 , similar to existing graph
gplot(net_hw2,vertex.col = net_col,usearrows = F,displaylabels = T,label.pos = 5,
vertex.cex = 3, vertex.sides = net_size)
Upvotes: 0
Views: 181
Reputation: 37641
You can assign a width to the edges using the edge.lwd argument to gplot. To match your target graph, I will also assign a color using edge.col. The only tricky part is figuring out which edge is which. I will just set the edge width and color for one edge. After that, I will let you go through to adjust all the others.
You can get all of the edges using as.edgelist(net_hw2)
This gives the edges based on the number of the nodes. You can match the node number to its name using your list
rownames(hw2)
. I will use as an example the edge between a3 and a9. This edge is thicker than most of the others and it is yellow instead of blue. Looking at rownames(hw2)
we see that a3 is node 3 and a9 is node 9. Looking through the output of as.edgelist(net_hw2)
we see that edges 29 and 51 connect these nodes.
I will make a vector of colors and widths so that the default color is navy and the default width is 1.
LWD = rep(1, nrow(as.edgelist(net_hw2)))
COL = rep("navy", nrow(as.edgelist(net_hw2)))
Now we can explicitly change the color and width for the edges connecting a3 and a9
LWD[29] = 8
COL[29] = "yellow"
LWD[51] = 8
COL[51] = "yellow"
Now you can just use that in the plot
gplot(net_hw2,vertex.col = net_col,usearrows = F, displaylabels = T,
label.pos = 5, vertex.cex = 3, vertex.sides = net_size,
edge.col=COL, edge.lwd=LWD)
Now we have a nice wide, yellow edge between a3 and a9. You can change the color and width of any other edges in the same way.
Upvotes: 1