how to snip or crop or white-fill a rectangle tightly surrounding the outside of a polygon with ggplot2
Asked Answered
S

1

3

I'm just trying to white-fill the area outside of a simple polygon. For some reason, it's screwing up by drawing a weird stake through the center like it thinks its a vampire slayer or something.

I tried following this post but something's gone bananas. I would've thought this would be easier, but it's proving to be quite an irascible little demon.

How do I white-fill the area outside a projection-friendly polygon without screwing up the area inside the polygon? thanx

# reproducible example
library(rgeos)
library(maptools)

shpct.tf <- tempfile() ; td <- tempdir()

download.file( 
    "ftp://ftp2.census.gov/geo/pvs/tiger2010st/09_Connecticut/09/tl_2010_09_state10.zip" ,
    shpct.tf ,
    mode = 'wb'
)

shpct.uz <- unzip( shpct.tf , exdir = td )

# read in connecticut
ct.shp <- readShapePoly( shpct.uz[ grep( 'shp$' , shpct.uz ) ] )

# box outside of connecticut
ct.shp.env <- gEnvelope( ct.shp )

# difference between connecticut and its box
ct.shp.diff <- gDifference( ct.shp.env , ct.shp )

# prepare both shapes for ggplot2
f.ct.shp <- fortify( ct.shp )
outside <- fortify( ct.shp.diff )


library(ggplot2)

# create all layers + projections
plot <- ggplot(data = f.ct.shp, aes(x = long, y = lat))  #start with the base-plot 
layer1 <- geom_polygon(data=f.ct.shp, aes(x=long,y=lat), fill='black')
layer2 <- geom_polygon(data=outside, aes(x=long,y=lat), fill='white')
co <- coord_map( project = "merc" )

# this works
plot + layer1 

# this does not
plot + layer1 + layer2

# this also does not
plot + layer1 + layer2 + co

enter image description here

Sharie answered 14/10, 2014 at 7:16 Comment(2)
How about plot + layer2 + layer1? That seems to be working. When you draw a polygon in a polygon, I think you gotta draw the outer polygon first. This post may be helpful.Nad
@Nad thanks for the idea, but that doesn't solve what i'm trying to do. i've got a rectangle of points ct.shp.env that i'd like to map and layer2 needs to cover up the ones outside of the connecticut shape. plus even with your solution, there's still that weird shape on the left. :( anyway, shouldn't layer1 and layer2 be perfectly complementary? i don't understand why switching the order should matter?Sharie
S
4

ct.shp.diff consists of four polygons:

R> length(ct.shp.diff@polygons[[1]]@Polygons)
# 4

or

R> nlevels(outside$group) 
# 4

Therefore, you need a group aesthetic in layer2 (otherwise ggplot tries to plot a single polygon, which results in weird connections between the parts):

layer2 <- geom_polygon(data=outside, aes(x=long, y=lat, group=group), fill='white')
plot + layer1 + layer2 + co

plot

Surveyor answered 14/10, 2014 at 9:51 Comment(4)
thank you!!! this is obviously correct, and shame on me for missing that parameter in the other post that i cited. the example i provided only appears to work when the bounding box gets created with gEnvelope so i've posted a related question (with a similar problem) where i manually create the bounding box -- #26360409Sharie
manually creating an expanded grid makes this harder, huh? :(Sharie
The example in your new question creates a polygon with a hole. I'm not sure, but maybe this is problematic in coord_map. Can you reproject your data first (spTransform) and perform the rgeos operations afterwards?Surveyor
i tried that.. i might've been making a stupid mistake just like i made when asking this question, but i did give it a shot and failed. :(Sharie

© 2022 - 2024 — McMap. All rights reserved.