Skip to content

Instantly share code, notes, and snippets.

@schochastics
Created March 29, 2021 15:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save schochastics/69dcbf471b314914566cd30071dd84a1 to your computer and use it in GitHub Desktop.
Save schochastics/69dcbf471b314914566cd30071dd84a1 to your computer and use it in GitHub Desktop.
use constrained stress
library(igraph)
library(ggraph)
library(graphlayouts)
#handdrawn graph without isolates
el <- matrix(c(1,3,1,3,1,6,2,3,2,6,
3,4,3,4,3,6,4,6,4,8,
5,7,5,8,7,8,7,9,7,10,
7,11,9,10,10,11,9,11),ncol = 2,byrow = TRUE)
g <- graph_from_edgelist(el,F)
# add attribute
V(g)$year <- c(2005,2005,2006,2006,2007,2007,2008,2008,2009,2010,2011)
E(g)$type <- "paper"
E(g)$type[c(2,3,8)] <- "supervision"
# constrained stress layout (only works with a connected network)
xy <- layout_with_constrained_stress(g,coord = V(g)$year,fixdim = "x")
xy[,2] <- xy[,2]-min(xy[,2])
ggraph(g,"manual",x=xy[,1],y=xy[,2])+
geom_edge_parallel(aes(linetype=type))+
geom_node_point(shape=21,fill="white",size=7)+
geom_node_text(label=1:vcount(g))+
geom_vline(xintercept = 2006:2011-0.5,size=0.2,linetype=2)+
annotate("text",x=2005:2011,y=0,label=2005:2011,vjust=3)+
theme_graph()+
coord_cartesian(clip = "off")
@schochastics
Copy link
Author

year

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment