Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gigantic arrow heads when plotting network with igraph

EDIT I was trying to figure out what is wrong with my code and I started to plot simple graphs to see how the arrow will look on smaller graphs. I tired the following command:

 g2 <- graph( edges=c(1,2, 2,3, 3, 1), n=10 ) 
 plot(g2)   

And here is my graph: . Thus, I think the problem is not with my code but either with igraph or R. I re-installed both, igraph and R but it did not solve the problem. Is it possible that there is a conflict of packages that leads to this? Here is a lit of packages that I have installed:

 [1] "base"         "boot"         "class"        "cluster"     
 [5] "codetools"    "colorspace"   "compiler"     "datasets"    
 [9] "dichromat"    "digest"       "doParallel"   "foreach"     
[13] "foreign"      "graphics"     "grDevices"    "grid"        
[17] "gridBase"     "gtable"       "igraph"       "irlba"       
[21] "iterators"    "KernSmooth"   "labeling"     "lattice"     
[25] "lazyeval"     "magrittr"     "MASS"         "Matrix"      
[29] "methods"      "mgcv"         "munsell"      "nlme"        
[33] "NMF"          "nnet"         "parallel"     "pkgmaker"    
[37] "plyr"         "RColorBrewer" "Rcpp"         "registry"    
[41] "reshape2"     "rngtools"     "rpart"        "scales"      
[45] "spatial"      "splines"      "stats"        "stats4"      
[49] "stringi"      "stringr"      "survival"     "tcltk"       
[53] "tibble"       "tools"        "utils"        "xtable"    

I am trying to generate a plot of a network and for some reason I my arrowheads look like small rectangles instead of the usual triangle arrowheads.

Here is the code I am using for my plots:

toy.edges <- na.omit(read.csv("Data/Edge_list-toy.csv", header = TRUE, colClasses = "numeric", na.strings = c("NA", "", "#N/A")))
toy.nodes <- na.omit(read.csv("Data/NodesDataF-toy.csv", header = TRUE, na.strings = c("NA", "", "#N/A")))
toy.graph <- graph_from_data_frame(toy.edges, directed = TRUE, vertices = toy.nodes)

V(toy.graph)$color <- "magenta" 
V(toy.graph)$shape <- "sphere"
V(toy.graph)$size <- 3*15^(ifelse(is.na(V(toy.graph)$node.size), 0.001, 
V(toy.graph)$node.size))
plot(toy.graph, layout = layout.fruchterman.reingold(toy.graph),
     vertex.label=NA, edge.width=E(toy.graph)$weight, 
     edge.arrow.size=0.005, edge.arrow.width=0.0000001)

And here is an example plot:

[1]: https://i.stack.imgur.com/iCx

It looks even worse when I take slightly bigger values for edge.arrow.size and edge.arrow.width.

What is wrong with my code? Can it have something to do with the version of R? I made a ton of plots before, using very similar command, and I never had problems.

Here are files with nodes info and edge list.

like image 534
Justyna Avatar asked May 07 '17 19:05

Justyna


3 Answers

So it seems like the problem is with how R displays graphic on my computer. When, instead of plotting the figure directly in the console, I save it to a file, everything looks just fine. Here is a code I am using, in case someone else faces similar problem:

png("my-plot.png", width=1200, height=1200)
par(mar=c(0,0,0,0))
plot(mat_gr, layout = layout.auto(mat_gr), vertex.label=NA,   
     edge.width=E(mat_gr)$weight)
dev.off()

I realize it does not solve the problem with displaying gigantic arrowheads but at least it allows to generate as save usable plots.

like image 100
Justyna Avatar answered Dec 15 '22 21:12

Justyna


Using plot.igraph() rather than simply plot() may be of help here.

like image 41
Jason Avatar answered Dec 15 '22 22:12

Jason


The parameter edge.arrow.size may help to reduce the arrow head size. There are more information available via this link to the documentation of the plot function. Basically, we can use all listed parameters by adding either vertex/edge in front of the parameters.

plot(g2, edge.arrow.size = 0.1)
like image 42
devance Avatar answered Dec 15 '22 22:12

devance