hrbrmstr / albersusa Goto Github PK
View Code? Open in Web Editor NEWTools, shapefiles & data to work with an "AlbersUSA" composite projection in R
License: Other
Tools, shapefiles & data to work with an "AlbersUSA" composite projection in R
License: Other
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")
(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)
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")
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)
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.
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
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)
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'
Is there a plan to include a US Metro Areas sf
object?
It's the way of the future!
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.
Any suggestions about what could be happening with rmarkdown
that causes these to behave differently?
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 <- usa_composite()
us_map <- fortify(us, region="name")
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()
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
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.
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
abandon it and support https://github.com/pdil/usmap @hadley @rdinter ?
FWIW I have a new pkg ready that only uses sf
(halfway done porting the ++gd independent elide-ing code to sf
@rdinter added, too) so we cld keep both.
That will help in easy geom_text/geom_point leaflet markers etc.
Mercator really sucks and seems people don't take much efforts to try out other projections. So it makes sense to make Alber's the default projection.
i.e. crs_us_aeqd
instead of us_aeqd_proj
?
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.
All in title..
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?
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
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.