this question is a follow-up of my prior SO question and is related to this question.
i'm just trying to white-fill an area 10% bigger than a simple polygon with ggplot2. maybe i'm grouping things wrong? here's a photo of the spike with reproducible code below
# reproducible example
library(rgeos)
library(maptools)
library(raster)
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 )
ct.shp.out <- as( 1.2 * extent( ct.shp ), "SpatialPolygons" )
# difference between connecticut and its box
ct.shp.env.diff <- gDifference( ct.shp.env , ct.shp )
ct.shp.out.diff <- gDifference( ct.shp.out , ct.shp )
library(ggplot2)
# prepare both shapes for ggplot2
f.ct.shp <- fortify( ct.shp )
env <- fortify( ct.shp.env.diff )
outside <- fortify( ct.shp.out.diff )
# 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=env, aes(x=long,y=lat,group=group), fill='white')
layer3 <- geom_polygon(data=outside, aes(x=long,y=lat,group=id), fill='white')
co <- coord_map( project = "albers" , lat0 = 40.9836 , lat1 = 42.05014 )
# this works
plot + layer1
# this works
plot + layer2
# this works
plot + layer1 + layer2
# this works
plot + layer2 + co
# this works
plot + layer1 + layer3
# here's the problem: this breaks
plot + layer3 + co
# this also breaks, but it's ultimately how i want to display things
plot + layer1 + layer3 + co
# this looks okay in this example but
# does not work for what i'm trying to do-
# cover up points outside of the state
plot + layer3 + layer1 + co
coord_map
function: "This is still experimental, and if you have any advice to offer regarding a better (or more correct) way to do this, please let me know". My bet is that you found a bug in this "experimental" function – Omophagiaggplot2
solution? Assuming I understand your intentions, this stuff is fairly straightforward with base plotting. – Evonevonnecoord_map
in particular, I stopped digging, since ggplot is so frigging difficult (for me at least) to debug! With a base or lattice plotting function, I'd have at least given it a shot. – Blastomereggplot2
because other components of the full code i'm trying to perfect rely on geom_tile(). that said, i would certainly appreciate a non-ggplot2 solution if you think it's straightforward – Nyhagen1.1 * extent(x)
is not the same as adding 10% to each side, unless both axes span the equator. In the present case,1.1 * xmax
and1.1 * ymax
actually subtract 10% from those bounds (sincexmax
andymax
are negative). – Evonevonne*
when applied to anExtent
class object, and it's smarter than that. Tryextent(10,20,10,20) * 1.2
to see that it does in fact add to each dimension. (Do notice that you have to multiply by1.2
to get a 10% extension in each direction.) If you want to have a look at the code underlyingExtent
's*
method, you can get it by typinggetMethod("Arith", c("Extent", "numeric"))
. – Blastomere