thomasp85 / ggraph

Grammar of Graph Graphics

Home Page:https://ggraph.data-imaginist.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error in layout_with_sparse_stress()

Polligator opened this issue · comments

commented

run the example code:
library(igraph)
library(ggraph)

g <- sample_gnp(1000,0.005)

ggraph(g,layout = "sparse_stress",pivots = 100)+
geom_edge_link0(edge_colour = "grey66")+
geom_node_point(shape = 21,fill = "grey25",size = 5)+
theme_graph()

Got error:
Error in layout_with_sparse_stress(graph, pivots = pivots, weights = weights, :
only connected graphs are supported.

This is an issue of graphlayouts not ggraph (well if it is an official example, than it is also an issue of ggraph). The layout_with_sparse_stress() function only supports connected graphs and sample_gnp() produced a disconnected one by chance. In this case, the pivot handling gets too complicated for the unconnected case but here is a hack:

library(igraph)
library(ggraph)


set.seed(12)
g <- sample_gnp(1000,0.005)
V(g)$id <- seq_len(vcount(g))
gl <- decompose(g)
pivots <- 100
ll <- lapply(gl,function(x){
  if(vcount(x)<pivots){
    graphlayouts::layout_with_stress(x)
  }else{
    graphlayouts::layout_with_sparse_stress(x,pivots = pivots)
  }
}) 

l <- merge_coords(gl, ll)
l[unlist(sapply(gl, vertex_attr, "id")), ] <- l[]

ggraph(g,layout = "manual",x=l[,1],y=l[,2])+
  geom_edge_link0(edge_colour = "grey66")+
  geom_node_point(shape = 21,fill = "grey25",size = 5)+
  theme_graph()
#> Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.

One of these days I will implement that (schochastics/graphlayouts#48)

commented

You are legendary. Thanks so much.