我正在尝试 ggnetwork 和 ggplot2 绘制一些图形可视化,但我无法更改 ggnetwork 函数附带的图形布局参数。我的可重现代码如下,我在 Ubuntu 上的 R 4.0.3 上运行它
install.packages("WDI") # this is the data source I need for this example
library(WDI)
new_wdi_cache <- WDIcache()
library(igraph)
library(tidyverse)
library(ggnetwork)
education<-WDI(indicator=c("SE.PRM.ENRR","SE.SEC.ENRR",
"SE.TER.ENRR","SE.SEC.PROG.ZS","SE.PRM.CMPT.ZS"),
start=2014,
end=2014,
extra= TRUE,
cache=new_wdi_cache)
education<-education[education$region!="Aggregates",]
education<-na.omit(education)
education.features <- education[,4:8]
education.features_scaled <-scale(education.features)
education.distance_matrix <- as.matrix(dist(education.features_scaled))
education.adjacency_matrix <- education.distance_matrix < 1.5
g1<-graph_from_adjacency_matrix(education.adjacency_matrix, mode="undirected")
new.g2<-ggnetwork(g1, layout = "kamadakawai") # LINE A
ggplot(new.g2, aes(x=x, y=y, xend=xend, yend=yend))+
geom_edges(colour="grey")+geom_nodes(size=5,aes(colour=species ))+
theme_blank()+labs(caption='WDI School enrollment and progression datasets')
在 A 行,我收到一个我真的无法理解的错误:
Error: $ operator is invalid for atomic vectors
这意味着什么?如果我从 ggnetwork 中删除 'layout=' 参数,代码就会运行。但是我真的需要改变布局。