ggraphnode-centrality

ggraph throws Error in eval_tidy(enquo(centrality), .N()) : object '' not found


I'm trying to create a network graph based on centrality and I don't understand the error, that I'm getting. The code is following:

library("tidyverse","dplyr","Hmisc", "igraph")
library("tidygraph")
library("ggraph")

#create dataframe

V1 <- c("a","b","c","d")
V2 <- c("e","f","g","d")
cor <- c(0.1,0.2,0.3,0.4)
df <- data.frame(V1,V2,cor)

#create tidygraph

set.seed(1)
cor.graph <- as_tbl_graph(df, directed = FALSE)

#setting nodes

nodes <- df %>%
  select(V1)

#activate nodes and get centrality

cor.graph <- cor.graph %>%
  activate(nodes) %>%
  mutate(centrality = centrality_authority())

#activate edges

cor.graph <- cor.graph %>%
  activate(edges) 

set.seed(123)

#plot 

ggraph(cor.graph,layout = "centrality" )

Throwing this error

Error in eval_tidy(enquo(centrality), .N()) : object '' not found

Any help is highly appreciated.


Solution

  • The errors are 2-fold - the choice of "layout" in the ggraph call 'centrality' requires a second argument, centrality, as below. Other network layouts include 'kk', 'stress'. The 'centrality' layout seems to have additional requirements, like having at least one connection per node

    https://cran.r-project.org/web/packages/ggraph/vignettes/Layouts.html

    your data setup is fine. As a starting point for the graph, you need to add edges and nodes to your graph, and i'm assuming you want the nodes to be sized and maybe colored by centrality. I'd do something like this:

    ggraph(cor.graph, centrality = centrality_authority(), 
            layout = "centrality") + 
        geom_node_point(aes(size=centrality,color=centrality)) + 
        geom_edge_link(aes(color=cor)) + 
        scale_color_distiller(palette='Spectral')