## ----Load packages for quick start, eval=TRUE, message=FALSE------------------
#--- Load required packages
library("igraph")
library("ggplot2")
library("RGraphSpace")

## ----Toy igraph - 1, eval=TRUE, message=FALSE, results=FALSE------------------
# Make a 'toy' igraph with 5 nodes and 4 edges;
# ..either a directed or undirected graph
gtoy1 <- make_star(5, mode="out")

# Check whether the graph is directed or not
is_directed(gtoy1)
## [1] TRUE

# Check graph size
vcount(gtoy1)
## [1] 5
ecount(gtoy1)
## [1] 4

# Assign 'x' and 'y' coordinates to each vertex;
# ..this can be an arbitrary unit in (-Inf, +Inf)
V(gtoy1)$x <- c(0, 2, -2, -4, -8)
V(gtoy1)$y <- c(0, 0,  2, -4,  0)

# Assign a name to each vertex
V(gtoy1)$name <- paste0("n", 1:5)

## ----Toy igraph - 2, eval=TRUE, message=FALSE, out.width="100%"---------------
# Plot the 'gtoy1' using standard R graphics
plot(gtoy1)

## ----Toy igraph - 3, eval=TRUE, message=FALSE, out.width="80%"----------------
# Plot the 'gtoy1' using RGraphSpace
plotGraphSpace(gtoy1, add.labels = TRUE)

## ----Node attributes, eval=TRUE, message=FALSE--------------------------------
# Node size (numeric in [0, 100], as '%' of the plot space)
V(gtoy1)$nodeSize <- c(8, 5, 5, 10, 5)

# Node shape (integer code between 0 and 25; see 'help(points)')
V(gtoy1)$nodeShape <- c(21, 22, 23, 24, 25)

# Node color (Hexadecimal or color name)
V(gtoy1)$nodeColor <- c("red", "#00ad39", "grey80", "lightblue", "cyan")

# Node line width (as in 'lwd' standard graphics; see 'help(gpar)')
V(gtoy1)$nodeLineWidth <- 1

# Node line color (Hexadecimal or color name)
V(gtoy1)$nodeLineColor <- "grey20"

# Node labels ('NA' will omit labels)
V(gtoy1)$nodeLabel <- c("V1", "V2", "V3", "V4", NA)

# Node label size (in pts)
V(gtoy1)$nodeLabelSize <- 8

# Node label color (Hexadecimal or color name)
V(gtoy1)$nodeLabelColor <- "black"

# Node transparency (in [0,1])
V(gtoy1)$nodeAlpha <- 1

## ----Edge attributes - 1, eval=TRUE, message=FALSE----------------------------
# Edge width (as in 'lwd' standard graphics; see 'help(gpar)')
E(gtoy1)$edgeLineWidth <- 0.8

# Edge color (Hexadecimal or color name)
E(gtoy1)$edgeLineColor <- c("red","green","blue","black")

# Edge type (as in 'lty' standard graphics; see 'help(gpar)')
E(gtoy1)$edgeLineType <- c("solid", "11", "dashed", "2124")

# Edge transparency (in [0,1])
E(gtoy1)$edgeAlpha <- 1

## ----Edge attributes - 2, eval=TRUE, message=FALSE----------------------------
# Arrowhead types in directed graphs
## Integer or character code:
## 0 = "---", 1 = "-->", -1 = "--|"
E(gtoy1)$arrowType <- 1

## ----Edge attributes - 3, eval=TRUE, message=FALSE----------------------------
# Arrowhead types in undirected graphs
## Integer or character code:
##  0 = "---"
##  1 = "-->",  2 = "<--",  3 = "<->",  4 = "|->",
## -1 = "--|", -2 = "|--", -3 = "|-|", -4 = "<-|", 
E(gtoy1)$arrowType <- 1
# Note: in undirected graphs, this attribute overrides 
# the edge's orientation in the edge list

## ----A shortcut for RGraphSpace, eval=TRUE, message=FALSE, out.width="80%"----
# Plot the updated 'gtoy1' using RGraphSpace
plotGraphSpace(gtoy1, add.labels = TRUE)

## ----Using geoms, eval=TRUE, message=FALSE, out.width="80%"-------------------
gs <- GraphSpace(gtoy1)
gs <- normalizeGraphSpace(gs)
ggplot(gs) + 
  geom_edgespace() +
  geom_nodespace() +
  theme_gspace_coords(is_norm = TRUE)

## ----label='Session information', eval=TRUE, echo=FALSE-----------------------
sessionInfo()

