What is the best way to location vs intensity?

Using R studio and preferably ggplot what is the best way to plot the following data.

I have a data-set containing UK postcodes and a corresponding number. Eg.
PC Number
XXX XXX 123456

I would like to create a plot of the UK with the postcodes plotted and the plot intensity corresponds to the number value.

What would be the easiest way to do this?

UK post codes are somewhat tricky (they are not defined as polygons, but a set of addresses, not necessarily continuous).

You can download a shapefile of reconstructed boundaries - a good approximation for most use cases - from Open Door Logistics web pages.

The shapefile can be read in via {sf} package workflow; it will be a modified data.frame, so it should be easy to attach your data using one of the dplyr::*_join() family functions.

Recent versions of {ggplot2} support geom_sf(), which is a way to visualize sf objects, including polygons; just set the fill aesthetic to your value, and proceed like you would with an ordinary ggplot.

1 Like

Will the shapefile automatically correctly join to the postcodes? Or will I have to manually join the reconstructed boundaries to the postcodes?

This will of course depend on the way keys are set in your data, over which I have little control :slight_smile:

Consider this example, built on leaflet (as I wanted to show the basemap):

library(sf)

data <- data.frame(name = "CB2 1") # Cambridge university - historic area

raw <- st_read("./src/Sectors.shp", # from https://www.opendoorlogistics.com/
               stringsAsFactors = F)

joined <- dplyr::inner_join(raw, data)

library(leaflet)

leaflet(joined) %>% 
  addProviderTiles("Stamen.Toner") %>% 
  addPolygons(color = "cornflowerblue")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.