knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

ggcart

The goal of ggcart is to include Puerto Rico, the Virgin Islands and Guam in the traditional Albers maps. Also, ggcart will make it easier to map line, point and other data in the Albers projection.

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("uncoast-unconf/ggcart")

Examples

Sample datasets include information from the National Inventory of Dams and the National Bridge Inventory.

library(ggcart)
## Pull in sample data for mapping
head(bridges) ## Data slice from National Bridge Inventory

The standard Albers conic projection isn't great for points in Alaska, Hawaii, Puerto Rico, Guam and the U.S. Virgin Islands. (AK and HI are taken care of with AlbersUSA. )

# Map the dams sample data.
library(ggplot2)
ggplot() + geom_sf(data = dams) + coord_sf()
# Map the bridges sample data.
library(ggplot2)
ggplot() + geom_sf(data = bridges) + coord_sf()

To convert your own set of point data with a latitude and longitude to an sf object, try the following:

# To convert your own point data for use with ggcart
bridges <- read_sf("data-raw/bridges.csv", ## Replace with your own csv file
                   options = c("X_POSSIBLE_NAMES=longitude",
                               "Y_POSSIBLE_NAMES=latitude"))

Demonstration on how the shift/rotate/scale functionality within the transport_sf() function works. For this example we will be shifting Puerto Rico by 1300km west and 900km north, then rotate 90 degrees, then scale to 2 times the size.

### Demonstration of how the shift functionality works -----------
pr_centroid <- sf::st_centroid(sf::st_geometry(ggcart:::puerto_rico))

# define shift dimension points for visual reference on map
pt1 <- pr_centroid
pt2 <- pr_centroid + 1000*c(-1300,0)
pt3 <- pr_centroid + 1000*c(-1300,900)
# Create into two lines (pt1-->pt2, pt2-->pt3) in sf object
pts <- sf::st_multilinestring(list(rbind(pt1[[1]][1:2], pt2[[1]][1:2]),
                                   rbind(pt2[[1]][1:2], pt3[[1]][1:2])))
pts <- sf::st_sfc(pts)
pts <- sf::st_set_crs(pts, sf::st_crs(pr_centroid))
# plot the shift/rotate/scale to Puerto Rico
ggplot()+
  geom_sf(data=ggcart:::lower48)+
  geom_sf(data=ggcart:::puerto_rico, color="red")+
  geom_sf(data=ggcart:::transport_sf(sf=ggcart:::puerto_rico,
                            scale=2,
                            shift = c(-1300,900)*1000,
                            rotate=pi/2), color="blue")+
  geom_sf(data=pts, color="purple")+ 
  annotate(geom="text",
           x=c(mean(c(pt1[[1]][1], pt2[[1]][1])),
               mean(c(pt2[[1]][1], pt3[[1]][1]))),
           y=c(mean(c(pt1[[1]][2], pt2[[1]][2])),
               mean(c(pt2[[1]][2], pt3[[1]][2]))),
           label=c("1300km West","900km North"),
           angle=c(0,90),
           color="purple",
           vjust=-.2)

Almost there!

geom_sf_albersusa() exists - but has some trouble...

(And is not documented, and it outputs some warning messages, and it ignores some territories...)

library(tigris)
library(rmapshaper)
library(sf)
library(dplyr)
library(ggplot2)
library(ggcart)


capture.output({map <- states(cb = TRUE) %>%
  st_as_sf(map) %>%
  ms_simplify(keep = .05) %>%
  st_transform(
    "+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"
  )
})
ggplot() + 
  ggcart:::geom_sf_albersusa(data = map, aes(geometry = st_geometry(map))) + 
  ggcart:::geom_sf_albersusa(data = bridges, aes(geometry = st_geometry(bridges)))


hlendway/albersextra documentation built on May 17, 2019, 4:21 a.m.