Last active
April 21, 2021 00:36
-
-
Save chris-prener/5a44573bd8d4b93dd6d0d063fd0a4d36 to your computer and use it in GitHub Desktop.
Locate Anchorage, AK using the albersusa package for R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# find where to locate Anchorage, AK using the albersusa package | |
# dependencies | |
library(albersusa) # composite projection | |
library(dplyr) # data wrangling | |
library(ggplot2) # mapping | |
library(mapview) # preview | |
library(sf) # spatial data | |
# load composite map | |
us <- usa_composite("longlat") | |
# convert to sf | |
us <- st_as_sf(us) | |
# preview | |
# the trick is to use the {mapview} package and hover over the object from {albersusa} | |
# so you can see the longitude and latitude of anchorage not where it is but in Baja California... | |
# it's in the upper left corner of the {mapview} window | |
mapview(us) | |
# create new sf object | |
anchorage <- tibble( | |
name = c("Anchorage"), | |
x = c(-111.49443), | |
y = c(25.84486) | |
) | |
anchorage <- st_as_sf(anchorage, coords = c("x", "y"), crs = 4326) | |
# convert to albers | |
albers <- "+proj=aea +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs" | |
us <- st_transform(us, crs = albers) | |
anchorage <- st_transform(anchorage, crs = albers) | |
# test map | |
ggplot() + | |
geom_sf(data = us) + | |
geom_sf(data = anchorage, color = "red", size = 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment