0

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)

enter image description here

Community
  • 1
  • 1
bharath
  • 60
  • 5
  • Edit: A user had commented asking me to use the tidy in bloom package instead of fortify. I tried it and updated the original post with the image of the plot and for some reason, his comment disappeared. Tidy(usmap) works fine like fortify(usmap). However, tidy(usmap,region="id) gives the same error as fortify(usmap,region="id). I am not sure what the issue is and hoping soemone would be able to provide some insights. – bharath Mar 17 '16 at 20:35

1 Answers1

2

I was able to figure out the mistake here. Assigning rowid to the initial spatial data and then joining the spatial data did the trick.

Modified code:

usmap@data$id <-rownames(usmap@data)
usmap@data<-left_join(usmap@data,pop_sub,"name")

#row.names(usmap@data) <- NULL
usmap_f<-fortify(usmap)
usmap_f<-inner_join(usmap_f,usmap@data,"id")

Plot after correction

bharath
  • 60
  • 5
  • I found this post when searching for the error message mentioned, Error in maptools::unionSpatialPolygons. I had recently updated to R 3.5.3 and cleaned up my libraries by reinstalling rather than porting over all old packages. The error appeared with a 'tidy' command on spatial data that used to run fine. That means I don't know which package(s) or command(s) have changed. But adding rownames as data$id fixed the vexing problem. – InColorado Apr 12 '19 at 16:01