## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = FALSE
)

## ----harris12-data------------------------------------------------------------
library("stratigraphr")

data("harris12")
harris12

## ----harris12-graph-----------------------------------------------------------
h12_graph <- stratigraph(harris12, "context", "above")
h12_graph

## ----harris12-tidygraph-------------------------------------------------------
library("tidygraph")

# Inspect the nodes (units)
h12_graph |>
  activate("nodes") |>
  as_tibble()

# Inspect the edges (relations)
h12_graph |>
  activate("edges") |>
  as_tibble()

## ----from-scratch-------------------------------------------------------------
strat_data <- data.frame(
  unit = c("A", "B", "C"),
  below = c("B", "C", NA)
)

stratigraph(strat_data, "unit", "below", direction = "below")

## ----csv-workflow-------------------------------------------------------------
library("readr")

csv_text <- "context,above
A,
B,A
C,A"

strat_data <- read_csv(csv_text, show_col_types = FALSE)

stratigraph(strat_data, "context", "above")

## ----mirror-check-------------------------------------------------------------
strat_is_mirror(harris12$context, harris12$above, harris12$below)

## ----read-lst-----------------------------------------------------------------
lst_file <- system.file("extdata", "bonn.lst", package = "stratigraphr")
lst_data <- read_lst(lst_file)
stratigraph(lst_data, "name", "above")

## ----validity-check-----------------------------------------------------------
strg_is_valid(h12_graph)

## ----cycle-example------------------------------------------------------------
cycle_data <- data.frame(
  unit = c("A", "B", "C"),
  below = c("B", "C", "A")
)

cycle_graph <- stratigraph(cycle_data, "unit", "below", direction = "below")

## ----redundant-example--------------------------------------------------------
redundant_data <- data.frame(
  unit = c("A", "B", "B", "C"),
  above = c("B", "C", "C", NA)
)

redundant_graph <- stratigraph(redundant_data, "unit", "above")
strg_is_valid(redundant_graph)

## ----prune-example------------------------------------------------------------
pruned_graph <- strg_prune(redundant_graph)
pruned_graph
strg_is_valid(pruned_graph)

## ----ggraph-harris------------------------------------------------------------
library("ggraph")

ggraph(h12_graph, layout = "sugiyama") +
  geom_edge_elbow() +
  geom_node_label(aes(label = context), label.r = unit(0, "mm")) +
  theme_graph()

## ----ggraph-shub1, fig.height = 8---------------------------------------------
shub1_graph <- stratigraph(shub1, "context", "above")

ggraph(shub1_graph, layout = "sugiyama") +
  geom_edge_elbow() +
  geom_node_point(aes(shape = type), fill = "white", size = 6) +
  geom_node_text(aes(label = context), vjust = 0.5, size = 3) +
  scale_shape_manual(values = c(22, 21, 23, 24)) +
  theme_graph()

