I am trying to plot the US census data using ggplot2
. I was able to plot the data for one specific year (all states) and generate the map with correct data. When I try to combine a couple of years together to analyse the trend over a period, I am getting errors with both fortify
and facet_wrap
.
When I use fortify after combining multiple years to the @dataof
the spatial object I get this error:
Error in maptools::unionSpatialPolygons(cp, attr[, region]) : input lengths differ
I tried to implement the solution provided in this stackoverflow question Drawing maps based on census data using ggplot2 but that does not seem to work for me.
Since I am not sure of the purpose behind using region="id"
in fortify
, I used the function without it. Fortify works fine but I am facing issues with the output of Facet_Wrap()
. I seem to face the same issue as this user R, Incorrect output from ggplot2 and facet_wrap when using spatial data but the question is not answered.
I have followed the steps mentioned in http://spatial.ly/2013/12/introduction-spatial-data-ggplot2/ and I am not able to figure out the issue
My code:
#Import the data from the web and make it tidy. This data includes years from 1900-2000
url<-"http://www.demographia.com/db-state1900.htm"
pop_00<-readHTMLTable(url,which = 1,skip=1,stringsAsFactors=FALSE)
pop_00<-tbl_df(pop_00)
pop_00<-pop_00%>%gather(date,pop,-State)%>%filter(date!="2003")
colnames(pop_00)[1]<-"name"
#Import 2010 data from https://www.census.gov/popest/data/national/totals/2015/files/NST-EST2015-alldata.csv
uspop_10<-read.csv("NST-EST2015-alldata.csv")
poptidy<-tbl_df(uspop_10)
colnames(poptidy)<-tolower(colnames(poptidy))
poptidy<-select(poptidy,c(name,census2010pop))
poptidy<-poptidy%>%gather(date,pop,-name)
poptidy$date<-gsub("census2010pop","2010",poptidy$date)
pop_sub<-rbind(poptidy,pop_00)
pop_sub$pop<-as.numeric(pop_sub$pop)
# Download list of states to filter unwanted rows in the pop_sub table
url_state<-"http://www.columbia.edu/~sue/state-fips.html"
state_fips<-readHTMLTable(url_state,which=1)
pop_sub<-filter(pop_sub,name %in% state_fips$`State or District`)
# Shape file of US https://www.census.gov/geo/maps- data/data/cbf/cbf_state.html
usmap<-readOGR("cb_2014_us_state_500k",layer="cb_2014_us_state_500k")
colnames(usmap@data)<-tolower(colnames(usmap@data))
usmap@data<-left_join(usmap@data,pop_sub,"name")
usmap@data$id <-row.names(usmap@data)
usmap_f<-fortify(usmap,region="id")
usmap_f<-inner_join(usmap_f,usmap@data,"id")
ggplot(usmap_f)+aes(long,lat,group=group,fill=pop/1000)+geom_polygon()+facet_wrap(~date)+coord_map(xlim=c(-150,-50),ylim=c(20,50))+scale_fill_gradient2(low="green",mid="blue",high="red",midpoint=15000,name="Population in Thousands")+geom_path(colour="black", lwd=0.05)