I am trying to get rid of intersections in state polygons from ggplot2
library("ggplot2")
states <- map_data("state")
states<-states[states$region=="washington"|states$region=="oregon"|states$region=="california",]
statesPoly<-Polygon(as.matrix(cbind(states$long,states$lat)),hole = F)
crdref <- CRS('+proj=longlat +datum=WGS84')
p1 <- SpatialPolygons(list(Polygons(list(statesPoly), "p1")),proj4string=crdref)
plot(p1)
See here (Trimming (difference) polygon by ggplot2 states polygons in R) as to why the self-intersections are an issue.
The polyclip::polysimplify
function appears to be designed for this issue (https://search.r-project.org/CRAN/refmans/polyclip/html/polysimplify.html), yet I cannot get the function to work with any of the following specifications:
library(polyclip)
polysimplify(list(cbind(states$long,states$lat)))
polysimplify(list(statesPoly))
polysimplify(list(p1))
Error in polysimplify(list(matrix(cbind(states$long, states$lat)))) :
Argument A should be a list of lists, each containing vectors x,y
If you wrap the arguments inside polysimplify
within another list()
(for example: polysimplify(list(list(p1)))
) such that "Argument A" is a "list of lists", this doesn't help.