knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
Convert simple features to a generic common form that is more general and can be used for a wide variety of data structures.
This is work in progress and at a very early stage. More to come.
library(sf) ## a MULTIPOLYGON layer nc = st_read(system.file("shape/nc.shp", package="sf"))
The common form is the entity tables, objects, paths, vertices and a link table to allow de-duplication of shared vertices. Currently this de-duplication is done on all coordinate fields, but for most applications it will usually be done only in X-Y.
library(scsf) nc = st_read(system.file("gpkg/nc.gpkg", package="sf")) (bmodel <- PATH(nc))
Prove that things work by round-tripping to the PATH model and onto the old fortify approach for ggplot2
.
inner_cascade <- function(x) { tabnames <- silicate:::join_ramp(x) tab <- x[[tabnames[1]]] for (ni in tabnames[-1L]) tab <- dplyr::inner_join(tab, x[[ni]]) tab } ## this just joins everything back together in one big fortify table library(dplyr) tab <- bmodel %>% inner_cascade() library(ggplot2) ggplot(tab) + aes(x = x_, y = y_, group = path) + geom_polygon(aes(fill = AREA)) + geom_path(lwd = 2, col = "black")
What about polygons with holes and lots of tiny complicated parts.
data("inlandwaters") iw <- PATH(inlandwaters) str(iw) tab <- iw %>% inner_cascade() library(ggplot2) ggplot(tab) + aes(x = x_, y = y_, group = path) + ggpolypath::geom_polypath(aes(fill = Province)) + geom_path(col = "black") ggplot(tab %>% filter(Province == "South Australia")) + aes(x = x_, y = y_, group = path) + ggpolypath::geom_polypath(fill = "dodgerblue") + geom_path(col = "black") + coord_fixed()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.