[R] R help, using R to build choropleth

Jim Lemon jim at bitwrit.com.au
Thu Jun 28 12:38:03 CEST 2012


On 06/28/2012 03:26 PM, iverson wrote:
> Hi guys
>
> i need some help to build choropleth.
>
> Basically i am trying to colour regions on the map by population.
>
> I possess the shape file of the country, and also the population data,
> however, i am having trouble to create the plot, below is my code:
>
>
>
> population=read.csv("nz2.csv")
> population
> names(population)
>
> nz=readShapeSpatial((sprintf('nz-geodetic-marks',tmp_dir)))
>
> nz$land_distr
>
> mapnames = nz$land_distr
>
> mapnamesnew=as.character(mapnames)
>
> popnames =as.character(mapnamesnew)
> pop =population$People
>
> pop2=as.numeric(pop)
>
> popdich = ifelse(pop2<  100000, "red", "blue")
> popnames.lower = tolower(popnames)
>
> cols=popdich[match(mapnames,popnames.lower)]
>
> plot(nz,fill=TRUE,col=cols,proj="GCS_NZGD_2000")
>
Hi Iverson,
Here is some code that I have used to draw a choropleth map of NSW where 
the colors represent the average Index of Relative Social Disadvantage 
for Statistical Local Areas in New South Wales. I've cut out the bits 
that map the locations of low speed vehicle runovers as you won't need 
that code.

# load the RGDAL package
require(rgdal)
# read in the SLA boundaries for Australia
SLAmap<-readOGR(".","SLA10aAust")
# pick out the bits in NSW
NSWmap<-SLAmap[1:199,]
# read in the SEIFA (including IRSD) values
AU_SEIFA<-read.csv("AU_SEIFA_SLA_2006.csv")
require(plotrix)
# generate the colors for the IRSD scores
NSW_SEIFAcol<-color.scale(AU_SEIFA$SEIFA1dec[1:199],
  c(1,0.9,0.8,0.8),c(0.8,0.9,0.9,0.8),c(0.8,0.8,0.9,1),xrange=c(0,10))
# open the graphics device
png("lsvro_NSW_96-10.png",width=700,height=700)
par(mar=c(4,0,3,1))
# plot the map with the colors
plot(NSWmap,xlim=c(140,max(lsvro$longitude,na.rm=TRUE)),
  col=NSW_SEIFAcol)
legend_values<-c(0:10)
# generate the colors for the legend
SEIFAlegendcol<-color.scale(legend_values,
  c(1,0.9,0.8,0.8),c(0.8,0.9,0.9,0.8),c(0.8,0.8,0.9,1))
# stick a legend on the side
color.legend(151.8,-37.5,152.3,-34.5,as.character(legend_values),SEIFAlegendcol,
  align="rb",gradient="y")
title(main="Low speed vehicle run overs NSW - 1996-2010")
par(xpd=TRUE)
# put a title on the color legend
text(151.8,-34.3,"SEIFA decile",adj=0)
par(xpd=FALSE)
dev.off()

Jim



More information about the R-help mailing list