Data Art: London mapped in R

By Data Tricks, 17 October 2017

Last month we published a tutorial on creating a unique map of London in R using the ggplot2 package. We’ve been playing around with colours to produce this rather patriotic and artistic looking alternative map. See below for the full code. Be sure to read our tutorial which has links to the postcode data CSVs.

Full code:

library(ggplot2)
library(plyr)
filenames <- c("e.csv", "n.csv", "nw.csv", "se.csv", "sw.csv", "w.csv", "wc.csv", "ec.csv")
postcodes <- do.call(rbind,lapply(filenames,read.csv,header=FALSE))
postcodes <- postcodes[-which(postcodes$V3 == 0),]

#CREATE THE FLAG COORDINATES AND JOIN TO POSTCODES DATAFRAME
diagonal1white <- data.frame(V3=c(514709:548505), Diagonal1white=seq(from=195461, to=169935, length.out=33797))
diagonal1red <- data.frame(V3=c(514709:548505), Diagonal1red=seq(from=195461, to=169935, length.out=33797))
diagonal2white <- data.frame(V3=c(514709:548505), Diagonal2white=seq(from=169935, to=195461, length.out=33797))
diagonal2red <- data.frame(V3=c(514709:548505), Diagonal2red=seq(from=169935, to=195461, length.out=33797))
postcodes <- join_all(list(postcodes,diagonal1white,diagonal1red,diagonal2white,diagonal2red), by="V3", type="full")

#ADD COLOURS TO POSTCODES DATAFRAME
#Blue background
postcodes$Colour <- "Blue"
#Diagonals
postcodes$Colour <- ifelse(abs(postcodes$V4 - postcodes$Diagonal1white)<2500, "White", postcodes$Colour)
postcodes$Colour <- ifelse((postcodes$V3>531607 & (postcodes$V4 - postcodes$Diagonal1red)<1500 & (postcodes$V4 - postcodes$Diagonal1red>-500)), "Red", postcodes$Colour)
postcodes$Colour <- ifelse((postcodes$V3<531607 & (postcodes$V4 - postcodes$Diagonal1red)<500 & (postcodes$V4 - postcodes$Diagonal1red>-1500)), "Red", postcodes$Colour)
postcodes$Colour <- ifelse(abs(postcodes$V4 - postcodes$Diagonal2white)<2500, "White", postcodes$Colour)
postcodes$Colour <- ifelse((postcodes$V3>531607 & (postcodes$V4 - postcodes$Diagonal2red)<1500 & (postcodes$V4 - postcodes$Diagonal2red>-500)), "Red", postcodes$Colour)
postcodes$Colour <- ifelse((postcodes$V3<531607 & (postcodes$V4 - postcodes$Diagonal2red)<500 & (postcodes$V4 - postcodes$Diagonal2red>-1500)), "Red", postcodes$Colour)
#Cross
postcodes$Colour <- ifelse(abs(postcodes$V3 - 531607)<2500, "White",postcodes$Colour)
postcodes$Colour <- ifelse(abs(postcodes$V4 - 182698)<3000, "White",postcodes$Colour)
postcodes$Colour <- ifelse(abs(postcodes$V3 - 531607)<1500, "Red",postcodes$Colour)
postcodes$Colour <- ifelse(abs(postcodes$V4 - 182698)<2000, "Red",postcodes$Colour)

#PLOT THE MAP
gg <- ggplot()
gg <- gg + geom_point(data=postcodes[which(postcodes$Colour == "White"),], aes(V3,V4), colour="#ffffff", size=0.2, alpha=0.3)
gg <- gg + geom_point(data=postcodes[which(postcodes$Colour == "Red"),], aes(V3,V4), colour="#c80000", size=0.2, alpha=0.3)
gg <- gg + geom_point(data=postcodes[which(postcodes$Colour == "Blue"),], aes(V3,V4), colour="#0000ff", size=0.2, alpha=0.3)
gg <- gg + coord_fixed(1)
gg <- gg + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
gg <- gg + theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank())
gg <- gg + theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank())
gg <- gg + theme(panel.background = element_rect(fill = 'grey'))
print(gg)

Tags: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Please note that your first comment on this site will be moderated, after which you will be able to comment freely.