library(learnr) knitr::opts_chunk$set(echo = FALSE, message=FALSE, warning=FALSE)
library("igraph")
In case you did not run the Creating Graphs tutorial, you can run the following code:
data(chemodats) gr = graph_from_data_frame(chemodats[,c("node1", "node2")], directed = FALSE) E(gr)$weight = 1 V(gr)$size = centralization.degree(gr)$res
If you look at the help for igraph.plotting
you find a description of many different choices for laying out the graph as usually the x and y coordinates are not provided
data but extra information that is only computed for aesthetic or clarity.
If you don't provide a layout all the graph plotting functions will pick one for your (very often a Fruchterman-Reingold) which tries to stretch the graph enough so that are not too many overlapping edges, a little like Multidimensional Scaling stretches the points out.
Let's look at some examples; these are all the same graphs but with different layout choices.
co <- layout_with_fr(gr) ## Fruchterman-Reingold layout (force directed layout) plot(gr, layout=co) ## On a circle coordsc <- layout_in_circle(gr) plot(gr, layout=coordsc) ##The Kamada-Kawai layout algorithm plot(gr, layout=layout_with_kk)
Try to find other layout choices (on a grid, on a sphere, through multidimensional scaling).
lgrid <- layout_on_grid(gr) lmds <- layout_with_mds(gr) lsphere <- layout_on_sphere(gr)
lmds <- layout_with_mds(gr) lgrid <- layout_on_grid(gr) lsphere <- layout_on_sphere(gr) plot(gr, layout=lmds) plot(gr,layout=lgrid) plot(gr, layout=lsphere)
Some questions to verify that you understand the way we use graphs:
quiz( question("Which package contains functions for plotting graphs?", answer("base"), answer("graph"), answer("igraph", correct = TRUE), answer("codetools") ), question("Which of the objects listed below are components of a graph?", answer("vertex", correct = TRUE), answer("border"), answer("center"), answer("edge", correct = TRUE) ) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.