Hi, I have this sample where I created a map showing one of the values based on a post code:
region.scores <- data.frame(
stringsAsFactors = FALSE,
NAME_3 = c("BB7","BB1","BB12","BB6",
"BB9","BB3","BB10","PR7","DG1","CA11","CA4","DG2",
"CA13","DG11","ST7","CW9","CW4","CW6","ST5","CW3",
"SY13","WA16","PR4","PR2","PR7","PR1","FY8","PR3",
"PR5","PR25","FY6","FY5","PR6","FY4","PR26","PR9",
"L40","PR8","LA1","FY3","WN6","ST17","ST18",
"ST16","ST15","WS15","WS12","ST19","WS11","TF10",
"ST20","ST21","ST14","ST12","ST15"),
RetentionA = c(0.717647058823529,
0.761194029850746,0.69811320754717,0.764705882352941,
0.606060606060606,0.628571428571429,0.827586206896552,
0.722222222222222,0.822222222222222,0.693877551020408,
0.714285714285714,0.631578947368421,0.695652173913043,
0.736842105263158,0.830769230769231,0.868421052631579,
0.740740740740741,0.789473684210526,0.730769230769231,
0.705882352941177,0.769230769230769,0.8125,0.819672131147541,
0.732673267326733,0.741379310344828,0.673076923076923,
0.689655172413793,0.84,0.833333333333333,0.682926829268293,
0.787878787878788,0.810810810810811,0.857142857142857,
0.64,0.666666666666667,0.695652173913043,0.8,0.5625,
0.733333333333333,0.823529411764706,0.75,
0.811764705882353,0.761194029850746,0.844827586206897,
0.736842105263158,0.6,0.769230769230769,0.576923076923077,
0.708333333333333,0.92,0.7,0.785714285714286,
0.764705882352941,0.684210526315789,0.733333333333333),
RetentionB = c(0.465648854961832,
0.467889908256881,0.493333333333333,0.5,0.384615384615385,
0.448979591836735,0.571428571428571,0.393939393939394,
0.536231884057971,0.5,0.540540540540541,0.342857142857143,
0.470588235294118,0.451612903225806,0.650602409638554,
0.55,0.526315789473684,0.405405405405405,
0.558823529411765,0.444444444444444,0.416666666666667,
0.565217391304348,0.581395348837209,0.471337579617834,
0.425742574257426,0.406976744186047,0.512820512820513,0.56,
0.533333333333333,0.451612903225806,0.448275862068966,0.6,0.5,
0.4,0.315789473684211,0.432432432432432,0.375,
0.28125,0.354838709677419,0.482758620689655,
0.428571428571429,0.547619047619048,0.5,0.521276595744681,
0.518518518518518,0.288461538461538,0.408163265306122,
0.319148936170213,0.485714285714286,0.657142857142857,0.4375,
0.733333333333333,0.541666666666667,0.481481481481481,
0.523809523809524),
Dealer = c("Blackburn","Blackburn",
"Blackburn","Blackburn","Blackburn","Blackburn",
"Blackburn","Blackburn","Carlisle","Carlisle","Carlisle",
"Carlisle","Carlisle","Carlisle","Crewe","Crewe","Crewe",
"Crewe","Crewe","Crewe","Crewe","Crewe","Preston",
"Preston","Preston","Preston","Preston","Preston",
"Preston","Preston","Preston","Preston","Preston",
"Preston","Preston","Preston","Preston","Preston",
"Preston","Preston","Preston","Stafford","Stafford",
"Stafford","Stafford","Stafford","Stafford","Stafford",
"Stafford","Stafford","Stafford","Stafford","Stafford",
"Stoke","Stoke"),
Cor1 = c(53.874,53.756,53.803,53.792,
53.837,53.698,53.799,53.645,55.073,54.664,54.847,
55.065,54.66,55.114,53.088,53.259,53.207,53.162,
53.015,53,52.952,53.303,53.754,53.778,53.645,53.757,
53.751,53.869,53.731,53.694,53.868,53.874,53.667,
53.791,53.687,53.66,53.602,53.626,54.046,53.821,
53.574,52.789,52.814,52.813,52.9,52.758,52.706,
52.713,52.689,52.769,52.798,52.868,52.903,52.948,52.9),
Cor2 = c(-2.386,-2.462,-2.29,-2.417,
-2.214,-2.465,-2.219,-2.652,-3.58,-2.786,-2.833,
-3.657,-3.366,-3.336,-2.265,-2.501,-2.347,-2.666,
-2.237,-2.406,-2.689,-2.371,-2.833,-2.708,-2.652,
-2.701,-3.002,-2.716,-2.656,-2.693,-2.981,-3.021,
-2.609,-3.028,-2.74,-2.968,-2.841,-3.01,-2.799,-3.021,
-2.669,-2.099,-2.081,-2.118,-2.148,-1.917,-1.999,
-2.147,-2.017,-2.385,-2.263,-2.259,-1.868,-2.171,
-2.148),
PostCode2digits = c("BB","BB","BB","BB","BB",
"BB","BB","PR","DG","CA","CA","DG","CA","DG","ST",
"CW","CW","CW","ST","CW","SY","WA","PR","PR",
"PR","PR","FY","PR","PR","PR","FY","FY","PR","FY",
"PR","PR","L4","PR","LA","FY","WN","ST","ST",
"ST","ST","WS","WS","ST","WS","TF","ST","ST","ST",
"ST","ST"),
Region = c("North West","North West",
"North West","North West","North West","North West",
"North West","North West","Scotland","North West",
"North West","Scotland","North West","Scotland",
"West Midlands","North West","North West","North West",
"West Midlands","North West","Yorkshire and the Humber",
"North West","North West","North West","North West",
"North West","North West","North West","North West",
"North West","North West","North West","North West",
"North West","North West","North West","North West","North West",
"North West","North West","North West",
"West Midlands","West Midlands","West Midlands","West Midlands",
"West Midlands","West Midlands","West Midlands",
"West Midlands","West Midlands","West Midlands",
"West Midlands","West Midlands","West Midlands","West Midlands"),
City = c("Blackburn","Blackburn",
"Blackburn","Blackburn","Blackburn","Blackburn",
"Blackburn","Preston","Dumfries","Carlisle","Carlisle",
"Dumfries","Carlisle","Dumfries","Stoke on Trent","Crewe",
"Crewe","Crewe","Stoke on Trent","Crewe","Sheffield",
"Warrington","Preston","Preston","Preston","Preston",
"Blackpool","Preston","Preston","Preston",
"Blackpool","Blackpool","Preston","Blackpool","Preston",
"Preston","Liverpool","Preston","Lancaster","Blackpool",
"Wigan","Stoke on Trent","Stoke on Trent",
"Stoke on Trent","Stoke on Trent","Walsall","Walsall",
"Stoke on Trent","Walsall","Telford","Stoke on Trent",
"Stoke on Trent","Stoke on Trent","Stoke on Trent",
"Stoke on Trent")
)
region.scores
library(tidyverse)
library(sf)
#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")
uk <- read_sf('Distribution/Districts.shp')
uk.all <- left_join(uk, region.scores, by = c('name'='NAME_3'))
uk.all
uk.all %>% filter(!is.na(RetentionA))
ggplot() +
geom_sf(data = uk.all, alpha = 0.3) +
geom_sf(data = uk.all, aes(fill = RetentionA)) +
scale_fill_gradient(low = "red", high = "green") +
labs(title = "Retention Scores by region",
fill = "Option A") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5, size = 20),
axis.title = element_blank(),
plot.background = element_blank(),
legend.position = c(0.10, 0.20),
plot.margin = margin(5, 0, 5, 0))
rs_sf <- st_as_sf(region.scores, coords=c('Cor2','Cor1'), crs=st_crs(uk.all))
ggshopA <- ggplot()+
geom_sf(data=uk.all,mapping=aes(fill=RetentionA)) +
geom_sf(data=rs_sf,mapping=aes(shape=Dealer))+
scale_fill_gradient(low = "red", high = "green") +
coord_sf(xlim=c(-4,-2),ylim=c(52.6,55.15))
ggshopA
Is it possible to apply colours to bigger areas (UK Regions)?
I guess I should calculate averages for all four regions (North West, Scotland, West Midlands and Yorkshire and the Humber but how can I change a map to show colours for each region? Basically, the entire Scotland should be red as the average is the lowest, the entire Yorkshire and the Humber should be green as the average is the highest.
Is it easy to change the existing code?