knitr::opts_chunk$set(echo = FALSE) library(knitr) library(png)
sp
, rgeos
, raster
, rgdal
, tigris
, tidycensus
include_graphics("fig/3DistrictTypes.png") include_graphics("fig/Census_tracts.png")
merge
with with tract geographic boundary file from tigris
packageinclude_graphics("fig/Tidy_census_data.png") include_graphics("fig/Census_tract_pop.png")
intersect
census tracts with districts gArea
to find how much area of each tract is contained in each districtsapply
for iteration to create a data framemerge
dataframe onto district polygonssld_pop_table <- as.data.frame(t(sapply(1:length(state_lower@data$SLDLST), function(z) { sd_c_int <- intersect(x = state_lower[state_lower@data$SLDLST == z,], y = state_tracts_pop) dc_area <- gArea(spgeom = sd_c_int, byid = TRUE)/1000000 percentage <- dc_area/sd_c_int@data$AREA Pop_Total <- round(sum(sd_c_int@data$Pop_Total*percentage)) Pop_White <- round(sum(sd_c_int@data$Pop_White*percentage)) Pop_Black <- round(sum(sd_c_int@data$Pop_Black*percentage)) Pop_American_Indian <- round(sum(sd_c_int@data$Pop_American_Indian*percentage)) Pop_Asian <- round(sum(sd_c_int@data$Pop_Asian*percentage)) Pop_Hawaian_Pacific_Islander <- round(sum(sd_c_int@data$Pop_Hawaian_Pacific_Islander*percentage)) Pop_Hispanic <- round(sum(sd_c_int@data$Pop_Hispanic*percentage)) unlist(data.frame(state_lower[state_lower@data$SLDLST == z,], Pop_Total, Pop_White, Pop_Black, Pop_American_Indian, Pop_Asian, Pop_Hawaian_Pacific_Islander, Pop_Hispanic)) }))) # converting factors in data.frame to numeric sld_pop_table[] <- lapply(sld_pop_table, function(x) as.numeric(levels(x))[x]) # merging with state lower districts sld_pop <- merge(x = state_lower, y = sld_pop_table, by = "SLDLST")
SpatialPolygonDataFrames
, one for each district type with associated demographic data (18 years and older)include_graphics("fig/Example_Output_Table.png")
spplot
function in sp
package along with packages RColorBrewer
and Classint
include_graphics("fig/WISLD_choro.png") include_graphics("fig/WISUD_choro.png") include_graphics("fig/WISFCD_choro.png")
include_graphics("fig/WISLD_choro_black.png") include_graphics("fig/WISUD_choro_black.png") include_graphics("fig/WISFCD_choro_black.png")
include_graphics("fig/NCSLD_choro.png") include_graphics("fig/NCSUD_choro.png") include_graphics("fig/NCSFCD_choro.png")
include_graphics("fig/NCSLD_choro_black.png") include_graphics("fig/NCSUD_choro_black.png") include_graphics("fig/NCSFCD_choro_black.png")
sf
packageggplot2
, GISTools
, leaflet
, or tmap
)Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.