网络映射错误:每个变量必须是1d原子向量或列表

时间:2017-02-16 15:18:17

标签: r ggplot2 plotly

我正在尝试使用各种包和方法创建网络地图。

以此为指导,我复制并粘贴了每一步

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

但是,当我尝试查看情节时,会出现此错误:

错误:每个变量必须是1d原子向量或列表。 问题变量:'x','y','xend','yend'

很明显,这个例子没有出现这个错误,它对创建者来说很好,但是当我尝试使用自己的数据时也会发生这种错误。

对此没有任何有用的答案 ggnet2 : Error: Each variable must be a 1d atomic vector or list

知道问题是什么吗?

我觉得它可能与原点和目标变量是字符有关,但是我不知道它们是否可以转换为数字而我实际上并不希望它们像我想要的那样数字要显示的原点和目的地。

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()

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分:0)

问题中报告的问题似乎已消失,因为问题中发布的代码在运行时没有任何问题(有关软件包版本,请参见下面的会话信息)。

问题很可能与希望获得数据帧的ggnetwork无关,而与tibbles(由dplyr内部使用)有关,问题更加严格接受为“整洁”的数据帧。

> 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