Code Monkey home page Code Monkey logo

albersusa's Introduction

Project Status: Active – The project has reached a stable, usable state and is being actively developed. Signed by Signed commit % Linux build Status Coverage Status Minimal R Version License

albersusa

Tools, Shapefiles & Data to Work with an ‘AlbersUSA’ Composite Projection

Description

Creating a composite projection for states and counties of the United States that includes scaled and shifted polygons for Alaska and Hawaii is time consuming and potentially error-prone. Functions and data sets are provided to make it easier to produce maps with a composite projection. Furthermore, named projections for common transormations are provided to further increase mapping productivity.

What’s Inside The Tin

The following functions are implemented:

  • counties_sf: Retrieve a U.S. county composite map, optionally with a projection, as a simplefeature
  • usa_sf: Retreive a U.S. state composite map, optionally with a projection, as a simplefeature

New ones:

  • points_elided: Shift points around Alaska and Hawaii to the elided area (by @rdinter)

Some legacy ones:

  • usa_composite: Retrieve a U.S. composite map, optionally with a projection
  • counties_composite: Retrieve a U.S. county composite map, optionally with a projection

Pre-canned projection strings:

  • us_aeqd_proj: Oblique azimuthal equidistant convenience projection
  • us_eqdc_proj: Equidistant conic convenience projection
  • us_laea_proj: Albers equal-area conic convenience projection
  • us_lcc_proj: Lambert conformal conic convenience projection
  • us_longlat_proj: Generic long/lat convenience projection

The following data sets are included:

  • system.file("extdata/composite_us_states.geojson.gz", package="albersusa")
  • system.file("extdata/composite_us_counties.geojson.gz", package="albersusa")

Also, the simplefeatures columns and @data slot of each SpatialPolygonsDataFrame has some handy data you can use (like FIPS codes and state/county population information).

Installation

remotes::install_git("https://git.rud.is/hrbrmstr/albersusa.git")
# or
remotes::install_git("https://git.sr.ht/~hrbrmstr/albersusa")
# or
remotes::install_gitlab("hrbrmstr/albersusa")
# or
remotes::install_bitbucket("hrbrmstr/albersusa")
# or
remotes::install_github("hrbrmstr/albersusa")

NOTE: To use the ‘remotes’ install options you will need to have the {remotes} package installed.

Usage

library(albersusa)
library(sf)
library(sp)
library(rgeos)
library(maptools)
library(ggplot2)
library(ggalt)
library(ggthemes)
library(viridis)
library(scales)

# current version
packageVersion("albersusa")
## [1] '0.4.0'

Simple features base

par(mar=c(0,0,1,0))

us_sf <- usa_sf("laea")
plot(us_sf["pop_2012"])

cty_sf <- counties_sf("aeqd")
plot(cty_sf["census_area"])

ggplot2

ggplot() +
  geom_sf(data = us_sf, size = 0.125)

ggplot() +
  geom_sf(data = us_sf, size = 0.125) +
  coord_sf(crs = us_longlat_proj)

ggplot() +
  geom_sf(data = cty_sf, size = 0.0725)

ggplot() +
  geom_sf(data = cty_sf, size = 0.0725) +
  coord_sf(crs = us_longlat_proj)

albersusa Metrics

Lang # Files (%) LoC (%) Blank lines (%) # Lines (%)
R 9 0.9 106 0.77 66 0.67 277 0.86
Rmd 1 0.1 31 0.23 32 0.33 46 0.14

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

albersusa's People

Contributors

hrbrmstr avatar rdinter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

albersusa's Issues

callr error on install

Downloading GitHub repo hrbrmstr/albersusa@master
from URL https://api.github.com/repos/hrbrmstr/albersusa/zipball/master
Installing albersusa
trying URL 'https://cran.rstudio.com/src/contrib/classInt_0.1-24.tar.gz'
Content type 'application/x-gzip' length 19194 bytes (18 KB)
==================================================
downloaded 18 KB

Installing classInt
trying URL 'https://cran.rstudio.com/src/contrib/e1071_1.6-8.tar.gz'
Content type 'application/x-gzip' length 581513 bytes (567 KB)
==================================================
downloaded 567 KB

Installing e1071
Installation failed: NULL : 'rcmd_safe_env' is not an exported object from 'namespace:callr'
Installation failed: NULL : 'rcmd_safe_env' is not an exported object from 'namespace:callr'
trying URL 'https://cran.rstudio.com/src/contrib/e1071_1.6-8.tar.gz'
Content type 'application/x-gzip' length 581513 bytes (567 KB)
==================================================
downloaded 567 KB

Skipping e1071, it is already being installed.
trying URL 'https://cran.rstudio.com/src/contrib/rgdal_1.2-15.tar.gz'
Content type 'application/x-gzip' length 1648779 bytes (1.6 MB)
==================================================
downloaded 1.6 MB

Installing rgdal
Installation failed: NULL : 'rcmd_safe_env' is not an exported object from 'namespace:callr'
trying URL 'https://cran.rstudio.com/src/contrib/rgeos_0.3-26.tar.gz'
Content type 'application/x-gzip' length 250574 bytes (244 KB)
==================================================
downloaded 244 KB

Installing rgeos
Installation failed: NULL : 'rcmd_safe_env' is not an exported object from 'namespace:callr'
trying URL 'https://cran.rstudio.com/src/contrib/sf_0.5-5.tar.gz'
Content type 'application/x-gzip' length 7734623 bytes (7.4 MB)
==================================================
downloaded 7.4 MB

Installing sf
trying URL 'https://cran.rstudio.com/src/contrib/classInt_0.1-24.tar.gz'
Content type 'application/x-gzip' length 19194 bytes (18 KB)
==================================================
downloaded 18 KB

Skipping classInt, it is already being installed.
trying URL 'https://cran.rstudio.com/src/contrib/e1071_1.6-8.tar.gz'
Content type 'application/x-gzip' length 581513 bytes (567 KB)
==================================================
downloaded 567 KB

Skipping e1071, it is already being installed.
Installation failed: NULL : 'rcmd_safe_env' is not an exported object from 'namespace:callr'
Installation failed: NULL : 'rcmd_safe_env' is not an exported object from 'namespace:callr'

Updating shapefile

There's some strange behavior with the default shapefiles in the package that appears to have occurred when the rds files (which have the .rda extension but are rds files...why is that?) were updated with ac3cdf3. It looks like both the county and state shapefiles are of fairly poor quality and produce poor graphics. Let me show the examples and then what I think is happening:

# Splotchy shapefile example

library(albersusa)
library(ggplot2)
library(maptools)
library(sp)

us      <- counties_composite("aeqd")
us_map  <- fortify(us)

ggplot(us_map, aes(long, lat, group = group)) +
  geom_polygon(fill = "blue")

shapefile_issue

(It might be a bit difficult to see, but if you zoom in slightly then the dead pixels become very clear)

This issue is further exacerbated if I try to take the original shapefile, aggregate up to a different level (say states) and create a geom_path:

states <- fortify(us, region = "state")

ggplot(us_map, aes(long, lat, group = group)) +
  geom_polygon(fill = "blue") +
  geom_path(data = states)

shapefile_path_issue

The same performance issues occur with the usa_composite() function for states:

us      <- usa_composite("aeqd")
us_map  <- fortify(us)

ggplot(us_map, aes(long, lat, group = group)) +
  geom_polygon(fill = "blue")

shapefile_issue_states

And if I attempt to create a path for a subset of states:

south_east <- c("AL", "FL", "GA", "NC", "SC")
us$region <- ifelse(us$iso_3166_2 %in% south_east, "SOUTHEAST", "REST")

states <- fortify(us, region = "region")

ggplot(us_map, aes(long, lat, group = group)) +
  geom_polygon(fill = "blue") +
  geom_path(data = states)

shapefile_path_issue_states

Issue

When I went through https://github.com/hrbrmstr/rd3albers to create the Alaska and Hawaii bounding boxes, I noticed that there were two types of formats used for the spatial objects in the repository: the classic .shp/.shx/.dbf shapefile which and also a GeoJSON file. What I am pretty sure is happening is that the current .rda files for counties/states are based off of the GeoJSON file, which has some funky resolution. I'd bet that the previous .rda files were based off of the .shp/.shx/.dbf shapefile, which doesn't appear to produce the funky path lines and splotches while filling in the map.

Are the .rda/.rds files based off of that rd3albers repository? If so, I can put in a pull request here for the objects which uses that repository's data. But further, is there a reason why the counties/states have the .rda extension but are actually .rds based files? That seems a bit confusing to me, but there might be a reason for it that I'm missing.

points_elided() maps don't work in rmarkdown document

I"m generating two maps, one using the usa_composite(), and another using a shapefile downloaded from elsewhere.

For each of them, I apply points_elided() and then use ggmap().

When I generate one off maps inside RStudio, I get the correct output for both shapes. However, when I knit the document, the standard use_composite() shape map looks right, but the other one comes out wonky.

image

Any suggestions about what could be happening with rmarkdown that causes these to behave differently?

Weed out data

Given:

## $ geo_id              <chr> "0400000US04", "0400000US05", "0400000US06", "0400000US08", "0400000US09", "0400000US11...
## $ fips_state          <chr> "04", "05", "06", "08", "09", "11", "13", "17", "18", "22", "27", "28", "30", "35", "38...
## $ name                <chr> "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "District of Columbia",...
## $ lsad                <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",...
## $ census_area         <dbl> 113594.084, 52035.477, 155779.220, 103641.888, 4842.355, 61.048, 57513.485, 55518.930, ...
## $ iso_3166_2          <chr> "AZ", "AR", "CA", "CO", "CT", "DC", "GA", "IL", "IN", "LA", "MN", "MS", "MT", "NM", "ND...
## $ census              <int> 6392017, 2915918, 37253956, 5029196, 3574097, 601723, 9687653, 12830632, 6483802, 45333...
## $ pop_estimataes_base <int> 6392310, 2915958, 37254503, 5029324, 3574096, 601767, 9688681, 12831587, 6484192, 45334...
## $ pop_2010            <int> 6411999, 2922297, 37336011, 5048575, 3579345, 605210, 9714464, 12840097, 6490308, 45455...
## $ pop_2011            <int> 6472867, 2938430, 37701901, 5119661, 3590537, 620427, 9813201, 12858725, 6516560, 45759...
## $ pop_2012            <int> 6556236, 2949300, 38062780, 5191709, 3594362, 635040, 9919000, 12873763, 6537632, 46047...
## $ pop_2013            <int> 6634997, 2958765, 38431393, 5272086, 3599341, 649111, 9994759, 12890552, 6570713, 46292...
## $ pop_2014            <int> 6731484, 2966369, 38802500, 5355866, 3596677, 658893, 10097343, 12880580, 6596855, 4649...

I think you should only save:

## $ geo_id              <chr> "0400000US04", "0400000US05", "0400000US06", "0400000US08", "0400000US09", "0400000US11...
## $ fips_state          <chr> "04", "05", "06", "08", "09", "11", "13", "17", "18", "22", "27", "28", "30", "35", "38...
## $ name                <chr> "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "District of Columbia",...
## $ census_area         <dbl> 113594.084, 52035.477, 155779.220, 103641.888, 4842.355, 61.048, 57513.485, 55518.930, ...
## $ iso_3166_2          <chr> "AZ", "AR", "CA", "CO", "CT", "DC", "GA", "IL", "IN", "LA", "MN", "MS", "MT", "NM", "ND...

Possibly standardising variables to an R package that provides population data

Points_elided not working with Tigris sp

One of the recent changes to points_elided may have made it stop working with the Tigris package.

library(tigris)
library(albersusa)
sp_states = states(cb = TRUE)
us.map = points_elided(sp_states )

Returns

Error in [.data.frame(x@data, i, j, ..., drop = FALSE) :
undefined columns selected

Error: "unable to find an inherited method..."

Following this blog post:

https://rud.is/b/2019/01/30/fast-static-maps-built-with-r/

Following this block of code:

hi <- select(filter(area_pct, state == "HI"), lon, lat)
hi <- points_elided(hi)
area_pct[area_pct$state == "HI", c("lon", "lat")] <- hi

I get the following error:

Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘proj4string’ for signature ‘"tbl_df"’

I am using the most recent development version of albersusa (‘0.3.1’), R version 3.5.2, all packages up-to-date.

points_elided() moves Alaska and Hawaii onto mexico

points_elided() moves Alaska and Hawaii on top of mexico, producing something like this:

image

Is there some way to make adjustments to where they end up? I don't see any arguments to points_elided() to do so.

counties_sf is out of date

The list of counties returned by counties_sf is now out-of-date.

counties_sf() %>%
  filter((name == "Bedford" & state == "Virginia") | (name == "Petersburg" & state == "Alaska"))
# Simple feature collection with 3 features and 8 fields
# geometry type:  MULTIPOLYGON
# dimension:      XY
# bbox:           xmin: -107.3392 ymin: 24.88892 xmax: -79.1833 ymax: 37.617
# epsg (SRID):    4326
# proj4string:    +proj=longlat +datum=WGS84 +no_defs
#    fips state_fips county_fips       name   lsad census_area    state
# 1 51019         51         019    Bedford County     753.021 Virginia
# 2 51515         51         515    Bedford   city       6.878 Virginia
# 3 02195         02         195 Petersburg     CA    3281.982   Alaska
#   iso_3166_2                       geometry
# 1         VA MULTIPOLYGON (((-79.62613 3...
# 2         VA MULTIPOLYGON (((-79.5436 37...
# 3         AK MULTIPOLYGON (((-107.1118 2...

See this note, Petersburg, AK needs to have its lsad updated, but Bedford, VA needs to be merged.

Unfortunately the fixes would make this incompatible with the last decennial census, so I'm not sure what the best move is. I guess a year argument to the function call would be the safest, but would require a lot of engineering effort.

README.md example doesn't work, with "Error in zero_range(from) : x must be length 1 or 2".

Library

library(albersusa)
library(sf)
library(sp)
library(rgeos)
library(maptools)
library(ggplot2)
library(ggalt)
library(ggthemes)
library(viridis)
library(scales)

# current verison
packageVersion("albersusa")

US States Map

us <- usa_composite()
us_map <- fortify(us, region="name")

Basemap

gg <- ggplot()
gg <- gg + geom_map(data=us_map, map=us_map,
                    aes(x=long, y=lat, map_id=id),
                    color="#2b2b2b", size=0.1, fill=NA)
gg <- gg + theme_map()

gg + coord_map()

Fill with Population

gg + 
  geom_map(data=us@data, map=us_map,
           aes(fill=pop_2014, map_id=name),
           color="white", size=0.1) +
  coord_proj(us_laea_proj) +
  scale_fill_viridis(name="2014 Populaton Estimates", labels=comma) +
  theme(legend.position="top", 
        legend.key.width=unit(3, "lines"))

Error in zero_range(from) : x must be length 1 or 2

 devtools::session_info('albersusa')
Session info ------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.0 (2016-05-03)
 system   x86_64, mingw32             
 ui       RStudio (1.1.423)           
 language (EN)                        
 collate  C                           
 tz       America/New_York            
 date     2018-05-09                  

Packages ----------------------------------------------------------------------------------------------------------------
 package   * version date       source                             
 albersusa * 0.3.0   2018-05-04 Github (hrbrmstr/albersusa@82220d3)
 class       7.3-14  2015-08-30 CRAN (R 3.3.0)                     
 classInt    0.2-3   2018-04-16 CRAN (R 3.3.3)                     
 DBI         1.0.0   2018-05-02 CRAN (R 3.3.0)                     
 e1071       1.6-8   2017-02-02 CRAN (R 3.3.3)                     
 foreign   * 0.8-66  2015-08-19 CRAN (R 3.3.0)                     
 graphics  * 3.3.0   2016-05-04 local                              
 grDevices * 3.3.0   2016-05-04 local                              
 grid        3.3.0   2016-05-04 local                              
 lattice   * 0.20-35 2017-03-25 CRAN (R 3.3.3)                     
 magrittr    1.5     2014-11-22 CRAN (R 3.3.3)                     
 maptools  * 0.9-2   2017-03-25 CRAN (R 3.3.3)                     
 MASS        7.3-45  2016-04-21 CRAN (R 3.3.0)                     
 methods   * 3.3.0   2016-05-04 local                              
 Rcpp        0.12.16 2018-03-13 CRAN (R 3.3.3)                     
 rgdal       1.2-18  2018-03-17 CRAN (R 3.3.3)                     
 rgeos     * 0.3-26  2017-10-31 CRAN (R 3.3.3)                     
 sf        * 0.6-1   2018-03-22 CRAN (R 3.3.3)                     
 sp        * 1.2-7   2018-01-19 CRAN (R 3.3.3)                     
 spData      0.2.8.3 2018-03-25 CRAN (R 3.3.3)                     
 stats     * 3.3.0   2016-05-04 local                              
 tools       3.3.0   2016-05-04 local                              
 udunits2    0.13    2016-11-17 CRAN (R 3.3.2)                     
 units       0.5-1   2018-01-08 CRAN (R 3.3.3)                     
 utils     * 3.3.0   2016-05-04 local

SpatialPointsDataFrame support?

Is there a method here that can plot a SpatialPointsDataFrame over the state or county map and have the Alaska and Hawaii points adjusted? Possibly a function to take all points within the bounding box of Alaska/Hawaii and adjust to the bounding box of the accompanying shapefiles?

Puerto Rico?

Thanks for these composite maps and tools, very useful!
I sometimes get requests for including Puerto Rico on Albers USA composite maps. I'm not sure if there's a more official way to represent this, maybe something like https://bl.ocks.org/mbostock/5629120? Could this be added to counties_sf and state_sf?

'albersusa' for R 3.4.1

I am trying to install 'albersusa' and I get message that it is not available for R version 3.4.1?

Warning in install.packages :
package ‘albersusa’ is not available (for R version 3.4.1)

Metro areas?

Is there a plan to include a US Metro Areas sf object?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.