knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
DAG operations of ralget are now in the raldag
package.
Ralget creates and combines graphs with algebraic operations.
library(tidyverse) library(tidygraph) library(ggraph) library(patchwork) library(igraph) library(magrittr) library(ralget)
format <- theme(plot.title = element_text(hjust = 0.5)) + theme(panel.background = element_rect(fill = rgb(.95,.95,.95,1),color = NA)) plotter <- function(graph, layout_df,title ){ graph %>% ggraph(layout_df) + geom_edge_link( aes(end_cap = circle(10, "pt"), start_cap = circle(10, "pt")), edge_colour = "black", arrow = arrow( angle = 15, length = unit(0.15, "inches"), ends = "last", type = "closed" ) ) + geom_node_point(col = "grey66", size = 10, show.legend = FALSE) + geom_node_text(aes(label = name)) + theme_graph() + ggtitle(title) + format }
Take the following vertices:
p <- v("p") q <- v("q") s <- v("s") r <- v("r")
The +
operator places vertices in the same graph.
The *
operator joins one vertex to another.
g1 <- p * q + s g2 <- q * s + q* r g3 <- s * r
g1 <- p*q+s g2 <- q*s+q*r g3 <- s*r gg <- (g1+g2) p1 <- g1 %>% plotter(data.frame(x = c(1,2,2), y = c(2,2,1)),"g1 = p * q + s") + xlim(c(1,3)) p2 <- g2 %>% plotter(data.frame(x = c(2,2,3), y = c(2,1,2)),"g2 = q * s + q * r") + xlim(c(1,3)) p3 <- g3 %>% plotter(data.frame(x = c(2,3), y = c(1,2)),"g3 = s * r") + xlim(c(1,3)) p1 + p2 + p3
The +
operator overlays graphs.
gs <- (g1+g2)
g1 + g2
format <- theme(plot.title = element_text(hjust = 0.5)) + theme(panel.background = element_rect(fill = rgb(.95,.95,.95,1),color = NA)) p1 <- g1 %>% plotter(data.frame(x = c(1,2,2), y = c(2,2,1)),"g1") + xlim(c(1,3)) p2 <- g2 %>% plotter(data.frame(x = c(2,2,3), y = c(2,1,2)),"g2") + xlim(c(1,3)) p3 <- gs %>% plotter(data.frame(x = c(1,2,2,3), y = c(2,2,1,2)),"g1 + g2") + xlim(c(1,3)) p1 + p2 + p3
The *
operator creates a link from each vertex in the first graph to each vertex in the second graph.
gp <- (g1*g3)
g1 * g3
p1 <- g1 %>% plotter(data.frame(x = c(1,2,2), y = c(2,1.9,1)),"g1") + xlim(c(1,3)) p2 <- g3 %>% plotter(data.frame(x = c(2,3), y = c(1,2)),"g2") + xlim(c(1,3)) p3 <- gp %>% plotter(data.frame(x = c(1,2,2,3), y = c(2,1.90,1,2)),"g1 * g2") p1 + p2 + p3 + geom_edge_loop(end_cap = circle(10, "pt"))
The %x%
operator creates the graph product.
v0 <- v(name = "00") v1 <- v(name = "01") v2 <- v(name = "02") v3 <- v(name = "03") A <- v("A") B <- v("B") C <- v("C") D <- v("D") E <- v("E") x = A*B+B*C+C*D+D*E+E*A y = v0*v1+v1*v2+v2*v3
x %x% y
layout_df <-data.frame( x = c(-2.25625566239723, -2.06694404978908, -1.58402956895644, -1.55953811495272, -1.89737276852298), y = c(0.138709465825545, -1.1508396186631, -0.585121176745636, 0.66449533913645, 1.38325400507676)) p1 <- x %>% plotter(layout_df,"x") + xlim(c(-4,0)) layout_df <-data.frame( x = rev(c(1, 2, 3 ,4)), y = c(0, 0, 0,0)) p2 <- (y) %>% plotter(layout_df,"y") p3 <- (x %x% y) %>% plotter("stress", "x %x% y") p1+ p2 + p3
Vertices are created with the v()
function, which takes a name and list of attributes associated with the vertex.
This creates a ralget/tidygraph object:
v("x", Latitude= 78.26077, Longitude= -94.11077)
Edge attributes are added by interleaving e() between graph multiplication.
Here's a simple example:
v("X1") * e("E:X1.X2") * v("X2")
g <- v("X1") * e("E:X1.X2") * v("X2")
You can install the development version from GitHub with:
# install.packages("devtools") devtools::install_github("ianmoran11/ralget")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.