Site icon R-bloggers

Plotting UK Maps with ggplot2

[This article was first published on https://pacha.dev/blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< !DOCTYPE html> < charset="utf-8"> < http-equiv="X-UA-Compatible" content="IE=edge"> < name="viewport" content="width=device-width, initial-scale=1.0"> pacha.dev/blog < !-- MathJax Configuration --> < !-- Smart header: libraries detected based on content --> < !-- File: /tmp/tmp.wApuhXjSkm/index.html -->
  • < !-- DEBUG: Found sourceCode --> < !-- Load custom CSS after any library CSS to ensure proper precedence -->
  • < header class="site-top">

    Mauricio “Pachá” Vargas Sepúlveda

    Blog with notes about R, Shiny, SQL, Python, Linux and C++. This blog is listed on R-Bloggers.

    HOME 🏠
    < !-- categories are printed below this--> < nav class="sidebar-nav">

    Categories

    < header id="title-block-header" class="quarto-title-block default">

    Plotting UK Maps with ggplot2

    Super early stage work but it will improve.
    Author

    Mauricio “Pachá” Vargas S.

    Published

    August 27, 2025

    Because of delays with my scholarship payment, if this post is useful to you I kindly ask a minimal donation on Buy Me a Coffee. It shall be used to continue my Open Source efforts. The full explanation is here: A Personal Message from an Open Source Contributor. If you need an R package or Shiny dashboard for your team, you can email me or inquiry on Fiverr.

    I got this question: How can you plot a map of the UK with ggplot2?

    The answer is that you need to read a GeoJSON or equivalent file with the sf package and then use the geom_sf() function from ggplot2.

    My apologies if my online search wasn’t good but all search engines in Canada became worse after Bill C-18 was passed. I found this maintained by Martin Chorley, I thought the data reading could be simplified, and so I started a small data package.

    You can install the development version of ukmaps like so to install v0.0.1:

    remotes::install_github("pachadotdev/ukmaps")

    And here is a simple example of how to use it with dplyr and ggplot2:

    library(ukmaps)
    library(dplyr)
    library(ggplot2)
    
    london_areas <- c(
      "City of London", "Barking and Dagenham", "Barnet", "Bexley", "Brent", "Bromley",
      "Camden", "Croydon", "Ealing", "Enfield", "Greenwich", "Hackney", "Hammersmith and Fulham",
      "Haringey", "Harrow", "Havering", "Hillingdon", "Hounslow", "Islington",
      "Kensington and Chelsea", "Kingston upon Thames", "Lambeth", "Lewisham", "Merton",
      "Newham", "Redbridge", "Richmond upon Thames", "Southwark", "Sutton",
      "Tower Hamlets", "Waltham Forest", "Wandsworth", "Westminster"
    )
    
    d <- administrative %>%
      filter(country == "England") %>%
      mutate(is_london = if_else(area_name %in% london_areas, "Yes", "No"))
    
    pal <- c("#165976", "#d04e66")
    
    ggplot(d) + 
      geom_sf(aes(fill = is_london, geometry = geometry), color = "white") +
      scale_fill_manual(values = pal, name = "Is this an administrative area of London?") +
      labs(title = "Map of England with Administrative Boundaries") +
      theme_minimal(base_size = 13)

    I will improve the datasets soon to allow filtering by city and more.

    I hope this is useful 🙂

    < footer>

    Loading…

  • < !-- Load shared sidebar -->
    To leave a comment for the author, please follow the link and comment on their blog: https://pacha.dev/blog.

    R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
    Exit mobile version