Hi,
I have following challenge.
I have a list of shops which scores should be shown on the UK map (high in dark colour and low in a light one):
postcodeinfo <- data.frame(
stringsAsFactors = FALSE,
PostCode = c("BB7","BB1","PR5","CA3",
"DG12","CA15","CW1","ST7","CW9","PR4","PR2","PR7",
"ST17","ST18","ST16","ST3","ST4","TF9"),
Score = c(0.717647058823529,
0.761194029850746,0.4375,0.777777777777778,0.764705882352941,
0.727272727272727,0.807017543859649,0.830769230769231,
0.868421052631579,0.819672131147541,0.732673267326733,
0.741379310344828,0.811764705882353,0.761194029850746,
0.844827586206897,0.68,0.855263157894737,0.857142857142857),
Shop = c("AAA","AAA","AAA","BBB",
"BBB","BBB","CCC","CCC","CCC","DDD","DDD","DDD",
"EEE","EEE","EEE","FFF","FFF","FFF"),
Cor1 = c(53.874,53.756,53.731,54.907,
54.99,54.712,53.103,53.088,53.259,53.754,53.778,
53.645,52.789,52.814,52.813,52.981,52.995,52.897),
Cor2 = c(-2.386,-2.462,-2.656,-2.939,
-3.251,-3.481,-2.434,-2.265,-2.501,-2.833,-2.708,
-2.652,-2.099,-2.081,-2.118,-2.122,-2.183,-2.469),
PostCode2digits = c("BB","BB","PR","CA","DG",
"CA","CW","ST","CW","PR","PR","PR","ST","ST","ST",
"ST","ST","TF"),
Region = c("North West","North West",
"North West","North West","Scotland","North West",
"North West","West Midlands","North West","North West",
"North West","North West","West Midlands","West Midlands",
"West Midlands","West Midlands","West Midlands",
"West Midlands"),
City = c("Blackburn","Blackburn",
"Preston","Carlisle","Dumfries","Carlisle","Crewe",
"Stoke on Trent","Crewe","Preston","Preston","Preston",
"Stoke on Trent","Stoke on Trent","Stoke on Trent",
"Stoke on Trent","Stoke on Trent","Telford")
)
As you can see, all geo info is already there. Now I need to colour all postcodes on the map.
Additionally, I would like to add a pin to these specific postcodes (best shops):
BB1 3HT (AAA group)
BB1 2DY (AAA group)
CA3 0HA (BBB group)
Is is doable?
My previous work cannot be applied as I used more general, two digit post codes
library(ggplot2)
library(rgdal)
library(maptools)
if (!require(gpclib)) install.packages("gpclib", type="source");library(gpclib)
gpclibPermit() # Gives maptool permisssion to use gpclib
#Download UK postcode polygon Shapefile
download.file(
"http://www.opendoorlogistics.com/wp-content/uploads/Data/UK-postcode-boundaries-Jan-2015.zip",
"postal_shapefile"
)
unzip("postal_shapefile")
# Read the downloaded Shapefile from disk
postal <- readOGR(dsn="C:/Users/.../GeoInfo/UK-postcode-boundaries-Jan-2015/Distribution", layer="Areas")
# Assign each "region" an unique id
postal.count <- nrow(postal@data)
postal@data$id <- 1:postal.count
# Transform SpatialPolygonsDataFrame to regular data.frame in ggplot format
postal.fort <- ggplot2::fortify(postal, region='id')
# Import data for each postal area
df <- read_excel("C:/Users/.../Scores by Post Code.xlsx", sheet = "Sheet1")
# Add "region" id to frequency data
df <- merge(df, postal@data, by.x="postal_area_code", by.y="name")
# Merge frequency data onto geogrphical postal polygons
postal.fort <- merge(postal.fort, df, by="id", all.x=T, all.y=F)
postal.fort <- postal.fort[order(postal.fort$order),] # Reordering since ggplot expect data.fram in same order as "order" column
head(postal.fort)
ggplot(postal.fort) +
geom_polygon(aes(x = long, y = lat, group = group, fill=Score), colour='white') +
scale_fill_gradient(low = "green", high = "red") +
labs(title = "Total Score by region",
fill = "Value (£)") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5, size = 20),
axis.title = element_blank(),
plot.background = element_blank(),
legend.position = c(0.95, 0.70),
plot.margin = margin(5, 10, 5, 10))