[R-sig-Geo] Choropleth maps using matched csv/shp files

HallS sam_l_cruickshank at hotmail.com
Fri Aug 1 13:46:44 CEST 2014


as my data is confidential I've tried to make an anonymous data set. I
started with a shp file, and csv with a matching ID column and further data,
including "Col_2" which is a higher level region of an aggregate of the
shapefile region. At first I merged these but it jumbled the data so I
managed to do it via match() and then gUnionCascaded. I have now retrieved
aggregated polygons without the internal boundaries, but I'm a bit lost as
to how I begin to utilise these in a choropleth map. All the instructions I
read seem out of context and being new to R I don't know how to start. I've
had a few abortive efforts with ggplot2 but all failed. Any help or pointers
to useful articles would be appreciated. Dummy code below.

install.packages("sp")
install.packages("rgdal")
install.packages("maptools") #read in shape files
install.packages("rgeos") # needed by maptools
install.packages("RColorBrewer")
install.packages("scales")
install.packages("gridExtra")

library("sp")
library("rgdal")
library("maptools")
library("ggplot2")
library("rgeos")
library("RColorBrewer")
library("scales")
library("gridExtra")

##Shapefile from http://www.sharegeo.ac.uk/handle/10672/50

##Working matched code
setwd("...")
gc<-read.csv("...dummy.csv", header=TRUE) 
str(gc)

##'data.frame':  9 obs. of  3 variables:
##$ NAME : Factor w/ 9 levels "East Midlands",..: 4 5 3 8 9 7 1 6 2
##$ Col_2: int  1 1 1 1 3 3 3 3 3
##$ Data : int  8 7 6 5 2 0 1 5 0

#Shape File and match file 
frame<-readOGR(dsn=".../Regions",
               layer="Regions")
str(frame)
plot(frame)

frame at data=gc[match(frame at data[,"NAME"], gc[,"NAME"]),]

##Successfully merged, now to concatenate by territory

##Region 1 
#With internal boundaries
REGION1 <- frame[ which(frame$Col_2=='1'), ]
plot(REGION1)
#Joined so no internal boundaries
REGIONA <- gUnionCascaded(frame[ which(frame$Col_2=='1'), ])
plot(REGIONA)

##Region 3
#With internal boundaries
REGION3 <- frame[ which(frame$Col_2=='3'), ]
plot(REGION3)
#Joined so no internal boundaries
REGIONC <- gUnionCascaded(frame[ which(frame$Col_2=='3'), ])
plot(REGIONC)



--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Choropleth-maps-using-matched-csv-shp-files-tp7586853.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list