Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Last active January 27, 2020 07:54
Show Gist options
  • Save matt-dray/1e776683ad0a7e2b42f4386f5670a475 to your computer and use it in GitHub Desktop.
Save matt-dray/1e776683ad0a7e2b42f4386f5670a475 to your computer and use it in GitHub Desktop.
Plot a GeoJSON of UK local authorities from the Office for National Statistics's Open Geography Portal using the {geojsonio}, {sf} and {ggplot2} packages
# Plotting a UK local authorities geojson with sf and ggplot2
# Feb 2019
# Matt Dray
# Packages needed
library(sf) # spatial features package for working with geographies
library(geojsonio) # for working with GeoJSON files
library(ggplot2) # for plotting
# Read GeoJSON from the web (local authorities)
# Get the URL from the ONS Open Geography Portal, for example:
# http://geoportal.statistics.gov.uk/datasets/local-authority-districts-december-2018-ultra-generalised-clipped-boundaries-uk
# Click the 'API' dropdown and copy the URL for the GeoJSON
lad_json <- geojson_read(
x = "https://opendata.arcgis.com/datasets/b2d5f4f8e9eb469bb22af910bdc1de22_3.geojson",
what = "sp" # spatial class
)
lad_sf <- st_as_sf(lad_json) # convert to {sf} format
head(lad_sf) # preview (notice geography is a listcol)
ggplot(lad_sf) + geom_sf() # plot with {ggplot2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment