Data included here - one row per edge. Recipes have names starting “Recipe:” and compounds start “compound:”. Ingredients have no prefix. This data should really be in a separate file:
library(igraph)
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
data = "From, To
Recipe:Chicken Marsala,flour
Recipe:Chicken Marsala,sage
Recipe:Chicken Marsala,chicken
Recipe:Chicken Marsala,wine
Recipe:Chicken Marsala,butter
Recipe:Glazed Carrots,butter
Recipe:Glazed Carrots,vinegar
Recipe:Glazed Carrots,carrot
Recipe:Glazed Carrots,chive
flour,compound:X2
sage,compound:X3
chicken,compound:X6
chicken,compound:X7
wine,compound:X1
wine,compound:X4
wine,compound:X5
wine,compound:X8
wine,compound:X9
wine,compound:X10
wine,compound:X11
wine,compound:X12
butter,compound:X4
butter,compound:X5
butter,compound:X7
butter,compound:X8
butter,compound:X11
vinegar,compound:X8
vinegar,compound:X13
carrot,compound:X2
carrot,compound:X15
chive,compound:X6
chive,compound:X14
"
Read the data in from the text version above into a data frame:
data=read.csv(textConnection(data),head=TRUE)
Make a graph out of it:
g = graph_from_data_frame(data,directed=FALSE)
Assign numbers to layers by type. layer 2 is ingredients, layer 1 is recipes, layer 3 is compounds:
layer = rep(2, length(V(g)$name))
layer[grep("Recipe:",V(g)$name)]=1
layer[grep("compound:",V(g)$name)]=3
now get rid of the prefix
names = V(g)$name
names = sub("Recipe:","", names)
names = sub("compound:","", names)
V(g)$name = names
Now compute a layout
layout = layout_with_sugiyama(g, layers=layer)
Now plot using the coordinates from the layout. Default seems to be vertical, so use first column as Y coordinate and layer number as X coordinate. Set shape and size etc by layer.
plot(g,
layout=cbind(layer,layout$layout[,1]),
vertex.shape=c("square","circle","none")[layer],
vertex.size=c(50,20,0)[layer],
vertex.label.dist=c(0,0,.8)[layer],
vertex.label.degree=0)