I'm unable to simplify a shapefile in R
library(tmap) library(maptools) library(ggmap) England <- readOGR(dsn = "...") #works fine print(qtm(England, "LAD14CD", borders = NA, fill.title = "A-Level" )) # simplify the polygons England<-gSimplify(England,tol=0.01, topologyPreserve=TRUE) print(qtm(England, "LAD14CD", borders = NA, fill.title = "A-Level" ))
Gives an error of:
Error in process_fill(data, g$tm_fill, gborders, gt, gf, z = z + which(plot.order == : Fill argument neither colors nor valid variable name(s)
If you look at the UK dataobject you can see that it has changed from a Large Spatial polygonDataFrame to Large Spatial Polygons and dropped the @data
Instead, if you try to only simplify the polygons in the Shapefile:
It says that:
Error in gSimplify(England@polygons, tol = 0.01, topologyPreserve = TRUE) : cannot get a slot ("proj4string") from an object of type "list"
How can I simplify the polygons from a shapefile?
The return from
gSimplify is just the geometry, not the attributes, so you have to construct a new
SpatialPolygonsDataFrame with the simplified geometry and the attribute data from the original:
> England2 <-gSimplify(England,tol=0.01, topologyPreserve=TRUE) > England3 = SpatialPolygonsDataFrame(England2, data=England@data)
I think the polygons are guaranteed to be in the same order, unless anything has been simplified away. Check
length(England2) has the same number of rows as
England, or match up the rows on an ID.