1

I have SpatialPolygons grouped by 81 polygons and in each there are some other Polygons I'd like to plot them group by group but have little trouble with it.

> rtu_spl
class       : SpatialPolygons 
features    : 81 
extent      : 450685, 458760, 358519, 364860  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs 
> plot(rtu_spl)
Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'

I can't plot them all so I'd like to view group by group but how to do it?

What's working is viewing like this, for example:

plot(rtu_spl@polygons[[1]]@Polygons[[42]]@coords)

but this one not:

> plot(rtu_spl@polygons[[1]]@Polygons)
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' is a list, but does not have components 'x' and 'y'

How to skip any kind of loop and plot a whole group? More, even if I extract from loop like:

pls = NULL
for (i in 1:81) {
  pls = rbind(pls, rtu_spl@polygons[[1]]@Polygons[i])
}

I still can't plot it:

> plot(pls)
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  (list) object cannot be coerced to type 'double'

or

> spols = sp::SpatialPolygons(pls)
Error in sp::SpatialPolygons(pls) : 
  no slot of name "Polygons" for this object of class "Polygon"

Please give me a hint how to plot a group separately.

  • Could you supply your data or example data representative of your data's structure? The plot method should just work, unless your data is malformed or you forgot to load sp; I've found in general sp S4 methods are solid and I rarely if ever need to delve into the S4 structure manually--when I find myself doing so it's a clue to me that I'm doing something wrong. Make sure you've invoked library(sp) to ensure the necessary methods are in your namespace. – 0mn1 Jun 21 at 17:13
  • Why are you digging so deep into the objects, if you are just after the polygons just index the rows eg.; x[1:10,] or an index query x[x$y == "something",] – Jeffrey Evans Jun 21 at 18:04
  • For me this was solved by loading the 'sp' package. – AleRuete Jul 9 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.