data(north_carolina.tract)
############################################
## Helper function for handling coloring of the map
############################################
color.map<- function(x,dem,y=NULL){
l.poly<-length(x@polygons)
dem.num<- cut(dem ,breaks=ceiling(quantile(dem)),dig.lab = 6)
dem.num[which(is.na(dem.num)==TRUE)]<-levels(dem.num)[1]
l.uc<-length(table(dem.num))
if(is.null(y)){
##commented out, but creates different color schemes
## using runif, may take a couple times to get a good color scheme.
##col.heat<-rgb( runif(l.uc,0,1), runif(l.uc,0,1) , runif(l.uc,0,1) )
col.heat<-heat.colors(16)[c(14,8,4,1)] ##fixed set of four colors
}else{
col.heat<-y
}
dem.col<-cbind(col.heat,names(table(dem.num)))
colors.dem<-vector(length=l.poly)
for(i in 1:l.uc){
colors.dem[which(dem.num==dem.col[i,2])]<-dem.col[i,1]
}
out<-list(colors=colors.dem,dem.cut=dem.col[,2],table.colors=dem.col[,1])
return(out)
}
############################################
## Helper function for handling coloring of the map
############################################
colors.use<-color.map(north_carolina.tract,north_carolina.tract$pop2000)
plot(north_carolina.tract,col=colors.use$colors)
#text(coordinates(north_carolina.tract),north_carolina.tract@data$name,cex=.3)
title(main="Census Tracts \n of North_carolina, 2000", sub="Quantiles (equal frequency)")
legend("bottomright",legend=colors.use$dem.cut,fill=colors.use$table.colors,bty="o",title="Population Count",bg="white")
###############################
### Alternative way to do the above
###############################
## Not run:
####This example requires the following additional libraries
library(RColorBrewer)
library(classInt)
library(maps)
####This example requires the following additional libraries
data(north_carolina.tract)
map('state',region='north_carolina')
plotvar <- north_carolina.tract$pop2000
nclr <- 4
#BuPu
plotclr <- brewer.pal(nclr,"BuPu")
class <- classIntervals(plotvar, nclr, style="quantile")
colcode <- findColours(class, plotclr)
plot(north_carolina.tract, col=colcode, border="transparent",add=TRUE)
#transparent
title(main="Census Tracts \n of North_carolina, 2000", sub="Quantiles (equal frequency)")
map.text("county", "north_carolina",cex=.7,add=TRUE)
map('county','north_carolina',add=TRUE)
legend("bottomright","(x,y)", legend=names(attr(colcode, "table")),fill=attr(colcode, "palette"),
cex=0.9, bty="o", title="Population Count",bg="white")
## End(Not run)