0

I'm quite new with R and have been trying to use ggplot2 to try and replicate county-level choropleths of unemployment data, using FIPS codes as the identifying variable between both shapefiles and the data. I tried to mesh the methods used by Hadley and Barry years ago(http://blog.revolutionanalytics.com/2009/11/choropleth-challenge-result.html), but I keep running into the error: Aesthetics must either be length one, or the same length as the dataProblems:freq_a

I have been trying to find out how to fix this for a couple of days now, so would really appreciate any help.

Here is my code:

    require(rgdal)
    require(maps)
    require(rgdal)
    require(RColorBrewer)
    require(ggplot2)


    unem<-read.table("unemployment09.csv",sep=",",
     as.is=TRUE,
    colClass=c(rep("character",8),"numeric"),
    col.names=c("code","f1","f2","name","yr","x1","x2","x3","percentage")
      )

    unem$fips=paste(unem$f1,unem$f2,sep="")


   county<-readOGR("tl_2009_us_county.shp",  
  "tl_2009_us_county")
  colClasses=c("character", "character")
   county$fips<-paste(county$STATEFP,county$COUNTYFP,sep="")

  m<-match(county$fips, unem$fips)
  county$freq<-unem$percentage[m]
  county$freq[is.na(county$freq)]=0

  county$freq_a<-cut(county$freq, breaks = c(seq(0,10,by=2),35))



  county$freq_a<-as.numeric(county$freq_a)

  ggplot(county, aes(long, lat, group = group)) +
  geom_polygon(aes(fill = county$freq_a), colour = "white", size = 0.2) + 
  scale_fill_brewer(palette = "PuRd") + theme(panel.background = element_rect(fill = 
  "white"))        

Thanks

  • 1
    (1) Never refer to variables like `county$freq_a` inside of `aes()`; just `fill = freq_a`, (2) Does `county` have a column named `group`? – joran Nov 07 '13 at 16:11
  • When I do fill = freq_a, I get this error: Error in eval(expr, envir, enclos) : object 'freq_a' not found. Because of that I thought I had to specify county$freq_a. There is no column group in county. Thanks @joran for your help – user2965512 Nov 07 '13 at 16:54
  • You would only get that error if ggplot could not find `freq_a` inside of `county`. So it's impossible to say exactly what's going on without a reproducible example. But you should still never be subsetting data frames from inside of `aes()`. That's not how it was designed to be used, and will cause other problems down the road. – joran Nov 07 '13 at 16:57

0 Answers0