Skip to content

Instantly share code, notes, and snippets.

@dmi3kno
Last active December 7, 2018 20:59
Show Gist options
  • Save dmi3kno/ad629551770a8160a79dd90f05df1a36 to your computer and use it in GitHub Desktop.
Save dmi3kno/ad629551770a8160a79dd90f05df1a36 to your computer and use it in GitHub Desktop.
# create geometry matrix
geom <- cbind(x=c(4,4,2,5,2,3,3,3,4,2),
y=c(6,2,3,4,5,2,4,7,8,0))
# create data frame with special topology list column
df <- tibble::tibble(name="John", age=32,
topo=list( # list column containing polygon(s) and inner/outer flag
list(poly = list(c(10, 5, 8, 9, 4, 2),
c(3, 6, 1, 7)),
type = c(1L, 0L)))
)
# create topogeo object which contains "shared" geometry matrix and data.frame with topologies
obj <- structure(list(geom=geom, data=df), class=c("topogeo", "list"))
plot.topogeo <- function(obj){
plot(1, type="n", xlab="", ylab="",
xlim=range(obj$geom[,1]), ylim=range(obj$geom[,2]),
bty="n")
add_poly <- function(topo, geom){
for (i in seq_along(topo$poly)){
polygon(geom[topo$poly[[i]],], col=topo$type[[i]])
}
}
invisible(lapply(obj$data$topo, add_poly, obj$geom))
}
plot(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment