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'
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
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??