1

everyone!

I am attempting to create a choropleth map of the Denver metro area, showing different housing values by census tract. When I try to map, it gives me 'Error: Aesthetics must be either length 1 or the same as the data (83658): x, y, group, fill'... How do I solve this problem, so I can create a visual map?

I read through various questions but none appeared to help answer the problem. Suggestions will be appreciated. A sample of 20 (of 588) data points is below.

Thanks!

1.Load packages

library(maptools)
library(ggmap)
library(ggthemes)
library(rgdal)

2.Select, define, plot initial map

 DenverMetro <- c(-105, 40, -104.8535, 39.3760)
    DenverMetroMap <- get_map(location=DenverMetro, 
                              source = "google", 
                              maptype = "roadmap",
                              zoom = 10,
                              crop=FALSE)
    ggmap(DenverMetroMap)

3.Import polygons from shp file

DenverMetroCensusTracts <-readOGR(dsn = "c://Users/John/Denver_Housing_Project/ACS_Data/Final_Data", layer = "cb_2013_08_tract_500k")
proj4string(DenverMetroCensusTracts)
DenverMetroCensusTracts <-spTransform(DenverMetroCensusTracts,
                              CRS("+proj=longlat + datum=WGS84"))

4.Create initial census tract map

ggmap(DenverMetroMap) + geom_polygon(aes(x = long, y = lat, group=id),
             data = DenverMetroCensusTracts, 
             color="white", 
             fill="orange",
             alpha = .4, size = .2)

5a.Import Denver housing data, make one variable numeric, and merge with census shapefile

        setwd('C:/Users/John/Denver_Housing_Project/ACS_Data/Final_Data')
        Median_Values_ACS_13_DHP <-read.csv('ACS_13_5YR_B25077_DHP.csv',
         na.strings=c("NA", "-", "?", "(X)"), header=TRUE)
        names(Median_Values_ACS_13_DHP) <-c("AFFGEOID", "Id2", "Geography", "Year",
                                             "HouseValue_Median", "HouseValue_Median_MOE_DHP")

 HouseValue_Median=as.numeric(as.character(Median_Values_ACS_13_DHP$HouseValue_Median))

Mapping  <-merge(DenverMetroCensusTracts, Median_Values_ACS_13_DHP, by.x="AFFGEOID") 

6.Create choropleth map

ggmap(DenverMetroMap) + geom_polygon(aes(x = long, y = lat, group = id, 
 fill = HouseValue_Median), data = Mapping, alpha = .4, size = .2) +
 scale_fill_gradient()

GEO.id GEO.id2 GEO.display-label YEAR HD01_VD01 HD02_VD01 1400000US08001007801 8001007801 Census Tract 78.01, Adams County, Colorado 2013 150300 40311 1400000US08001007802 8001007802 Census Tract 78.02, Adams County, Colorado 2013 114700 4570 1400000US08001007900 8001007900 Census Tract 79, Adams County, Colorado 2013 118600 8228 1400000US08001008000 8001008000 Census Tract 80, Adams County, Colorado 2013 139000 10440 1400000US08001008100 8001008100 Census Tract 81, Adams County, Colorado 2013 29800 105549 1400000US08001008200 8001008200 Census Tract 82, Adams County, Colorado 2013 145100 6189 1400000US08001008308 8001008308 Census Tract 83.08, Adams County, Colorado 2013 46300 67342 1400000US08001988700 8001988700 Census Tract 9887, Adams County, Colorado 2013 - ** 1400000US08001008309 8001008309 Census Tract 83.09, Adams County, Colorado 2013 36600 18824 1400000US08001008353 8001008353 Census Tract 83.53, Adams County, Colorado 2013 134600 8921 1400000US08001008401 8001008401 Census Tract 84.01, Adams County, Colorado 2013 241300 43038 1400000US08001008402 8001008402 Census Tract 84.02, Adams County, Colorado 2013 215900 15189 1400000US08001008505 8001008505 Census Tract 85.05, Adams County, Colorado 2013 174100 6525 1400000US08001008506 8001008506 Census Tract 85.06, Adams County, Colorado 2013 157300 15556 1400000US08001008507 8001008507 Census Tract 85.07, Adams County, Colorado 2013 177800 5644 1400000US08001008508 8001008508 Census Tract 85.08, Adams County, Colorado 2013 188700 10424 1400000US08001008523 8001008523 Census Tract 85.23, Adams County, Colorado 2013 268500 29657 1400000US08001008524 8001008524 Census Tract 85.24, Adams County, Colorado 2013 231200 4994 1400000US08001008526 8001008526 Census Tract 85.26, Adams County, Colorado 2013 284900 14945

MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • I don't understand why your code formatting doesn't work in the beginning of your post... – HubertL Jan 07 '16 at 18:32
  • [Do you even google?](http://zevross.com/blog/2015/10/14/manipulating-and-mapping-us-census-data-in-r-using-the-acs-tigris-and-leaflet-packages-3/) – alexwhitworth Jan 07 '16 at 19:10
  • we can't reproduce your error because we don't have access to step 3 and 5. And what's all the data after step 6? – MLavoie Jan 08 '16 at 09:35
  • It's probably a problem with the join. If your shapefile is an `sp` spatial polygon data frame (or along those lines), you need to join your new data with the data, not the polygons. You do this by inserting `@data` between the name of the object and the column[s] you care about, e.g. `shapefile@data <- dplyr::inner_join(shapefile@data, newdata, by = c('shapefile@data$joinvar' = 'newdata$otherjoinvar'))` or whatnot. – alistaire Jan 08 '16 at 11:37

0 Answers0