D. de Jonge
D. de Jonge

Reputation: 103

Why do I get the error "Error in y1 - y2 : non-numeric argument to binary operator" when adding a shape to my igraph network plot?

Edit: I have managed to set only the specific nodes I wanted to change shape. However, I am still curious what this error is pointing to. Solution:

V(net)$shape <- "circle"
V(net)[c(2, 4, 7, 8, 9)]$shape <- "square". 

Problem: I am trying to plot a network, but I have issues with changing the vertex shape. I think it has something to do with my data, but I don't know where to search for my error.

Working version:

This code is working fine. I create a data frame with information about the nodes and vertices, and use that in the igraph function graph_from_data_frame() to create the data structure to plot. I create a matrix which contains the x and y positions of my vertices, and then plot my network. In the plot the vertices are positioned correctly according to "l".

# Get nodes and links
nodes <- getNodes(site = site, datapath = pathdata)
links <- getEdges(site = site, datapath = pathdata)

# Get net
net <- graph_from_data_frame(d=links, vertices=nodes, directed=T) 

# Lay-out and plot
l <- matrix(c(as.numeric(nodes$X), as.numeric(nodes$Y)),ncol=2)
plot(net, layout=l)

Problem: However, if I want to change the default shape of the vertices (circle) into a square, I get an error I cannot place.

nodes <- getNodes(site = site, datapath = pathdata)
links <- getEdges(site = site, datapath = pathdata)

net <- graph_from_data_frame(d=links, vertices=nodes, directed=T) 

l <- matrix(c(as.numeric(nodes$X), as.numeric(nodes$Y)),ncol=2)
V(net)$shape <- "square"
plot(net, layout=l)

The error I get is "Error in y1 - y2 : non-numeric argument to binary operator". I thought it may had to do with the positions in matrix "l" but these are all numeric:

is.numeric(l) [1] TRUE

I reckon the issue must be in the rest of the data maybe? Does anyone know where this error is pointing at? My data frame with information about the nodes and vertices is quite large, so I can't post them here, but the types all seem fine (numeric what has to be numeric etc). Thank you!

Upvotes: 1

Views: 246

Answers (1)

Kevin Coombes
Kevin Coombes

Reputation: 61

I don't believe the error has anything to do with shapes. When I get it, a "traceback()" points to something in igraph.Arrows, which is called by plot.igraph. Further exploration suggests that it occurs when two nodes are too close together. Identical/duplicated is clearly a problem. But manually moving one of the nodes a small amount doesn't necessarily fix things. I have, however, had success with

plot(myGraph, layout = jitter( myLayoutMatrix) )

Upvotes: 0

Related Questions