library(extremevalues) library(ggplot2) library(maps) library(rgeos) library(maptools) library(colorspace) library(munsell) library(gridSVG) london <- list() tmp <- list() london$labour <- read.csv("2011-labour.csv", header=TRUE, skip=1) london$population <- read.csv("mid-2011-lsoa-quinary-estimates.csv", header=TRUE, skip=3) london$labour.lsoa <- droplevels(subset(london$labour, grepl("^E01", ZONEID))) tmp$nonNumericNames <- c("DISTLABEL", "X", "X.1", "ZONEID", "ZONELABEL") tmp$msoaNames <- sub(".$", "", london$labour.lsoa$ZONELABEL) tmp$data <- london$labour.lsoa[,!names(london$labour.lsoa) %in% tmp$nonNumericNames] london$labour.msoa <- aggregate(tmp$data, list(tmp$msoaNames), sum) tmp <- list() london$population.lsoa <- droplevels(subset(london$population, Area.Codes %in% levels(london$labour.lsoa$ZONEID))) tmp$nonNumericNames <- c("Area.Codes", "Area.Names", "X") tmp$msoaNames <- sub(".$", "", london$population.lsoa$X) tmp$data <- london$population.lsoa[,!(names(london$population.lsoa) %in% tmp$nonNumericNames)] london$population.msoa <- aggregate(tmp$data, list(tmp$msoaNames), sum) tmp <- list() london$employment.msoa <- cbind(london$labour.msoa, london$population.msoa[match(london$labour.msoa$Group.1, london$population.msoa$Group.1),]) tmp$notequals <- london$population.msoa$Group.1 != london$labour.msoa$Group.1 stopifnot(!sum(tmp$notequals)) tmp <- list() ggplot(reshape(london$population.msoa, varying=list(3:21), direction="long", idvar="Group.1")) + geom_line(aes(x=5*time-2.5, y=X0.4/All.Ages, group=Group.1), alpha=0.03, show_guide=FALSE) + scale_x_continuous(name="age / years") + scale_y_continuous(name="fraction") + theme_bw() ggplot(london$employment.msoa, aes(x=Full.time/All.Ages)) + geom_density() + theme_bw() london$employment.msoa$younger = rowSums(london$employment.msoa[,62:64]) london$employment.msoa$older = rowSums(london$employment.msoa[,75:80]) ggplot(london$employment.msoa, aes(x=Full.time/(All.Ages-younger-older))) + geom_density() + theme_bw() ggplot(london$employment.msoa, aes(x=older/All.Ages, y=younger/All.Ages)) + geom_point(alpha=0.1) + geom_density2d() + coord_equal() + theme_bw() tmp$outliers <- with(london$employment.msoa, getOutliers(log(Full.time/All.Ages/(1-Full.time/All.Ages)))) tmp$high <- london$employment.msoa$Group.1[tmp$outliers$iRight] tmp$low <-london$employment.msoa$Group.1[tmp$outliers$iLeft] london$outliers <- c(tmp$high, tmp$low) tmp <- list() london$borough <- readShapePoly("London_Borough_Excluding_MHW") london$msoa <- readShapePoly("MSOA_2011_London_gen_MHW") london$borough.fortified <- fortify(london$borough) london$msoa.fortified <- fortify(london$msoa) london$msoa.fortified$borough <- london$msoa@data[as.character(london$msoa.fortified$id), "LAD11NM"] london$msoa.fortified$zonelabel <- london$msoa@data[as.character(london$msoa.fortified$id), "MSOA11NM"] tmp$is <- match(london$msoa.fortified$zonelabel, london$employment.msoa$Group.1) london$msoa.fortified$fulltime <- london$employment.msoa$Full.time[tmp$is] london$msoa.fortified$allages <- london$employment.msoa$All.Ages[tmp$is] london$msoa.fortified$older <- london$employment.msoa$older[tmp$is] london$msoa.fortified$younger <- london$employment.msoa$younger[tmp$is] ggplot.london <- function(expr, subsetexpr=TRUE) { expr.limits <- range(eval(substitute(expr), london$msoa.fortified)) expr.aes <- do.call(aes, list(fill=substitute(expr), colour=substitute(expr))) london.data <- droplevels(do.call(subset, list(london$msoa.fortified, substitute(subsetexpr)))) (ggplot(london$borough.fortified) + coord_equal() + (theme_minimal() %+replace% theme(panel.background = element_rect(fill="black"), panel.grid.major = element_blank(), panel.grid.minor = element_blank())) + geom_map(map=london.data, data=london.data, modifyList(aes(map_id=id, x=long, y=lat), expr.aes), label="map") + scale_fill_gradient(limits=expr.limits, low=mnsl2hex(darker(rgb2mnsl(coords(hex2RGB("#13432B", TRUE))))), high=mnsl2hex(darker(rgb2mnsl(coords(hex2RGB("#56F7B1", TRUE)))))) + scale_colour_gradient(limits=expr.limits, low="#13432B", high="#56F7B1", guide=FALSE) + geom_map(map=london$borough.fortified, aes(map_id=id, x=long, y=lat), colour="white", fill=NA, size=0.1) ) } ggplot.london(fulltime/(allages-younger-older)) ggplot.london(fulltime/(allages-younger-older), zonelabel %in% london$outliers) ggplot.london(fulltime/(allages-younger-older), pmin(fulltime/(allages-younger-older) - min(fulltime/(allages-younger-older)), max(fulltime/(allages-younger-older)) - fulltime/(allages-younger-older)) < 0.1)