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

boB Rudis bob at rudis.net
Wed Jan 20 02:48:51 CET 2016


I think this might help you out a bit (it's probably worth taking the
time to look it over as it removes much of the redundant code you
had).

library(sp)
library(maptools)
library(rgeos)
library(rgdal)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(ggalt)

URL <- "http://census.edina.ac.uk/ukborders/easy_download/prebuilt/shape/England_gor_2011.zip"
fil <- basename(URL)
if (!file.exists(fil)) download.file(URL, fil)

fils <- unzip(fil)
shp <- grep("shp$", fils, value=TRUE)

region <- readOGR(shp, ogrListLayers(shp)[1], stringsAsFactors=FALSE)

# define new regions
region at data$Region <- c("East", "South &S.E", "North", "North", "Midlands",
                        "North", "SouthWest", "Midlands", "South & S.E")
colnames(region at data) <- c("code", "name","Region")

# dissolve polygons and simplify the shapes
regions <- unionSpatialPolygons(region, region at data$Region)
regions <- gSimplify(regions, 100, topologyPreserve=TRUE)
regions <- gBuffer(regions, byid=TRUE, width=0)

# coord_map and coord_proj will eventually work but this transformation
# will absolutely speed up the calculations

regions <- SpatialPolygonsDataFrame(spTransform(regions,
                                                CRS("+proj=longlat
+ellps=sphere +no_defs")),
                                    data.frame(Region=names(regions),
                                               row.names=names(regions),
                                               stringsAsFactors=FALSE))

eng_map <- fortify(regions, region="Region")

# new projection (good for the UK)
eng_proj <- "+proj=aea +lat_0=54.55635146083455 +lon_0=-3.076171875"

ggplot() +
  # base map
  geom_map(data=eng_map, map=eng_map,
           aes(x=long, y=lat, map_id=id),
           color=NA, fill=NA, size=0.5) +
  # sample fill of the regions
  geom_map(data=data.frame(id=unique(eng_map$id)), map=eng_map,
           aes(map_id=id, fill=id),
           color="white", size=0.15) +
  scale_fill_brewer(palette="Set2") +
  coord_proj(eng_proj) +
  theme_map()

NOTE: this requires the new ggplot2 (2.0)

On Tue, Jan 19, 2016 at 3:04 PM, Joshua Onyango via R-sig-Geo
<r-sig-geo at r-project.org> wrote:
> 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]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list