2

I am attempting to create a network map using various packages and methods.

Using this as a guide, I have copied and pasted each step

http://minimaxir.com/notebooks/interactive-network/

However, when I try to view plot it comes up with this error:

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

Clearly this error has not come up on that example and it works fine for the creator but it also happens when I have tried using my own data too.

There aren't any useful answers to this ggnet2 : Error: Each variable must be a 1d atomic vector or list

Any idea what the issue could be?

I have a feeling it may be to do with the origin and destination variables being characters, however I don't know if they can be converted to numeric and I wouldn't actually want them as numeric as I would like the name of origin and destination to be displayed.

library(dplyr)
library(nycflights13)
library(igraph)
library(sna)
library(ggnetwork)

df_edges <- flights %>% group_by(origin, dest) %>% summarize(weight = n()) 
net <- graph.data.frame(df_edges, directed = T) 
V(net)$degree <- centralization.degree(net)$res 
df_net <- ggnetwork(net, layout = "fruchtermanreingold", weights = "weight", niter = 5000) 
ggplot(df_net, aes(x = x, y = y, xend = xend, yend = yend)) + geom_edges(size = 0.4, alpha = 0.25) + geom_nodes(aes(size = degree, text = vertex.names)) + ggtitle("Network Graph of U.S. Flights Outbound from NYC in 2013") + theme_blank()
Fr.
  • 2,865
  • 2
  • 24
  • 44
MLMM
  • 85
  • 2
  • 8
  • 1
    Welcome to Stackoverflow! Can you post a minimal example which can reproduce the error? – Maximilian Peters Feb 16 '17 at 16:10
  • library(dplyr) library(nycflights13) library(igraph) library(sna) library(ggnetwork) df_edges <- flights %>% group_by(origin, dest) %>% summarize(weight = n()) net <- graph.data.frame(df_edges, directed = T) V(net)$degree <- centralization.degree(net)$res df_net <- ggnetwork(net, layout = "fruchtermanreingold", weights = "weight", niter = 5000) ggplot(df_net, aes(x = x, y = y, xend = xend, yend = yend)) + geom_edges(size = 0.4, alpha = 0.25) + geom_nodes(aes(size = degree, text = vertex.names)) + ggtitle("Network Graph of U.S. Flights Outbound from NYC in 2013") + theme_blank() – MLMM Feb 17 '17 at 09:25
  • Thanks! I added your code to the question. It runs without any problems for me. https://i.stack.imgur.com/GW5St.jpg – Maximilian Peters Feb 17 '17 at 09:34
  • Thank you for that! I've also tested it on a colleague's R and it works fine on hers so it's just an issue with mine. Do you know what it might be or what could be done to fix it? I am using version 3.3.2 but my colleague is on 3.3.1 if that helps at all – MLMM Feb 17 '17 at 10:00
  • Try updating your packages as suggested here: https://www.r-bloggers.com/update-all-user-installed-r-packages-again/ – Maximilian Peters Feb 17 '17 at 10:06
  • I tried that but had no luck so ended up erasing all the packages from my computer, uninstalling R then reinstalling it and loaded all the packages again and this has worked. Thanks for your help! – MLMM Feb 17 '17 at 10:55

2 Answers2

0

I had the same issue using ggnet and ggnetwork recently. A current workaround is to export the data generated by ggnetwork and import it again (I am a simple man):

library(readr)
write_csv(df_net,"dat.csv")
df_net <- read_csv("dat.csv")
thomasB
  • 303
  • 3
  • 11
0

The issue reported in the question seems to have vanished, as the code posted in the question runs without any issue on my end (see session info below for the package versions).

It is likely that the issue had to do not with ggnetwork, which expects a data frame, but with tibbles (used internally by dplyr), which is stricter in what it accepts as a 'tidy' data frame.

> sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggnetwork_0.5.1      ggplot2_3.2.0        sna_2.4              statnet.common_4.3.0
[5] igraph_1.2.4.1       nycflights13_1.0.0   dplyr_0.8.1          network_1.15        
[9] survival_2.44-1.1   

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1         pillar_1.4.1       compiler_3.6.0     RColorBrewer_1.1-2
 [5] plyr_1.8.4         tools_3.6.0        digest_0.6.19      tibble_2.1.3      
 [9] gtable_0.3.0       lattice_0.20-38    pkgconfig_2.0.2    rlang_0.4.0       
[13] Matrix_1.2-17      GGally_1.4.0       rstudioapi_0.10    ggrepel_0.8.1     
[17] coda_0.19-2        withr_2.1.2        grid_3.6.0         tidyselect_0.2.5  
[21] reshape_0.8.8      glue_1.3.1         R6_2.4.0           purrr_0.3.2       
[25] magrittr_1.5       scales_1.0.0       splines_3.6.0      assertthat_0.2.1  
[29] colorspace_1.4-1   labeling_0.3       intergraph_2.0-2   lazyeval_0.2.2    
[33] munsell_0.5.0      crayon_1.3.4  

enter image description here

Fr.
  • 2,865
  • 2
  • 24
  • 44