0

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)

enter image description here

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.

Dylan_Gomes
  • 2,066
  • 14
  • 29
  • Perhaps just importing <- 'WA', <- 'OR', <- 'CA, then unioning them will get you past the bad poly aspects. – Chris Oct 30 '22 at 01:16
  • This is a good idea. It looks like CA and WA each have issues by themselves that are causing some union issues with `raster::union()`. I'll try some other methods. – Dylan_Gomes Oct 30 '22 at 01:41
  • Running `list(list(x = states$long, y = states$lat)) |> polyclip::polysimplify()` returns a list without errors, c.f. examples given. – dimfalk Oct 30 '22 at 19:01

0 Answers0