[R-sig-Geo] Merging regions in a map & plotting values

Joshua Onyango jaonyango at yahoo.com
Tue Jan 19 21:04:57 CET 2016


HelloI'm new into creating maps in R so any help would be great.I have read a few articles on the internet - mainly blogs on mapping in R which I have tried to follow with no much success. Basically I want to merge merge some English regions from 8 to 5 then show information that relates to disease prevalence, temperature etc
Here is what I have tried but sound like this cant run due to computer memory or must be doing following a wrong approach. 

 
> #Getting theshapefile into working directory

> download.file("http://census.edina.ac.uk/ukborders/easy_download/prebuilt/shape/England_gor_2011.zip",

               destfile ="lad-region-lookup.zip")> #inzipping theshapefile folder

>unzip("lad-region-lookup.zip", exdir = ".")

> #reading theshapefile in r

> region <-readOGR(".", "England_gor_2011")

OGR data source withdriver: ESRI Shapefile 

Source:".", layer: "England_gor_2011"

with 9 features

It has 2 fields

> # Check theshapefile has loaded correctly

> plot(region)

> 

> lu <-data.frame()

> lu <-rbind(lu, region at data)

> lu$CODE <-as.character(lu$CODE)

> lu$NAME <-as.character(lu$NAME)

> lu$Region <-NA

> name=c("East", "South &S.E","North","North","Midlands","North","SouthWest","Midlands","South & S.E")

> lu$Region <-name

> # Merge lu(LookUp) into polygons,

> region at data$CODE<- as.character(region at data$CODE)

> region at data<- full_join(region at data, lu, by = "CODE")

> # Tidy mergeddata

> region at data<- select(region at data, -NAME.x)

>colnames(region at data) <- c("code", "name","Region")

> 

> # Ensureshapefile row.names and polygon IDs are sensible

>row.names(region) <- row.names(region at data)

> region <-spChFIDs(region, row.names(region))

> 

> # Now thedissolve

> region <-gUnaryUnion(region, id = region at data$Region)

> 

> # If you want torecreate an object with a data frame

> # make sure row namesmatch

>row.names(region) <- as.character(1:length(region))

> 

> # Extract thedata you want (the larger geography)

> lu <-unique(lu$Region)

> lu <-as.data.frame(lu)

> colnames(lu)<- "Region"  # your datawill probably have more than 1 row!

> 

> # And add thedata back in

> region <-SpatialPolygonsDataFrame(region, lu)

> region2 <-merge(region at data, data, by="Region")

> dat2=fortify(region,region="Region")

>names(dat2)[names(dat2)=="id"]<-"Region"

> dat <-merge(dat2, region2, by="Region")

Error: cannotallocate vector of size 904.9 Mb

In addition:Warning messages:

1: In`[.data.frame`(x, c(m$xi, if (all.x) m$x.alone), c(by.x,seq_len(ncx)[-by.x]),  :

  Reached total allocation of 4016Mb: seehelp(memory.size)


 

Any simpler approach will be highly appreciated.
Thanks
Josh
	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list