0

Hello, I am new to GIS with R and have been trying to create a choropleth map. I successfully created a choropleth map with ggplot2 and the fortify function, but it is not that easy to add more layers on the top of a map with ggplot2. Instead I am using maptools to plot a choropleth map and later add more layers as needed for my analysis. The choropleth map I am trying to plot is the level of unemployment for Allegheny county by census tract. the files are available here: shapefile https://www.dropbox.com/s/uci5g2ekeq9niww/census%20tract%20allegheyny%202010.shp csv file https://www.dropbox.com/s/6nq8nnxftot8iya/allegheyny%20socioeconomic%20info.csv And here is my code

library(rgdal)
library(RArcInfo)
library(RColorBrewer)
library(maptools)
library(maps)
library(classInt)
we load the csv file, clean it and create a subset with Id2 and unemployment
data<- read.csv('allegheyny socioeconomic info.csv',dec='.',
                header=T)
data$Id2<-as.numeric(as.character(data$Id2))
data$Percent.Unemployed<-as.numeric(as.character(data$Percent.Unemployed))
names(data)[names(data)=="Percent.Unemployed"]<-'unemployed'
data1<-subset(data, select= c('Id2', 'unemployed'))
load shapefile of Allegheny County census tracts for 2010
tracts<-readShapePoly("census tract allegheyny 2010.shp")
names(tracts)[names(tracts)=="GEOID10"]<-'Id2'

merge the data by Id2

tr1<-merge(data1,tracts)                     
sort(tr1$Id2)
colours<-brewer.pal(5, 'Greens')
breaks<- classIntervals(tr1$unemployed, n=5, style='sd')
plot(tr1, col=colours[findInterval(tr1$unemployed, breaks, all.inside=T), axes=F])
And this is the message I get:
Error in x[-1L] >= x[-n] : comparison of these types is not implemented
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
asado23
  • 366
  • 1
  • 7
  • 20
  • Type: `traceback()` ... report the results. – IRTFM Dec 10 '13 at 22:40
  • After doing a couple of steps and then looking at data1 with str() I need to ask: Why would you want a variable named 'Id2' to be class 'numeric'? I would have thought it should be character. (I also get an error opening that shape file.) – IRTFM Dec 10 '13 at 22:53
  • Regarding the Id2, even though it is an id variable it is numeric and sometimes there are issues when merging with csv files. Also I uploaded all of the other files to properly read the shape file. This is the link: https://www.dropbox.com/s/gycwkruqx4msmlf/GIS.rar – asado23 Dec 10 '13 at 23:47

1 Answers1

1
plot(tracts,col=colours[findInterval(tr1$unemployed, breaks$brks, all.inside=T)])

Produces this:

To answer your question specifically:

The reason for the error is that findInteerval(...) takes as its second argument a vector of numeric. But

 breaks<- classIntervals(tr1$unemployed, n=5, style='sd')

produces a list with two elements: 'var' and 'brks'. You need to use breaks$brks in findInterval(...).

Your statement

plot(tr1, col=colours[findInterval(tr1$unemployed, breaks, all.inside=T), axes=F])

attempts to plot tr1, which is not a map, it's a data frame with 402 rows. You need to plot(tracts,...)

Finally, why do you believe it is difficult to add layers in ggplot??

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • @jhoward: Have you ever looked at the documentation present in `?layers`? Report back if you think someone would get value out of that page. – IRTFM Dec 11 '13 at 04:58
  • Thanks so much for your answer, it works perfectly. The only reason why ggplot2 seems confusing, to me a newbie, is the fact that I do not really understand how to add more layers without messing up the code, while with plot you just enable the option add=T. – asado23 Dec 11 '13 at 05:09
  • I will take a look at the documentation present in ?layers. – asado23 Dec 11 '13 at 05:10
  • It's actually in ?layer that I am complaining about, there being no page at either ?layers or at ?ggplot2::+ or ?ggplot2::+ .... oh heck, I give up; I cannot get SO Markdown to display the plus sign surrounded by single backticks. Contrast that with what you see in the lattice/trellis documentation for the "+" method: ?latticeExtra::`+.trellis` . (You do need the backticks surrounding the expression after the "::".) – IRTFM Dec 11 '13 at 06:53
  • @DWin: Are you referring to `?layer` in the `latticeExtra` package? I'm not really familiar with `lattice` - I try to do everything using `ggplot` if at all possible. – jlhoward Dec 11 '13 at 07:46
  • @jesusleal: The answer to [this question](http://www.stackoverflow.com/questions/20482822/) includes a map of the world with a separate layer identifying intensity and depth of earthquakes. The `ggplot` code is actually fairly simple. – jlhoward Dec 11 '13 at 07:51