Reputation: 281
I need to draw and save to SVG a network with directed connections. I took example from here: https://www.r-graph-gallery.com/251-network-with-node-size-based-on-edges-number.html
Here is my example with data:
library(igraph)
# create data:
links=data.frame(
source=c("A","A", "A", "A", "A","J", "B", "B", "C", "C", "D","I"),
target=c("B","B", "C", "D", "J","A","E", "F", "G", "H", "I","I")
)
# Turn it into igraph object
network <- graph_from_data_frame(d=links, directed=T)
# Count the number of degree for each node:
deg <- degree(network, mode="all")
# Plot
svg("c:\\temp\\network01.svg",
width = 7, height = 7, pointsize = 12)
plot(network, vertex.size=30, vertex.color=rgb(0.1,0.7,0.8,0.5) , edge.arrow.size = 0.7)
dev.off()
It's okay exept this part:
What I expected here - bigger link line length, 2 links instead of 1 2-way link. Something like this:
Is this possible?
BTW. Picture is different every time I generate it. Sometimes A <-> J link looks even worse:
Upvotes: 1
Views: 121
Reputation: 31452
You can specify the layout manually when the default arrangement is not good. For example:
layout = matrix(c(0,0, 0,1, 0,-1, -1,0, 1,0, 2,0, -0.5,-2,
0.5,-2, -2,-0.5, -2,0.5), byrow = T, ncol = 2)
plot(network, vertex.size=30, vertex.color=rgb(0.1,0.7,0.8,0.5),
edge.arrow.size = 0.2, layout=layout)
Note that the order in which the coordinates are specified must atch the order of the nodes in the igraph object (which you can see using lapply(network, names)
).
Alternatively, there are a wide number of automatic layout functions provided that you can try out to find a layout you like. ?layout_with_fr
provides a list of available layout functions. For example:
plot(network, vertex.size=30, vertex.color=rgb(0.1,0.7,0.8,0.5),
edge.arrow.size = 0.2,
layout=layout_with_sugiyama(network)$layout)
Upvotes: 3