Giter VIP home page Giter VIP logo

usmap's Introduction

πŸ—Ί usmap

CRAN Downloads check codecov

Purpose

Typically in R it is difficult to create nice US choropleths that include Alaska and Hawaii. The functions presented here attempt to elegantly solve this problem by manually moving these states to a new location and providing a simple features (sf) object for mapping and visualization. This allows the user to easily add spatial data or features to the US map.

Shape Files

The shape files that we use to plot the maps in R are located in the usmapdata package. These are generated from the US Census Bureau cartographic boundary files. Maps at both the state and county levels are included for convenience.

Update History

Date usmap version Shape File Year Link
January 20, 2024 0.7.0 2022 πŸ”—
February 27, 2022 0.6.0 2020 πŸ”—
June 3, 2018 0.3.0 2017 πŸ”—
January 29, 2017 0.1.0 2015 πŸ”—

Installation

πŸ“¦ To install from CRAN (recommended), run the following code in an R console:

install.packages("usmap")

Developer Build

⚠️ The developer build may be unstable and not function correctly, use with caution.

To install the package from this repository, run the following code in an R console:

install.package("devtools")
devtools::install_github("pdil/usmap")

This method will provide the most recent developer build of usmap.

To begin using usmap, import the package using the library command:

library(usmap)

Documentation

To read the package vignettes, which explain helpful uses of the package, use vignette:

vignette(package = "usmap")
vignette("usmap1", package = "usmap") # 1. Introduction
vignette("usmap2", package = "usmap") # 2. Mapping the US
vignette("usmap3", package = "usmap") # 3. Advanced Mapping

For further help with this package, open an issue or ask a question on Stack Overflow with the usmap tag.

Features

Map Plots

  • Plot US maps
states <- plot_usmap("states")
counties <- plot_usmap("counties")

cowplot::plot_grid(states, counties, nrow = 1)

  • Display only certain states, counties, or regions
library(ggplot2)

mt <- plot_usmap("states", include = .mountain, labels = TRUE)

fl <- plot_usmap("counties", data = countypov, values = "pct_pov_2021", include = "FL") +
  scale_fill_continuous(low = "green", high = "red", guide = "none")

ne <- plot_usmap("counties", data = countypop, values = "pop_2022", include = .new_england) +
  scale_fill_continuous(low = "blue", high = "yellow", guide = "none")

cowplot::plot_grid(mt, fl, ne, nrow = 1)

  • Transform and add spatial data to map
library(ggplot2)

# Transform included `usrivers` data set
rivers_transformed <- usmap_transform(usrivers)

river_map <- plot_usmap("counties", color = "gray80") +
  geom_sf(data = rivers_transformed, aes(linewidth = Shape_Length), color = "blue") +
  scale_linewidth_continuous(range = c(0.3, 1.5), guide = "none")

# Transform included `earthquakes` data set
eq_transformed <- usmap_transform(earthquakes)

earthquake_map <- plot_usmap() +
  geom_sf(data = eq_transformed, aes(size = mag), color = "red", alpha = 0.25) +
  scale_size_continuous(guide = "none")

cowplot::plot_grid(river_map, earthquake_map, nrow = 1)

Map Data

  • Obtain map data with certain region breakdown
us_map(regions = "states")
#> Simple feature collection with 51 features and 3 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -2590847 ymin: -2608151 xmax: 2523583 ymax: 731405.7
#> Projected CRS: NAD27 / US National Atlas Equal Area
#> # A tibble: 51 Γ— 4
#>    fips  abbr  full                                                         geom
#>    <chr> <chr> <chr>                                          <MULTIPOLYGON [m]>
#>  1 02    AK    Alaska               (((-2396840 -2547726, -2393291 -2546396, -2…
#>  2 01    AL    Alabama              (((1093779 -1378539, 1093270 -1374227, 1092…
#>  3 05    AR    Arkansas             (((483066 -927786.9, 506063 -926262.2, 5315…
#>  4 04    AZ    Arizona              (((-1388677 -1254586, -1389182 -1251858, -1…
#>  5 06    CA    California           (((-1719948 -1090032, -1709613 -1090025, -1…
#>  6 08    CO    Colorado             (((-789537.1 -678772.6, -789536.6 -678768.3…
#>  7 09    CT    Connecticut          (((2161728 -83727.16, 2177177 -65210.71, 21…
#>  8 11    DC    District of Columbia (((1955475 -402047.2, 1960230 -393564, 1964…
#>  9 10    DE    Delaware             (((2042501 -284358, 2043073 -279990.9, 2044…
#> 10 12    FL    Florida              (((1855614 -2064805, 1860160 -2054368, 1867…
#> # β„Ή 41 more rows
us_map(regions = "counties")
#> Simple feature collection with 3144 features and 4 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -2590847 ymin: -2608151 xmax: 2523583 ymax: 731405.7
#> Projected CRS: NAD27 / US National Atlas Equal Area
#> # A tibble: 3,144 Γ— 5
#>    fips  abbr  full   county                                                geom
#>    <chr> <chr> <chr>  <chr>                                   <MULTIPOLYGON [m]>
#>  1 02013 AK    Alaska Aleutians East Borough       (((-1762717 -2477334, -17612…
#>  2 02016 AK    Alaska Aleutians West Census Area   (((-2396840 -2547726, -23932…
#>  3 02020 AK    Alaska Anchorage Municipality       (((-1517576 -2089907, -15176…
#>  4 02050 AK    Alaska Bethel Census Area           (((-1905137 -2137044, -19008…
#>  5 02060 AK    Alaska Bristol Bay Borough          (((-1685824 -2253496, -16840…
#>  6 02063 AK    Alaska Chugach Census Area          (((-1476668 -2101298, -14698…
#>  7 02066 AK    Alaska Copper River Census Area     (((-1457012 -2063407, -14434…
#>  8 02068 AK    Alaska Denali Borough               (((-1585790 -1980739, -15864…
#>  9 02070 AK    Alaska Dillingham Census Area       (((-1793023 -2236834, -17884…
#> 10 02090 AK    Alaska Fairbanks North Star Borough (((-1512362 -1851013, -14878…
#> # β„Ή 3,134 more rows

FIPS Codes

  • Look up FIPS codes for states and counties
fips("New Jersey")
#> [1] "34"

fips(c("AZ", "CA", "New Hampshire"))
#> [1] "04" "06" "33"

fips("NJ", county = "Mercer")
#> [1] "34021"

fips("NJ", county = c("Bergen", "Hudson", "Mercer"))
#> [1] "34003" "34017" "34021"
  • Retrieve states or counties with FIPS codes
fips_info(c("34", "35"))
#>   abbr fips       full
#> 1   NJ   34 New Jersey
#> 2   NM   35 New Mexico

fips_info(c("34021", "35021"))
#>         full abbr         county  fips
#> 1 New Jersey   NJ  Mercer County 34021
#> 2 New Mexico   NM Harding County 35021
  • Add FIPS codes to data frame
library(dplyr)

data <- data.frame(
  state = c("NJ", "NJ", "NJ", "PA"),
  county = c("Bergen", "Hudson", "Mercer", "Allegheny")
)

data %>% rowwise %>% mutate(fips = fips(state, county))
#> # A tibble: 4 Γ— 3
#> # Rowwise: 
#>   state county    fips 
#>   <chr> <chr>     <chr>
#> 1 NJ    Bergen    34003
#> 2 NJ    Hudson    34017
#> 3 NJ    Mercer    34021
#> 4 PA    Allegheny 42003

Additional Information

Citation Information

The images generated by usmap are not under any copyright restrictions and may be used and distributed freely in any publication or otherwise.

The underlying shapefiles used to generate the map data are derived from the US Census Bureau’s TIGER/Line Shapefiles which are not copyrighted but do suggest citation. See section 1.2 of this document.

If you wish to cite usmap in a publication (appreciated but never required!), you may do so in the following way:

citation("usmap")
#> To cite package 'usmap' in publications use:
#> 
#>   Di Lorenzo P (2024). _usmap: US Maps Including Alaska and Hawaii_. R
#>   package version 0.7.0, <https://usmap.dev>.
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Manual{,
#>     title = {usmap: US Maps Including Alaska and Hawaii},
#>     author = {Paolo {Di Lorenzo}},
#>     year = {2024},
#>     note = {R package version 0.7.0},
#>     url = {https://usmap.dev},
#>   }

Coordinate System

usmap uses the US National Atlas Equal Area coordinate system:

sf::st_crs(9311)
  #> Coordinate Reference System:
  #>   User input: EPSG:9311 
  #>   wkt:
  #> PROJCRS["NAD27 / US National Atlas Equal Area",
  #>     BASEGEOGCRS["NAD27",
  #>         DATUM["North American Datum 1927",
  #>             ELLIPSOID["Clarke 1866",6378206.4,294.978698213898,
  #>                 LENGTHUNIT["metre",1]]],
  #>         PRIMEM["Greenwich",0,
  #>             ANGLEUNIT["degree",0.0174532925199433]],
  #>         ID["EPSG",4267]],
  #>     CONVERSION["US National Atlas Equal Area",
  #>         METHOD["Lambert Azimuthal Equal Area (Spherical)",
  #>             ID["EPSG",1027]],
  #>         PARAMETER["Latitude of natural origin",45,
  #>             ANGLEUNIT["degree",0.0174532925199433],
  #>             ID["EPSG",8801]],
  #>         PARAMETER["Longitude of natural origin",-100,
  #>             ANGLEUNIT["degree",0.0174532925199433],
  #>             ID["EPSG",8802]],
  #>         PARAMETER["False easting",0,
  #>             LENGTHUNIT["metre",1],
  #>             ID["EPSG",8806]],
  #>         PARAMETER["False northing",0,
  #>             LENGTHUNIT["metre",1],
  #>             ID["EPSG",8807]]],
  #>     CS[Cartesian,2],
  #>         AXIS["easting (X)",east,
  #>             ORDER[1],
  #>             LENGTHUNIT["metre",1]],
  #>         AXIS["northing (Y)",north,
  #>             ORDER[2],
  #>             LENGTHUNIT["metre",1]],
  #>     USAGE[
  #>         SCOPE["Statistical analysis."],
  #>         AREA["United States (USA) - onshore and offshore."],
  #>         BBOX[15.56,167.65,74.71,-65.69]],
  #>     ID["EPSG",9311]]

This coordinate reference system (CRS) can also be obtained with usmap::usmap_crs().

Acknowledgments

The code used to generate the map files was based on this blog post by Bob Rudis: Moving The Earth (well, Alaska & Hawaii) With R

usmap's People

Contributors

pdil avatar usmap-bot 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

Watchers

 avatar  avatar  avatar  avatar  avatar

usmap's Issues

Missing County

It seems that usmap is missing Shannon County, SD.

Running this...

usmap::fips(state= "SD", county = "Shannon")

...yields this error

Error in fips(state = "SD", county = "Shannon") : 
  Shannon is not a valid county in SD.

Expected behavior
Meanwhile, it works just fine for others:

usmap::fips(state= "SD", county = "Meade")
[1] "46093"

Environment:

  • Platform: Linux
  • version output:
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          1.3                         
year           2022                        
month          03                          
day            10                          
svn rev        81868                       
language       R                           
version.string R version 4.1.3 (2022-03-10)
nickname       One Push-Up  
  • sessionInfo() output:
R version 4.1.3 (2022-03-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Rocky Linux 8.6 (Green Obsidian)

Matrix products: default
BLAS/LAPACK: /usr/lib64/libopenblasp-r0.3.15.so

locale:
[1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
[4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
[7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] googlesheets4_1.0.0 ranger_0.13.1       showtext_0.9-5      showtextdb_3.0     
[5] sysfonts_0.8.8      doParallel_1.0.17   iterators_1.0.14    vip_0.3.2          
[9] usemodels_0.2.0     lubridate_1.8.0     foreach_1.5.2       yardstick_1.0.0    
[13] workflowsets_0.2.1  workflows_0.2.6     tune_0.2.0          rsample_0.1.1      
[17] recipes_0.2.0       parsnip_0.2.1       modeldata_0.1.1     infer_1.0.0        
[21] dials_0.1.1         scales_1.2.0        tidymodels_0.2.0    RSocrata_1.7.11-2  
[25] usmap_0.6.0         magrittr_2.0.3      broom_0.8.0         MASS_7.3-57        
[29] patchwork_1.1.1     arrow_8.0.0         forcats_0.5.1       stringr_1.4.0      
[33] dplyr_1.0.9         purrr_0.3.4         readr_2.1.2         tidyr_1.2.0        
[37] tibble_3.1.7        ggplot2_3.3.6       tidyverse_1.3.1    

loaded via a namespace (and not attached):
[1] googledrive_2.0.0  colorspace_2.0-3   ellipsis_0.3.2     class_7.3-20      
[5] readtext_0.81      fs_1.5.2           rstudioapi_0.13    farver_2.1.0      
[9] listenv_0.8.0      furrr_0.3.0        bit64_4.0.5        prodlim_2019.11.13
[13] fansi_1.0.3        xml2_1.3.3         codetools_0.2-18   splines_4.1.3     
[17] cachem_1.0.6       jsonlite_1.8.0     dbplyr_2.2.0       compiler_4.1.3    
[21] httr_1.4.3         backports_1.4.1    fastmap_1.1.0      assertthat_0.2.1  
[25] Matrix_1.4-1       gargle_1.2.0       cli_3.3.0          tools_4.1.3       
[29] gtable_0.3.0       glue_1.6.2         rappdirs_0.3.3     Rcpp_1.0.8.3      
[33] cellranger_1.1.0   DiceDesign_1.9     vctrs_0.4.1        conflicted_1.1.0  
[37] timeDate_3043.102  gower_1.0.0        globals_0.15.0     rvest_1.0.2       
[41] mime_0.12          lifecycle_1.0.1    future_1.26.1      ipred_0.9-13      
[45] vroom_1.5.7        hms_1.1.1          rematch2_2.1.2     curl_4.3.2        
[49] memoise_2.0.1      gridExtra_2.3      rpart_4.1.16       stringi_1.7.6     
[53] lhs_1.1.5          hardhat_1.0.0      lava_1.6.10        rlang_1.0.2       
[57] pkgconfig_2.0.3    lattice_0.20-45    labeling_0.4.2     bit_4.0.4         
[61] tidyselect_1.1.2   usmapdata_0.1.0    parallelly_1.32.0  plyr_1.8.7        
[65] R6_2.5.1           generics_0.1.2     DBI_1.1.2          pillar_1.7.0      
[69] haven_2.5.0        withr_2.5.0        survival_3.3-1     nnet_7.3-17       
[73] future.apply_1.9.0 modelr_0.1.8       crayon_1.5.1       utf8_1.2.2        
[77] tzdb_0.3.0         grid_4.1.3         readxl_1.4.0       data.table_1.14.2 
[81] reprex_2.0.1       digest_0.6.29      openssl_2.0.2      GPfit_1.0-8       
[85] munsell_0.5.0      viridisLite_0.4.0  askpass_1.1  

Add ability to display state labels

There should be an option in plot_usmap to display state abbreviation labels (AK, AL, AR, etc.) in the center of each state polygon.

This may look something like this:

plot_usmap(data, ..., labels = true)

labels would be the new parameter and accept true or false values (default being false) indicating whether or not to display labels.

Another new option could be the ability to set label color.

Sent with GitHawk

Extract US map data to separate package

Currently the data frame which contains coordinates to draw the US state and county boundaries is stored within the usmap package. This data should be extracted into a separate package that can be updated independently of usmap. This will greatly reduce the file size of usmap and will make it easier to develop for with the smaller footprint.

It will also prevent the following NOTE that currently appears during R CMD check (notice extdata size compared to installed size):

> checking installed package size ... NOTE
    installed size is  8.4Mb
    sub-directories of 1Mb or more:
      doc       1.6Mb
      extdata   6.4Mb

To do

  • determine name for new package (usmap_data, usmapd, usmapdata, ?) (#39 (comment))
  • create repository for new package (#39 (comment))
  • extract data and tests related to the map data frame (i.e. the us_map function) (#39 (comment))
  • extract data generation scripts (located in data-raw folder) (#39 (comment))
  • submit new package to CRAN (#39 (comment))
  • once uploaded and functional, update usmap to make use of new package instead of internal data (#49)

Extra

  • consider creating more robust script to download shape files from census.gov and automatically create map data frames This would require automated updating of the county-fips or state-fips files and currently there is no easily parseable document providing this information.

Add comprehensive vignettes/documentation

usmap has grown substantially in feature set since it was first released. The vignettes should be updated and added to such that all features are clearly explained and made accessible for new users.

Sent with GitHawk

Look into including US territories in map

US territories are frequently excluded from US maps. In the spirit of this package, which is to include often forgotten Alaska & Hawaii, it may be a good idea to allow the option to present the inhabited US territories on the map as well.

The territories being considered are:

  • American Samoa
  • Guam
  • Northern Mariana Islands
  • Puerto Rico
  • US Virgin Islands

These territories all have FIPS codes and official abbreviations so incorporating them into the data set should be seamless.

The difficult part will be moving the coordinates of these places so that they appear on the map in a suitable and aesthetically adequate part of the US map.

Update GitHub Actions workflows

Update r-lib based workflows to v2 versions.

These include setup-r and setup-r-dependencies used in test-coverage.yaml and R-CMD-check.yaml.

Add contributing guidelines and templates

The following documents need to be added to provide a way for new contributors to understand how to contribute and what is expected of them:

  • Code of conduct
  • Contributing guidelines
  • Issue templates
  • Pull request template

This is also to make the repository more complete. As part of this effort, the Wiki should be revamped as well.

plot_usmap gets confused by a column named "values"

The bug

plot_usmap is confused when a column is named "values"

Assume a data.frame as follows...

str(myDF)
$ fips : Factor w/ 30 levels ...
$ values : int ...

plot_usmap(data = myDF)
Error in [.data.frame(map_df, , values) : undefined columns selected

this also fails...

plot_usmap(data = myDF, values = "values" )
Error in [.data.frame(map_df, , values) : undefined columns selected

BUT...this works...

str(myDF)
$ fips : Factor w/ 30 levels ...
$ population : int ...

plot_usmap(data = myDF, values = "population" )

which produces the expected plot

Plotting a merged data frame

Describe the bug
This used to work in version 0.2.1, now an error is returned.

To Reproduce
Steps to reproduce the behavior:

library(usmap)
library(dplyr)
data(USArrests)
USArrests$full <- rownames(USArrests)
USA50 <- inner_join(us_map(), USArrests)
plot_usmap(data=USA50, values="Murder")
Error in [.data.frame(result, , c(setdiff(names(result), names(data)), :
undefined columns selected

Expected behavior
A map appearing in the plot window

Screenshots
If applicable, add screenshots to help explain your problem.

Smartphone (please complete the following information):

  • R version: [4.0.0]
  • usmap version: [0.5.0]
  • Platform: [Windows]

Additional context
Add any other context about the problem here.

Add function to transform data frame to coordinates matching map

The function would take a data frame as input that contains two columns, the first representing longitude and the second representing latitude (column names don't matter).

The output will be a data frame containing the same data frame/coordinates except with the canonical usmap projection applied to it:

CRS("+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs")

Function name: usmap_proj

Related issue: #13

Resolve R CMD Check issues in 0.6.0

Version 0.6.0 is currently failing several tests on certain platforms: https://cran.r-project.org/web/checks/check_results_usmap.html

Example:

Version: 0.6.0 
Check: tests 
Result: ERROR 
     Running 'testthat.R' [10s/14s]
    Running the tests in 'tests/testthat.R' failed.
    Complete output:
     > library(testthat)
     > library(usmap)
     > 
     > test_check("usmap")
     [ FAIL 2 | WARN 3 | SKIP 0 | PASS 163 ]
     
     == Failed tests ================================================================
     -- Failure ('test-plot.R:138'): singular regions can be used -------------------
     plot_usmap(regions = "states")$layers not equal to plot_usmap(regions = "state")$layers.
     Component 1: Component 12: Component 5: Component 21: Component 11: 1 string mismatch
     Component 1: Component 12: Component 5: Component 21: Component 12: 1 string mismatch
     Component 2: Component 12: Component 5: Component 21: Component 11: 1 string mismatch
     Component 2: Component 12: Component 5: Component 21: Component 12: 1 string mismatch
     -- Failure ('test-plot.R:140'): singular regions can be used -------------------
     plot_usmap(regions = "counties")$layers not equal to plot_usmap(regions = "county")$layers.
     Component 1: Component 12: Component 5: Component 21: Component 11: 1 string mismatch
     Component 1: Component 12: Component 5: Component 21: Component 12: 1 string mismatch
     Component 2: Component 12: Component 5: Component 21: Component 11: 1 string mismatch
     Component 2: Component 12: Component 5: Component 21: Component 12: 1 string mismatch
     
     [ FAIL 2 | WARN 3 | SKIP 0 | PASS 163 ]
     Error: Test failures
     Execution halted 
Flavor: [r-devel-linux-x86_64-debian-clang](https://www.r-project.org/nosvn/R.check/r-devel-linux-x86_64-debian-clang/usmap-00check.html)

This must be fixed by November 21, 2022 or package will be removed from CRAN. Fix will be included in version 0.6.1.

plot_usmap Error in if (labels) { : argument is not interpretable as logical

I get the error listed in the title of this issue when running a simple program:

library("usmap")
library("ggplot2")

fipsCodes <- c("54001","54003")
values <- c(10,20)
mapData <- data.frame (fipsCodes,values,stringsAsFactors=FALSE)
names(mapData) <- c("fips","value")
meanValue <- mean(values)
                  
usmap::plot_usmap(data = mapData, values="value", lines="red",
                  regions="counties", 
                  include = c("WV"),
                  scale_fill_continuous(low="#559999",mid="grey90",high="#886508",midpoint=meanValue),
                  theme(legend.position = "right")) 

Error in if (labels) { : argument is not interpretable as logical

If I remove the line containing the parameter "scale_fill_continuous(...", it works fine.

Documentation request: add these projection details to Intro/Vignette PDF

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"))

This is an important piece of projection info that will let others use other dataframes, for instance, having unprojected geographic data (i.e., lat/long coordinates) transform to the right spatial projection. I would include this in the intro/vignette part where you describe which projection is used (i.e., page 17 of https://cran.r-project.org/web/packages/usmap/usmap.pdf).

Add ability to only display certain states in county map

The county map should be able to display only counties in certain states using the include parameter.

For example,

plot_usmap("counties", include = c("NJ", "PA", "NY"))

This can be fixed at this location in usmap.R:

if (length(include) > 0) {
  if (regions_ %in% c("counties", "county")) {
    df <- df[df$fips %in% include, ]
  } else {
    df <- df[df$full %in% include | df$abbr %in% include | df$fips %in% include, ]
  }
}

The body of the if statement should be changed to:

if (length(include) > 0) {
  df <- df[df$full %in% include | df$abbr %in% include | df$fips %in% include, ]
}

This should work because both the county and state map files have columns fips, full, and abbr.

Updating this function also has the added side-effect of allowing users to specify any combination of states and counties to include using either FIPS code, state abbreviation, state name, or a mixture of the three.

Resolve aes_string and size deprecation

aes_string

aes_string is soft-deprecated in ggplot2 according to https://ggplot2.tidyverse.org/reference/aes_.html, see warnings below.

══ Warnings ════════════════════════════════════════════════════════════════════
── Warning ('test-plot.R:10'): (code run outside of `test_that()`) ─────────────
Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
β„Ή Please use `linewidth` instead.
Backtrace:
 1. usmap::plot_usmap("counties", fill = "red")
      at test-plot.R:10:0
 3. ggplot2 (local) `<fn>`(fill = "red", color = "black", size = 0.4, mapping = `<uneval>`)
 4. ggplot2::layer(...)
 5. ggplot2:::deprecate_soft0(...)
── Warning ('test-plot.R:11'): (code run outside of `test_that()`) ─────────────
`aes_string()` was deprecated in ggplot2 3.0.0.
β„Ή Please use tidy evaluation ideoms with `aes()`
Backtrace:
 1. usmap::plot_usmap(data = statepop, values = "pop_2015", color = "blue")
      at test-plot.R:11:0
 2. ggplot2::aes_string(x = "x", y = "y", group = "group", fill = values)
 3. ggplot2:::deprecate_soft0("3.0.0", "aes_string()", details = "Please use tidy evaluation ideoms with `aes()`")

size

size is deprecated in ggplot2 3.4.0 and has been replaced with linewidth

Plan

Fixes will be included in v0.6.1 release.

Using `fips` on a vector causes a warning

Expected behavior

Passing in a vector (e.g. a list of state abbreviations) to fips should result in a vector of the same length containing the respective FIPS codes.

Actual behavior

A warning is presented, due to the non-vectorized nature of the internal code of fips.

Steps to reproduce behavior

Attempt to pass a vector to fips:

fips(c("AK", "AL", "AR"))
#> Warning message:
#> In if (!(state_ %in% abbr) & !(state_ %in% full)) { :
#>   the condition has length > 1 and only the first element will be used

How to fix

Rework the if-statements to check whether the input is valid. This can be done in conjunction with issue #7 and made into a much more robust system of checking whether the input is states, counties, abbreviations, names, etc.

Plotting both state and county boundaries on same map

How could I plotting both state and county boundaries on the same map?

the following codes could run smoothly in the past,

counties_df <- us_map(regions = "counties", exclude = c("AK", "HI"))
states_df <- us_map(regions = "states", exclude = c("AK", "HI"))

ggplot() +
  geom_polygon(data = counties_df,
               aes(x = x, y = y, group = group),
               fill = NA, color = "black") +
  geom_polygon(data = states_df,
               aes(x = x, y = y, group = group),
               fill = NA, color = "red") +
  coord_equal()

But now the two do not overlap. Is there anyway to fix this? Thank you so much!

issue

Add simultaneous plotting of counties and states

Is your feature request related to a problem? Please describe.
There should be a way to plot both states and counties simultaneously. For example, plotting Alaska as a state with several Alaskan counties also outlined within them (possibly with different aesthetic features).

Describe the solution you'd like
This needs more thought but perhaps a boolean parameter to plot_usmap indicating that the include/exclude parameters that are states should be plotted as states and counties should be plotted as counties (instead of just following what is defined in the regions parameter).

Describe alternatives you've considered
Currently, the only way to achieve something like this is to create two separate plot_usmap objects (one for the states and one for counties) and then combine them by extracting the $layers[[1]] object from the plot_usmap object which is both not ideal and could be unreliable if ggplot2 behavior were to be changed in a future update.

For example:

library(usmap)
library(ggplot2)

states <- plot_usmap("states", include = "CA")
counties <- plot_usmap("counties", include = "06073", color = "red")

ggplot() +
  states$layers[[1]] +
  counties$layers[[1]] +
  counties$theme +
  coord_equal()

`fips` should be vectorized for counties

Actual Behavior

Currently, the following does not work:

fips("NJ", county = c("Bergen", "Hudson"))

As it outputs the following:

#> [1] "34003"
#> Warning messages:
#> 1: In if (county_ == "") { :
#>   the condition has length > 1 and only the first element will be used
#> 2: In name == county_ :
#>   longer object length is not a multiple of shorter object length
#> 3: In name == paste(county_, "county") :
#>   longer object length is not a multiple of shorter object length

It just results in the FIPS code of the first county entered in the vector, followed by a warning.

Expected Behavior

fips("NJ", county = c("Bergen", "Hudson"))
#> [1] "34003" "34017"

This would allow the ability to quickly convert a column of county names to a FIPS code in a data frame.

Things to Consider

  • How to handle multiple states and multiple counties at the same time?
    For example:
fips(state = c("NJ", "NY"), county = c("Bergen", "Hudson", "Westchester")

This would not work unless there was a direct mapping between the state and county parameters. In this case it would be easier to throw an error.

Automatically convert data with state names/abbreviations to FIPS

Currently, plotting and merging with the map data requires a data frame containing a fips column. The data should be able to be plotted with a column of state abbreviations or names as well.

This can be easily achieved by converting to FIPS codes with the fips function internally if abbreviations/names are detected.

Sent with GitHawk

Rename lat/long columns

Since a transform is applied to the original shape file, the column headers lat and long for the map generated by us_map() are not appropriate. Renaming them to x and y would make more sense.

Add ability to add custom shapefile layers

User should be able to add other shape file layers superimposed on the US map (e.g. roads, rivers, temperature data, etc.).

One way this could work is as follows:

roads <- # load shape file...

plot_usmap("counties") +
  add_layer(roads) +
  add_layer(...)
# etc.

This could work by applying the same spTransform that was applied to the original US map data to the layers passed through to plot_usmap.

Merging FIPS to map data breaks polygons

Something in the merging process breaks the appearance of the map when plotted. Polygon points are mismatched causing extraneous lines to appear all over the map (especially in county map).

fips_info should not sort and remove duplicates

I was surprised that fips_info would sort and remove duplicates from the argument.

Current:

fips_info(c("21","20","20"))

  abbr fips     full
1   KS   20   Kansas
2   KY   21 Kentucky

Expected:

  abbr fips     full
1   KY   21 Kentucky
2   KS   20   Kansas
3   KS   20   Kansas

In other words

 new_fips_info <- function(fips) if (length(fips)!=0)
     merge(list(fips=fips), fips_info(), all.x=TRUE )

This comes up when I want the state name from the fips stored in df$fips. I expected this to work within(df, state <- fips_info(fips)$full). Instead merge(df, fips_info(), all.x=T) %>% rename(state='full') will do.

Warnings

Describe the bug
When plotting maps I get the following warnings:

Use of `map_df$x` is discouraged. Use `x` instead.
Use of `map_df$y` is discouraged. Use `y` instead.
Use of `map_df$group` is discouraged. Use `group` instead.`

To Reproduce
This is running in RStudio (R version 3.5.3) ggplot 3.3.0, and usmap 0.5.0

Additional context
Package still works as expected, would like to update code to avoid the warnings.

Add ability to see labels in county map

County map should be able to display labels just like state maps.

However, this require further consideration on how to display the labels since they would be much longer than state abbreviations (should "county" be omitted?, sizing, geom_text vs geom_label etc.).

Unable to add usmap$layer to ggplot object

Describe the bug
As I understand it, currently the only way to have county data and state data in the same map is to create the maps separately, extract their layers and add it to a single ggplot object.
However, this method throws an error.

Simplest code to reproduce

ggplot() +
  plot_usmap()$layers[[1]]

Error

Error in FUN(X[[i]], ...) : object 'x' not found

Environment:

  • Windows 10 x64
  • R v4.0.3
  • ggplot2_3.3.2
  • usmap_0.5.1

Additional info
This method is also the only way to add multiple levels of data on the same map. So this should be looked into regardless of #26

States with a leading 0 in its FIPS code causes issues with plotting

The following states may cause issues when plotting with data because the leading zero may be omitted if the input data is numeric rather than a character (e.g. "01" becomes 1 and "01001" becomes 1001). This affects any counties within the following states as well.

Abbr  FIPS  Name
====  ====  ====
AK    02    ALASKA
AL    01    ALABAMA
AR    05    ARKANSAS
AZ    04    ARIZONA
CA    06    CALIFORNIA
CO    08    COLORADO
CT    09    CONNECTICUT

The solution will be to cast the input fips column to characters and make sure they are padded correctly ("00" for states and "00000" for counties).

Allow applying visual properties to defined regions

Is your feature request related to a problem? Please describe.
Users have requested the ability to define visual properties of the defined US Census Bureau regions such as borders and fill colors.

Describe the solution you'd like
A new shape file would probably have to be constructed where each state within the region is merged into a single entity. New parameters would have to be added to plot_usmap to define which regions' visual properties should be modified and how.

Describe alternatives you've considered
None, this idea will require some refinement and planning.

Additional context
Related StackOverflow questions:

Create an R spatial object rather than a gg object to enhance mapping capacity

Please correct me if I am wrong here, but the only spatial features I see in the maps are to obtain the CRS, and transform the object to a data frame (but not an SPDF). I see there is an option to snap other data frames having the same CRS to the usmap (gg).

Is there a simple way to back-transform the gg object to an SPDF or other spatial format outside gg universe?

Provide entire FIPS for states and counties

Great package, thanks!

I request you add one or both of the following:
a) a data object (retrievable via utils::data()) which returns all FIPs info
b) fips_info() and/or fips() returns all FIPS info when blank, or specifying states="all", etc.

Perhaps this feature is buried elsewhere?

Add support for PUMA geography?

Love the package -- great for quick/easy mapping.

Would it be straightforward to add support for Public Use Microdata Area (PUMA) geography?

That would allow for mapping of PUMA-level values derived from American Community Survey microdata files.

Revisit function naming

There are now a lot more functions within usmap than when the project was started. There should be some thought given to the naming of these functions going forward (before we reach a stable 1.0 release).

If the functions are to be changed, they will be renamed by v0.7.0 and the old function names will remain but be deprecated until v1.0.0 (where they will be removed).

Current Name Proposed Name
fips
fips_info
map_with_data
plot_usmap
usmap_crs
usmap_transform
usmap

Fix r-devel test errors

Describe the bug
Tests fail on certain r-devel environments due to changes in R discussed here: https://stat.ethz.ch/pipermail/r-devel/2020-December/080172.html

To Reproduce
See logs here: https://cran.r-project.org/web/checks/check_results_usmap.html

Expected behavior
All tests should run properly and successfully on all environments.

Environments:
usmap 0.5.1

r-devel-linux-x86_64-debian-clang
r-devel-linux-x86_64-debian-gcc
r-devel-linux-x86_64-fedora-clang
r-devel-linux-x86_64-fedora-gcc	
r-devel-windows-ix86+x86_64

Additional context
Possible solution here: Ryo-N7/tvthemes#15

Must be resolved by January 8, 2021 to prevent removal from CRAN.

usmap_crs causes warning

Running usmap::usmap_crs() results in the following warning:

Warning message:
In showSRID(uprojargs, format = "PROJ", multiline = "NO") :
Discarded datum unknown in CRS definition

The associated tests produce the following warnings:

test-transform.R:16: warning: data frame with AK and HI points is transformed
Discarded datum unknown in CRS definition

test-transform.R:16: warning: data frame with AK and HI points is transformed
CRS object has comment, which is lost in output

test-transform.R:16: warning: data frame with AK and HI points is transformed
Discarded datum Unknown based on Normal Sphere (r=6370997) ellipsoid in CRS definition

test-transform.R:16: warning: data frame with AK and HI points is transformed
CRS object has comment, which is lost in output

test-transform.R:16: warning: data frame with AK and HI points is transformed
Discarded datum Unknown based on Normal Sphere (r=6370997) ellipsoid in CRS definition

test-transform.R:32: warning: data frame with AK points is transformed
Discarded datum unknown in CRS definition

test-transform.R:32: warning: data frame with AK points is transformed
CRS object has comment, which is lost in output

test-transform.R:32: warning: data frame with AK points is transformed
Discarded datum Unknown based on Normal Sphere (r=6370997) ellipsoid in CRS definition

test-transform.R:48: warning: data frame with HI points is transformed
Discarded datum unknown in CRS definition

test-transform.R:48: warning: data frame with HI points is transformed
CRS object has comment, which is lost in output

test-transform.R:48: warning: data frame with HI points is transformed
Discarded datum Unknown based on Normal Sphere (r=6370997) ellipsoid in CRS definition

test-transform.R:64: warning: data frame with no AK or HI points is transformed
Discarded datum unknown in CRS definition

Environment

macOS 10.15.7

> version
               _                           
platform       x86_64-apple-darwin17.0     
arch           x86_64                      
os             darwin17.0                  
system         x86_64, darwin17.0          
status                                     
major          4                           
minor          0.2                         
year           2020                        
month          06                          
day            22                          
svn rev        78730                       
language       R                           
version.string R version 4.0.2 (2020-06-22)
nickname       Taking Off Again
> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] usmap_0.5.1       rstudioapi_0.11   knitr_1.30        magrittr_1.5      maptools_1.0-2   
 [6] munsell_0.5.0     colorspace_1.4-1  lattice_0.20-41   R6_2.4.1          rlang_0.4.7      
[11] tools_4.0.2       grid_4.0.2        gtable_0.3.0      xfun_0.18         tinytex_0.26     
[16] htmltools_0.5.0   ellipsis_0.3.1    yaml_2.2.1        digest_0.6.25     tibble_3.0.3     
[21] lifecycle_0.2.0   crayon_1.3.4.9000 ggplot2_3.3.2     vctrs_0.3.4       glue_1.4.2       
[26] evaluate_0.14     rmarkdown_2.4     sp_1.4-2          pillar_1.4.6      compiler_4.0.2   
[31] scales_1.1.1      foreign_0.8-80    pkgconfig_2.0.3 

usmap_transform improvements

Improve data input flexibility

Currently the data frame that is passed to usmap_transform must have longitude in the first column and latitude in the second column. It might be more natural to allow columns in any order that have the names lon/long/longitude and lat/latitude.

There can also be two optional parameters to specify the longitude/latitude column names.

For example:
If a data.frame contains columns "x" and "y" instead,
usmap_transform(data, longitude_name = "x", latitude_name = "y")

Improve data output

Currently the output data frame contains 4 columns: the two input columns followed by the two output columns. The output columns have the same name as the input columns but with a .1 appended to the end. This does not allow for intuitive use of the function as someone would have to inspect and notice the differences manually.

It might be better include an optional parameter that allows replacement of the input columns (i.e. input data frame has 2 columns named "lon" and "lat", and the output data frame has the same 2 columns, with data transformed). If all 4 columns are to be included in the output, a better convention might be to append _tf or prefix transformed_, etc. (TBD)

Other Fixes

Metadata for data updating frequency, last version

Please provide metadata or information in the README (or elsewhere) which provides information on the frequency (preferably the times) at which the boundaries are updated.

Would also be really cool to have historical boundaries available (might increase probability of people using this package for time series visualizations of political boundary shifts, eg gerrymandering)

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.