├── NAMESPACE ├── .Rbuildignore ├── LICENSE ├── .gitignore ├── data └── harris12.rda ├── README.md ├── man ├── hello.Rd ├── is_valid_harris.Rd ├── stratigraph.Rd └── harris.Rd ├── stratigraphr.Rproj ├── DESCRIPTION ├── vignettes └── harris.Rmd └── R └── graph.R /NAMESPACE: -------------------------------------------------------------------------------- 1 | exportPattern("^[[:alpha:]]+") 2 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2018 2 | COPYRIGHT HOLDER: Joe Roe 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | inst/doc 6 | -------------------------------------------------------------------------------- /data/harris12.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nevrome/stratigraphr/master/data/harris12.rda -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # stratigraphr 2 | 3 | Archaeological stratigraphy and chronological sequences: Harris matrices, DAGs, etc. 4 | 5 | ## Example 6 | 7 | See `vignettes/harris.Rmd` 8 | -------------------------------------------------------------------------------- /man/hello.Rd: -------------------------------------------------------------------------------- 1 | \name{hello} 2 | \alias{hello} 3 | \title{Hello, World!} 4 | \usage{ 5 | hello() 6 | } 7 | \description{ 8 | Prints 'Hello, world!'. 9 | } 10 | \examples{ 11 | hello() 12 | } 13 | -------------------------------------------------------------------------------- /stratigraphr.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | StripTrailingWhitespace: Yes 16 | 17 | BuildType: Package 18 | PackageUseDevtools: Yes 19 | PackageInstallArgs: --no-multiarch --with-keep.source 20 | -------------------------------------------------------------------------------- /man/is_valid_harris.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/graph.R 3 | \name{is_valid_harris} 4 | \alias{is_valid_harris} 5 | \title{Validate stratigraphic graphs} 6 | \usage{ 7 | is_valid_harris(stratigraph, warn = TRUE) 8 | } 9 | \arguments{ 10 | \item{stratigraph}{A \code{stratigraph} object (see \code{\link[=stratigraph]{stratigraph()}})} 11 | 12 | \item{warn}{Display warnings for invalid graphs (Default: \code{TRUE}).} 13 | } 14 | \value{ 15 | \code{TRUE} or \code{FALSE} 16 | } 17 | \description{ 18 | Checks whether a stratigraphic graph is a valid Harris matrix. 19 | } 20 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: stratigraphr 2 | Type: Package 3 | Title: Archaeological Stratigraphy and Chronological Sequences 4 | Version: 0.1.0 5 | Date: 2018-12-19 6 | Authors@R: person("Joe", "Roe", email = "jwg983@hum.ku.dk", 7 | role = c("aut", "cre")) 8 | Description: More about what it does (maybe more than one line) 9 | Use four spaces when indenting paragraphs within the Description. 10 | License: MIT + file LICENSE 11 | Encoding: UTF-8 12 | LazyData: true 13 | Suggests: knitr, 14 | rmarkdown 15 | VignetteBuilder: knitr 16 | Imports: dplyr, 17 | tibble, 18 | magrittr 19 | Roxygen: list(markdown = TRUE) 20 | RoxygenNote: 6.1.0 21 | -------------------------------------------------------------------------------- /man/stratigraph.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/graph.R 3 | \name{stratigraph} 4 | \alias{stratigraph} 5 | \title{Stratigraphic graphs} 6 | \usage{ 7 | stratigraph(data, context, relation, type = "harris") 8 | } 9 | \arguments{ 10 | \item{data}{\code{tibble} or \code{data.frame} of contexts and the stratigraphic 11 | relationships between them.} 12 | 13 | \item{context}{\code{character}. Name of the column containing context labels (nodes)} 14 | 15 | \item{relation}{\code{character}. Name of the column describing relations (edges) 16 | between contexts} 17 | 18 | \item{type}{Type of graph. Currently only "harris" is supported.} 19 | } 20 | \value{ 21 | A \code{stratigraph} object. 22 | } 23 | \description{ 24 | Stratigraphic graphs 25 | } 26 | -------------------------------------------------------------------------------- /vignettes/harris.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Harris Matrices" 3 | author: "Joe Roe" 4 | date: "`r Sys.Date()`" 5 | output: rmarkdown::html_vignette 6 | vignette: > 7 | %\VignetteIndexEntry{Harris Matrices} 8 | %\VignetteEngine{knitr::rmarkdown} 9 | %\VignetteEncoding{UTF-8} 10 | --- 11 | 12 | ```{r setup, include = FALSE} 13 | knitr::opts_chunk$set( 14 | collapse = TRUE, 15 | comment = "#>" 16 | ) 17 | ``` 18 | 19 | 20 | ```{r} 21 | library("tidyverse") 22 | library("tidygraph") 23 | library("ggraph") 24 | library("stratigraphr") 25 | 26 | # Example data after Harris 1979, Fig. 12 27 | data("harris12") 28 | h12_graph <- stratigraph(harris12, "context", "above", type = "harris") 29 | 30 | ggraph(h12_graph, layout = "sugiyama") + 31 | geom_edge_link() + 32 | geom_node_label(aes(label = context), label.r = unit(0, "mm")) + 33 | theme_graph() 34 | ``` 35 | 36 | -------------------------------------------------------------------------------- /man/harris.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/graph.R 3 | \name{harris} 4 | \alias{harris} 5 | \title{Harris Matrices} 6 | \usage{ 7 | harris(data, context, relation) 8 | } 9 | \arguments{ 10 | \item{data}{\code{tibble} or \code{data.frame} of contexts and the stratigraphic 11 | relationships between them.} 12 | 13 | \item{context}{\code{character}. Name of the column containing context labels (nodes)} 14 | 15 | \item{relation}{\code{character}. Name of the column describing relations (edges) 16 | between contexts} 17 | } 18 | \value{ 19 | A \code{tibble} of edges with \code{to} and \code{from} columns (see \code{\link[tidygraph:tbl_graph]{tidygraph::tbl_graph()}}) 20 | } 21 | \description{ 22 | Join stratigraphic units using the Harris matrix method (Harris 1979). 23 | } 24 | \examples{ 25 | data(harris12) 26 | matrix <- harris(harris12, "context", "above") 27 | plot(matrix) 28 | } 29 | \references{ 30 | \itemize{ 31 | \item Harris, E. C. 1979. \emph{Principles of archaeological stratigraphy}. London: 32 | Academic Press. 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /R/graph.R: -------------------------------------------------------------------------------- 1 | # Functions for constructing graphs from stratigraphic sequences 2 | 3 | #' Stratigraphic graphs 4 | #' 5 | #' @param data `tibble` or `data.frame` of contexts and the stratigraphic 6 | #' relationships between them. 7 | #' @param context `character`. Name of the column containing context labels (nodes) 8 | #' @param relation `character`. Name of the column describing relations (edges) 9 | #' between contexts 10 | #' @param type Type of graph. Currently only "harris" is supported. 11 | #' 12 | #' @return A `stratigraph` object. 13 | #' @export 14 | #' 15 | #' @examples 16 | stratigraph <- function(data, context, relation, type = "harris") { 17 | # Join nodes 18 | if (type == "harris") { 19 | edges <- harris(data, context, relation) 20 | } 21 | else { 22 | stop("Stratigraphic graphs other than Harris matrices not yet implemented!") 23 | } 24 | 25 | # Construct graph 26 | stratigraph <- tidygraph::tbl_graph(nodes = data, edges = edges, 27 | directed = TRUE) 28 | 29 | # Add stratigraph class 30 | # TODO: turn into an as.stratigraph function? 31 | class(stratigraph) %>% 32 | purrr::prepend("stratigraph") -> 33 | class(stratigraph) 34 | attr(stratigraph, "type") <- type 35 | 36 | # Check validity and return 37 | if (type == "harris") { 38 | if (is_valid_harris(stratigraph)) { 39 | return(stratigraph) 40 | } 41 | else { 42 | return(NULL) 43 | } 44 | } 45 | } 46 | 47 | #' Harris Matrices 48 | #' 49 | #' Join stratigraphic units using the Harris matrix method (Harris 1979). 50 | #' 51 | #' @param data `tibble` or `data.frame` of contexts and the stratigraphic 52 | #' relationships between them. 53 | #' @param context `character`. Name of the column containing context labels (nodes) 54 | #' @param relation `character`. Name of the column describing relations (edges) 55 | #' between contexts 56 | #' 57 | #' @return 58 | #' A `tibble` of edges with `to` and `from` columns (see [tidygraph::tbl_graph()]) 59 | #' 60 | #' @export 61 | #' 62 | #' @references 63 | #' * Harris, E. C. 1979. *Principles of archaeological stratigraphy*. London: 64 | #' Academic Press. 65 | #' 66 | #' @examples 67 | #' data(harris12) 68 | #' matrix <- harris(harris12, "context", "above") 69 | #' plot(matrix) 70 | harris <- function(data, context, relation) { 71 | to <- rep(data[[context]], times = map_int(data[[relation]], length)) 72 | from <- unlist(data[[relation]]) 73 | tibble::tibble(to, from) %>% 74 | drop_na() %>% 75 | return() 76 | } 77 | 78 | #' Validate stratigraphic graphs 79 | #' 80 | #' Checks whether a stratigraphic graph is a valid Harris matrix. 81 | #' 82 | #' @param stratigraph A `stratigraph` object (see [stratigraph()]) 83 | #' @param warn Display warnings for invalid graphs (Default: `TRUE`). 84 | #' 85 | #' @return `TRUE` or `FALSE` 86 | #' @export 87 | #' 88 | #' @examples 89 | is_valid_harris <- function(stratigraph, warn = TRUE) { 90 | if (!igraph::is.dag(stratigraph)) { 91 | if(warn) warning("Invalid Harris matrix: graph contains cycles") 92 | return(FALSE) 93 | } 94 | else { 95 | return(TRUE) 96 | } 97 | } --------------------------------------------------------------------------------