Open jjchern opened 8 years ago
Are you open to a ggcounty.us_full()
function?
#' Returns a ggplot2 object with a geom_map of the lower 48 states and the non-contiguous states of Alaska and Hawaii
#'
#' @param fill color string (e.g. + default = "white")
#' @param border color string (e.g. + default = "#7f7f7f")
#' @param border line width (e.g. + default = 0.25)
#' @param fill alpha (e.g. + default = 1)
#' @return list consisting of the fortified map object (map), list of FIPS ids (fips) & the ggplot2 object (gg)
#' @export
#' @examples
#' g <- ggcounty.us_full()
ggcounty.us_full <- function(fill="white", color="#7f7f7f", size=0.25, alpha=1) {
# http://www.census.gov/geo/maps-data/data/tiger-cart-boundary.html
require(sp)
require(maptools)
require(ggplot2)
us.file <- system.file(package="ggcounty", "counties", "48.shp")
cty <- readShapePoly(us.file, repair=TRUE, IDvar="FIPS")
cty.f <- fortify(cty, region="FIPS")
# Add projections
albers_proj <- "+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"
original_proj <- "+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0"
# Add hawaii
hawaii.file <- system.file(package="ggcounty", "counties", sprintf("%s.shp", "hawaii"))
hawaii <- readShapePoly(hawaii.file, repair=TRUE, IDvar="FIPS")
proj4string(hawaii) <- original_proj
hawaii <- spTransform(hawaii, CRS(albers_proj))
hawaii <- elide(hawaii, rotate = -35)
hawaii <- elide(hawaii, shift = c(5400000, -1400000))
proj4string(hawaii) <- albers_proj
hawaii <- spTransform(hawaii, CRS(original_proj))
hawaii.f <- fortify(hawaii, region = "FIPS")
cty.f <- rbind(cty.f, hawaii.f)
# Add Alaska
alaska.file <- system.file(package="ggcounty", "counties", sprintf("%s.shp", "alaska"))
alaska <- readShapePoly(alaska.file, repair=TRUE, IDvar="FIPS")
proj4string(alaska) <- original_proj
alaska <- spTransform(alaska, CRS(albers_proj))
alaska <- elide(alaska, rotate = -50)
alaska <- elide(alaska, scale = max(apply(bbox(alaska), 1, diff)) / 2.3)
alaska <- elide(alaska, shift = c(-2100000, -2500000))
proj4string(alaska) <- albers_proj
alaska <- spTransform(alaska, CRS(original_proj))
alaska.f <- fortify(alaska, region = "FIPS")
cty.f <- rbind(cty.f, alaska.f)
# Add gg
gg <- ggplot()
gg <- gg + geom_map(data=cty.f, map = cty.f, aes(map_id=id, x=long, y=lat),
fill=fill, color=color, size=size, alpha=alpha)
gg <- gg + coord_map()
gg <- gg + labs(x="", y="")
gg <- gg + theme(plot.background = element_rect(fill = "transparent", colour = NA),
panel.border = element_blank(),
panel.background = element_rect(fill = "transparent", colour = NA),
panel.grid = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.position = "right")
return(list(map=cty.f, fips=unique(cty.f$id), gg=gg))
}
aye (sorry for the delay in responding…too may pkgs in maintenance mode :-)
the issue i have now is that should anyone's time be invested in this or in the already-in-CRAN tigris
package and/or choroplethr
package. but i can def either take a PR or add this by hand.
No problem. I can submit a PR.
The idea of ggcounty
strikes me as a very clean and useful way for thinking thematic maps.
For new R users who just want to plot a map, they don't need to know what a shapefile or SpatialPolygonsDataFrame
is, and they won't be confused by whether one should use ggplot2::fortify()
or broom::tidy()
, but they can still be blessed by having A Layered Grammar of Graphics with ggcounty
.
In fact, if you don't mind, I would like to borrow the idea of ggcounty
and work on ggpuma
, ggcommutingzone
, ggschooldistrict
, gghealthreferralregion
, and so on.