1

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.

1 Answers1

0

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')
  • Thank you very much for helping me. I do have a question though, I was able to find this code for the layout "centrality": [github](https://github.com/thomasp85/ggraph/blob/main/R/layout_centrality.R). Does this mean, that there is layout called "centrality"? And if so, what do I need to change in my code to get it to work? – Christian Langenohl Mar 30 '23 at 09:28
  • I see! You're right. I tested it out. it seems it needs another variable in the ggraph call, "centrality". I tested it out on your example data, but it required that all the nodes were connected to at least one other (your g is not). I will edit my original answer to include the code using that layout – Kyle Kimler Mar 30 '23 at 20:43
  • Hey, thank you. Could you explain to me, why my "g" is not connected? When I look at my edges it says that edge "c" and "g" are connected. Also, do you know how to view the complete liste of nodes and edges of a tidygraph object? Kind Regards – Christian Langenohl Apr 01 '23 at 15:04