I have some spatial objects that I want to combine, namely three polygons and a line. The ultimate goal is to create a polygon that I can overlay on an area of a map of administrative divisions of the UK to highlight a certain zone.

If it were a case of combining four polygons, I would have a number of options, but as one of the objects is a line I'm at a bit of a loss as to how to tackle it. I would have thought combining polygons and lines would be a common operation but it seems not.

Here's the map produced by my code (see below).


The red line starts and ends on the coastline or, strictly speaking, on the bank of a river leading to an estuary at the south end, as can be seen in the following images.

north end

south end

So, because there is an intersection I assume it is possible to group the elements together, I just don't know now. To summarise...


Code follows:


response <- GET(url="https://dl.dropbox.com/u/4115850/spobjects.rda")

class(dist.shape) # map with administrative divisions
class(terr.shape) # line defining the landward side of the zone
head(dist.df) # data frames created using fortify() function
head(terr.df) # ditto

ggplot(data = dist.df, aes(x = long, y = lat, group = group)) +
     geom_polygon(colour = "grey50", fill = "grey80", size = 0.5) +
     geom_path(data = terr.df, aes(x = long, y = lat, group = group),
               colour = "red", size = 0.5) +

gUnion in the rgeos library will merge polygon and line geometries.

  • Thanks for this pointer. rgeos was full of interesting things to try but ultimately I couldn't get R to generate a polygon composed of the line and subsets of the other polygons as described above. In the end I manually added the line in QGIS on a different layer, merged the layer, snipped pieces off it and exported it as a shapeful. – SlowLearner Mar 13 '13 at 21:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.