Uncategorized

Census Bureau Maps

Maps

1. Download population data from Census Bureau
I chose population data by county for 2015 American Community Survey
download and save (aff_download.zip)
2. Download shapefile of counties in United States (tl_2016_us_county.zip)
3. Extract compressed files

4. Load required packages
require(dplyr)
require(geosphere)
require(ggmap)
require(ggplot2)
require(maptools)
require(raster)
require(rgdal)
require(rgeos)
require(sp)
require(spdep)

5. Establish directories
xdir = ‘C:/_data/blog/map/Shapefiles’
ydir = ‘C:/_data/blog/map’

6. Read spatial data into R by directory and filename (object is currently a spatial polygon dataframe)
SPD <- readOGR(dsn = xdir, layer = ‘tl_2016_us_county’)
SPD@data$id <- rownames(SPD@data)

7. Covert to dataframe
DF <- fortify(SPD, region= ‘id’)
DF2 <- merge(DF, SPD@data, by=’id’)

8. Filter by desired latitude and longitude
DF3 <- filter(DF2, lat < 48)
DF4 <- filter(DF3, lat > 34)
DF5 <- filter(DF4, long > -76)
DF6 <- filter(DF5, long < -67)

9. Change working directory to where population data is stored and read first 4 columns
setwd(ydir)
popdata <- read.csv(“ACS_15_5YR_S0101_with_ann.csv”)
popdata2 <- popdata[,1:4]

10. Join spatial data and population data
map <- left_join(DF6, popdata2, by = c(“GEOID” = “GEO.id2”))

11. Convert population to numeric so scale will be continuous
map$pop <- as.numeric(map$HC01_EST_VC01)

12. Plot map dataframe using ggplot with latitude and logitude being x and y
p <- ggplot() + geom_polygon(data=test, aes(x=long, y=lat, group=group, fill=pop))
p2 <- p + coord_equal()
print(p2)

map1
13. You can also change the map colors to better visualize the data
Here I made counties with high populations red and low populations green.  We could use this type of visualization if we are discussing a simple concept such as high populations having negative impacts on ecosystem health

map2
References:
http://ggplot2.tidyverse.org/reference/scale_brewer.html
http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s