Skip to content

Instantly share code, notes, and snippets.

@mpettis
Last active November 30, 2022 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpettis/3482d967176f43863df609a073bc62c8 to your computer and use it in GitHub Desktop.
Save mpettis/3482d967176f43863df609a073bc62c8 to your computer and use it in GitHub Desktop.
Using igraph, some basic i/o and plotting.
#' ---
#' title: "Graph_Explorations.R"
#' author: "Matt Pettis (Matthew.Pettis@gmail.com)"
#' date: "`r Sys.Date()`"
#' output:
#' html_document:
#' toc: true
#' toc_depth: 3
#' code_folding: hide
#' editor_options:
#' chunk_output_type: console
#' ---
#'
#+message=F,warning=F
#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#;; Setup
library(tidygraph)
library(DiagrammeR)
library(DiagrammeRsvg)
library(igraph)
# library(janitor)
library(glue)
# library(purrr)
# library(lubridate)
library(tidyverse)
#;; https://www.bioconductor.org/packages/release/bioc/html/Rgraphviz.html
library(Rgraphviz)
#' # Graphs on a simple tree, directed
#'
#' The label, color, and shape attributes added to the nodes dictate those
#' properties when the graph is plotted.
#'
#+
#;; Make a tree igraph
nodesTree1_df <-
tribble(
~name, ~label, ~color, ~shape
, "A", "root", "red", "circle"
, "B", "L1-1\nmore text", "yellow", "rectangle"
, "C", "L1-2", "yellow", "rectangle"
, "D", "L1-1-1", "green", "none"
, "E", "L1-1-2", "green", "none"
, "F", "L1-1-3", "green", "none"
, "G", "L1-2-1", "green", "none"
, "H", "L1-2-2", "green", "none"
)
edgesTree1_df <-
tribble(
~from, ~to
, "A", "B"
, "A", "C"
, "B", "D"
, "B", "E"
, "B", "F"
, "C", "G"
, "C", "H"
)
#;; Make the graph
gTree1 <- graph_from_data_frame(d = edgesTree1_df %>%
select(from, to, everything())
, directed = TRUE
, vertices = nodesTree1_df %>%
select(name, everything()))
#' ## Extract the parts
#'
#' If you also import tibble, or parts of the tidyverse, you have to explicitly
#' call `igraph::as_data_frame()`, because you'll see this message:
#'
#' dplyr::as_data_frame() masks tibble::as_data_frame(), igraph::as_data_frame()
#'
#' when you load the libraries.
#'
#' See: https://igraph.org/r/html/latest/graph_from_data_frame.html
#+
igraph::as_data_frame(gTree1, what="vertices")
igraph::as_data_frame(gTree1, what="edges")
#' ## Plot it
#'
#' Default plot isn't what we want, doesn't plot like a tree.
#+
plot(gTree1)
#' But we can control options.
#'
#' See: https://igraph.org/r/html/latest/plot.common.html
#+
plot(gTree1, layout=layout_as_tree)
#' But we can control these options globally as well. We have to prefix global
#' options with 'plot.' This will do the same thing:
#'
#' ```
#' igraph_options(plot.layout=layout_as_tree)
#' plot(gTree1)
#' ```
#'
#' ## Save plot
#' Save as named svg file.
#'
#' See: https://stackoverflow.com/questions/48796194/export-r-plot-via-svg-without-zooming
#'
#' ````
#' svg(here::here("dat", "tree1.svg"))
#' plot(gTree1, layout=layout_as_tree)
#' dev.off() %>% invisible()
#' ````
#'
#' ## Convert to graphviz and using it
#' See:
#'
#' - https://igraph.org/r/html/latest/write_graph.html
#'
#' Not helpful, but here:
#'
#' See:
#'
#' - https://rdrr.io/cran/DiagrammeR/man/from_igraph.html
#' - https://rich-iannone.github.io/DiagrammeR/graphviz_and_mermaid.html
#'
#+
#;; Save as dot file
write_graph(gTree1, here::here("dat", "tree1.dot"), format="dot")
#;; Read in and render with graphviz engine
DiagrammeR:::grViz(here::here("dat", "tree1.dot"))
#;; Read in dot file to Ragraph object
#;; See: https://support.bioconductor.org/p/48598/
rgvzTree1 <-
Rgraphviz::agread(here::here("dat", "tree1.dot"))
# renderGraph(rgvzTree1)
#;; Create a graphNEL object
#;; Convert to Ragraph object
#;; https://www.bioconductor.org/packages/release/bioc/manuals/Rgraphviz/man/Rgraphviz.pdf
#;; https://bioconductor.org/packages/release/bioc/html/Rgraphviz.html
agopen(randomGraph(letters[1:10], 1:4, 0.2), "g1")
#;; This works!!!
#;; Try to convert iGraph -> graphNEL -> Ragraph object.
rgvz2Tree1 <-
igraph::as_graphnel(gTree1) %>%
agopen("gTree1")
#;; Some plotting
plot(rgvz2Tree1)
plot(rgvz2Tree1, attrs=list(graph=list(rankdir="LR")))
plot(rgvz2Tree1, attrs=getDefaultAttrs())
g1 <-
plot(randomGraph(letters[1:10], 1:4, 0.2))
plot(g1, attrs=getDefaultAttrs())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment