Question

First, I apologize for the title, but I am not entirely sure how to describe what is happening with my map (hence the image inclusion). I am new to mapping with ggplot2, and for my first 'out of tutorial' map, I am attempting to display census data by tract in DC. Searching through the tutorials seems to support the procedure I have taken, but clearly I am veering off course (and I have not seen another instance of this).

The goal has been to merge data from a .csv I have prepared with a shapefile containing the Census tract polys. Initially, I thought it had something to do with the ordering of rows, but explicitly accounting for this does not seem to have changed anything.

Code:

###Combining Census data with a tract poly shapefile
library(maptools)
library(ggplot2)
library(gpclib)

setwd('~/ESRI/Trend Report Maps')

#Read data
tract<-readShapePoly('TractPly.shp')
cdata<-read.csv('census00_10.csv')

#Columns >> note that GEOID (tract) and geoid2 (cdata) are the merge keys
ntract<-names(tract)
ncdata<-names(cdata) 


#Prepare data for ggplot plotting of poly info (by conversion to DataFrame)
gpclibPermit()
tract_geom<-fortify(tract,region="GEOID")
#Note that this drops attribute and retains only spatial info. However, we don't really
#need the attribute info since we are joining it to the other dataframe

#Merge
tract_poly<-merge(tract_geom,cdata,by.x="id",by.y="geoid2")
tract_poly<-tract_poly[order(tract_poly$order),]
head(tract_poly)

workF<-ggplot(tract_poly,aes(long,lat,group-group,fill=dmed_age)) + 
        geom_polygon() +
        coord_equal() #fixes aspect ratio
workF

What is going on here??

Yeah, visualization of Census data has seen better days. In case I am not permitted to display the map, it looks like a haphazard assortment of jagged lines (not unlike what it looks like when someone folds up paper many times and randomly cuts pieces out of it before unfolding). None of the resultant shapes look like census tracts, though the outer boundary of DC remains in tact. I can confirm that the shapefile was exported from ArcMap (where it looked as it should).

Session info:

> sessionInfo()
R version 2.15.3 (2013-03-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gpclib_1.5-1    ggplot2_0.9.3.1 maptools_0.8-23 lattice_0.20-13 sp_1.0-5        foreign_0.8-52 

loaded via a namespace (and not attached):
 [1] colorspace_1.2-1   dichromat_2.0-0    digest_0.6.3       gtable_0.1.2       labeling_0.1      
 [6] MASS_7.3-23        munsell_0.4        plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5
[11] reshape2_1.2.2     scales_0.2.3       stringr_0.6.2      tools_2.15.3      
Was it helpful?

Solution

It's just a typo. Change group-group to group=group and you should be fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top