[R-sig-Geo] Proper use of maptools::elide to both scale AND shift a polygon object
Richard Careaga
public at careaga.net
Thu Jul 23 08:07:42 CEST 2015
My goal is to produce a choropleth map in which farflung units, like
Hawaii, are brought into the bounding box for the continental US or
brought in and downscaled, like Alaska. I would also like to "outset"
the three smallest units within the US, the states of Rhode Island and
Delaware and the District of Columbia. I'm finding that I cannot BOTH
move and enlarge the same object.
Beginning with a shapefile, us, the following works
us_aea = spTransform(us, CRS("+proj=laea +lat_0=45 +lon_0=-100 +x_0=0
+y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"))
us_aea at data$id = rownames(us_aea at data)
alaska = us_aea[us_aea$STATEFP=="02",]
alaska = elide(alaska, rotate=-50)
alaska = elide(alaska, scale=max(apply(bbox(alaska), 1, diff)) / 2.3)
#alaska = elide(alaska, scale=max(apply(bbox(alaska), 1, diff)) / 5) #
makes smaller
#alaska = elide(alaska, scale=max(apply(bbox(alaska), 1, diff)) / 1) #
makes larger
alaska = elide(alaska, shift=c(-2100000, -2500000))
proj4string(alaska) = proj4string(us_aea)
hawaii = us_aea[us_aea$STATEFP=="15",]
hawaii = elide(hawaii, rotate=-35)
hawaii = elide(hawaii, shift=c(5400000, -1400000))
proj4string(hawaii) = proj4string(us_aea)
us_aea = us_aea[!us_aea$STATEFP %in% c("02", "15"),]
us_aea = rbind(us_aea, alaska, hawaii)
to properly place AK & HI
and
rhode_island = us_aea[us_aea$STATEFP=="44",]
rhode_island = elide(rhode_island, shift=c(0,-200000))
proj4string(rhode_island) = proj4string(us_aea)
delaware = us_aea[us_aea$STATEFP=="10",]
delaware = elide(delaware, shift=c(200000,0))
proj4string(delaware) = proj4string(us_aea)
dc = us_aea[us_aea$STATEFP=="11",]
#dc = elide(dc, scale=max(apply(bbox(dc), 1, diff)) / .999) # can't make
smaller than 1.0
#dc = elide(dc, scale = 2000) # makes it invisible
dc = elide(dc, shift=c(250000,-125000))
proj4string(dc) = proj4string(us_aea)
us_aea = us_aea[!us_aea$STATEFP %in% c("44", "10", "11"),]
us_aea = rbind(us_aea, delaware, rhode_island, dc)
us50 <- fortify(us_aea, region="STUSPS")
us50 = remove.territories(us50)
p = ggplot(data=us50) +
geom_map(map=us50, aes(x=long, y=lat, map_id=id, group=group),
,fill="white", color="dark grey", size=0.15) +
no_ylab +
no_xlab +
plain_theme
p
will plot everything, but when I uncomment either of the lines in the dc
block the SpatialPolygonsDataFrame representing the District of Columbia
vanishes from sight.
I can probably find a workaround by plotting DC in a separate layer
because I don't worry about its CRS since it is small, very simply
shaped, won't move far and the distortion is tolerable for this kind of
thematic map. I'd prefer, however, to have everything in a single object.
Is it simply that the shift argument "values to shift the coordinates of
the input object; this is made ineffective by the scale argument" means
what I'm trying to do is impossible? (Seems to work for the case of AK,
though.) Or is it not that subtle?
Thanks,
Richard Careaga
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list