R ggnetwork:无法更改图形布局

R ggnetwork: unable to change graph layout

我正在尝试使用 ggnetwork 和 ggplot2 绘制一些图形可视化,但我无法更改 ggnetwork 函数附带的图形布局参数。我的可重现代码如下,我是 运行 this on R 4.0.3 on Ubuntu

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=' 参数,代码就会运行。但是我真的需要更改布局。

布局参数不采用字符串,而是 igraph::layout_ 函数的输出。

所以你可以这样做:

new_g2 <- ggnetwork(g1, layout = igraph::layout.kamada.kawai(g1))

ggplot(new_g2, aes(x, y, xend = xend, yend = yend)) +
  geom_edges(colour = "grey") +
  geom_nodes(size = 8, aes(colour = name)) +
  theme_blank() + 
  labs(caption = 'WDI School enrollment and progression datasets') +
  theme(plot.caption = element_text(size = 16))