2

I'm trying to plot a tidy map of English regions in R. I'm using code that runs successfully on other shapefiles, but when I run this the code executes without error, but then just presents a blank image. I can plot the blank map using traditional methods, and the tidy object looks fine to me, so I'm at a loss to understand what's going on.

This is the code I'm using:

library(tidyverse)
library(broom)
library(rgdal)

# load and check the English regions map
eng_reg_map <- readOGR(
  dsn = "./Region_(December_2015)_Boundaries"
)

plot(eng_reg_map)

# make a tidy map
tidy_eng_reg_map <- tidy(eng_reg_map, region = "rgn15nm")

# blank map
ggplot() +
  geom_polygon(data = tidy_eng_reg_map, aes(x = long, y = lat, group = group), fill = "white", colour = "black") +
  theme_void() +
  coord_map()

The shapefile I'm using is available here (you have to select the shapefile from the list - sorry I can't provide a direct link).

Does anyone know what the problem could be?

(Cross-posting on Stack Overflow)

Edit A friend got me halfway there: the problem is that latitude and longitude are in British-style Eastings and Northings, not global co-ordinates. Anyone know a way to convert them using rgdal?

1 Answer 1

3

The shapefile is in OSGB coordinates (basically a planar system of metres N and E of a point off the SW of Cornwall) and coord_map only works with lat-long degrees, it seems.

Transforming the sp object to lat-long produces results:

eng_reg_map=spTransform(eng_reg_map, "+init=epsg:4326")
[and then tidy etc]

BUT you might want to consider using the sf package for spatial data, in which case you can do nice maps without having to unwrap everything with this tidy function using either the tmap package or geom_sf.

rgdal and sp objects are not long for this world.

5
  • Thanks so much - worked like a charm! If you want to post the same answer on SO I'll upvote it (stackoverflow.com/questions/73621382/…) Sep 6 at 12:22
  • What I like about the tidy approach is that it makes it very transparent to join in metadata about the regions (which is what I ultimately want to display). Do you have any advice on doing that in a post-tidy world? Sep 6 at 12:23
  • 1
    With sf the polygons are single rows of a data frame, and you just do merge and join like its any other data frame. You are only having to do tidy here (which I think is like fortify) to get the data into an expanded form for geom_polygons, which repeats each polygon attribute for each polygon vertex and is extremely memory inefficient and slow.
    – Spacedman
    Sep 6 at 12:57
  • Thanks, got it now - I didn't realise I could do straightforward joins to these big geospatial objects - you've been a great help Sep 6 at 13:31
  • 1
    Work through geocompr.robinlovelace.net/index.html
    – Spacedman
    Sep 6 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.