Nate Miller
Nate Miller

Reputation: 386

ggnet2 : Error: Each variable must be a 1d atomic vector or list

I am trying to use ggnet2 for visualizing a network analysis, but have run into an error with the vignette.

I can generate a random network,

library(ggnet2)
library(network)
library(sna)
library(ggplot2)

net = rgraph(10, mode = "graph", tprob = 0.5)
net = network(net, directed = FALSE)

# vertex names
network.vertex.names(net) = letters[1:10]

With an output that looks reasonable

 >net
 Network attributes:
 vertices = 10 
directed = FALSE 
hyper = FALSE 
loops = FALSE 
multiple = FALSE 
bipartite = FALSE 
total edges= 28 
missing edges= 0 
non-missing edges= 28 

Vertex attribute names: 
vertex.names 

No edge attributes

However, when I try to run..

ggnet2(net)

I get an error

Error: Each variable must be a 1d atomic vector or list. Problem variables: 'x', 'y', 'xend', 'yend'

I am not clear on how this error is arising in the vignette as net is a list, and all variables within it are lists. I have checked to ensure that I have all the necessary packages and they are up-to-date as well as the most recent R version.

I just tried ggnetwork and seem to get a similar error.

Any thoughts on why this errors is arising?

Upvotes: 4

Views: 3462

Answers (3)

4126cfbe1fd5
4126cfbe1fd5

Reputation: 136

I think I've found a solution for the issue, at least on my machine. I'm running R 3.4.3 on Win 10, but this is probably not platform-related.

Until now I've had the CRAN version of GGally installed, along with network and sna, just as mentioned in the example, and this setup threw the exact same error mentioned above. I was confused as the original network object doesn't even have members like x, xend, etc. It turned out that these are in fact members of the ggnetwork object, which is created invisibly during the command ggnet() (ggnetwork vignette).

So I've tried installing ggnetwork: install.packages("ggnetwork") along with the source version of ggnet (devtools::install_github("briatte/ggnet")).

  • ggnetwork was installed succesfully, but the package couldn't be loaded because it said that ggplot2 is not installed (which I've found strange because it's a package that I use on a daily basis as part of the tidyverse library).

  • ggnet installation failed for some reason related to packages being already loaded.

I've restarted the R session, tried loading ggnetwork again, which again failed for lack of ggplot2, so I installed ggplot2. I tried once again installing ggnet from source, (this time before any packages were loaded) and it worked.

So, to sum it up:

After restarting the R session:

install.packages("ggnetwork")
install.packages("ggplot2")
devtools::install_github("briatte/ggnet")

I don't know exactly which step did it, but afterwards the example script produced a graph plot, just as it was supposed to:

library(ggnetwork)
library(network)
library(sna)
library(ggnet)

net = rgraph(10, mode = "graph", tprob = 0.5)
net = network(net, directed = FALSE)

network.vertex.names(net) = letters[1:10]

ggnet2(net)

Upvotes: 0

Thomas Luechtefeld
Thomas Luechtefeld

Reputation: 1456

The error message says

Error: Each variable must be a 1d atomic vector or list. Problem 
variables: 'x', 'y', 'xend', 'yend'

If you do the following

library(ggnetwork)
net <- ggnetwork(net)
class(net$x)

you will find that x is a one column matrix. The same is true for your other components. So doing

net$x <- net$x[,1]
net$y <- net$y[,1]
net$xend <- net$xend[,1]
net$yend <- net$yend[,1]

will change these all to 1d atomic vectors and

ggplot(net, aes(x = x, y = y, xend = xend, yend = yend)) +
    geom_edges(aes(linetype = "directed"), color = "grey50")

should work. You can read more about how to make ggnetwork work for prettier graphs.

Upvotes: 1

Jackson
Jackson

Reputation: 106

I had the same issue following the tutorial at briatte.github.io/ggnet.

The issue went away after I updated the 'igraph', 'ggplot', 'intergraph', 'GGally' packages and installed the 'ggnetwork' from source: install.packages("ggnetwork", type="source").

This was suggested here to fix another issue, but somehow it worked for me on this one. Perhaps you could give it a try.

Upvotes: 1

Related Questions