简体   繁体   English

Plot 一个整洁的 map 的英语区域

[英]Plot a tidy map of English regions

I'm trying to plot a tidy map of English regions in R.我正在尝试 plot 一个整洁的 map 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.我正在使用在其他 shapefile 上成功运行的代码,但是当我运行此代码时,代码执行时没有错误,但随后只显示一个空白图像。 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.我可以使用传统方法 plot 空白 map ,整洁的 object 在我看来很好,所以我不知道发生了什么。

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).我正在使用的 shapefile 可在此处获得(您必须 select 列表中的 shapefile - 抱歉,我无法提供直接链接)。

Does anyone know what the problem could be?有谁知道是什么问题?

(Cross-posting on GIS Stack Exchange) (GIS Stack Exchange 上的交叉发布)

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 ?有人知道使用rgdal转换它们的方法吗?

See below for an option using sf .有关使用sf的选项,请参见下文。 This is relevant for two reasons:这有两个原因:

  1. rgdal will be retired soon: rgdal即将退休:

Please note that rgdal will be retired by the end of 2023, plan transition to sf/stars/terra functions using GDAL and PROJ at your earliest convenience.请注意,rgdal 将于 2023 年底退役,计划尽早使用 GDAL 和 PROJ 过渡到 sf/stars/terra 功能。

I do actually also get an error running using readOGR (your code):实际上,我使用readOGR (您的代码)运行时也会出错:

Error in OGRSpatialRef(dsn, layer, morphFromESRI = morphFromESRI, dumpSRS = dumpSRS, : (converted from warning) Discarded datum Ordnance_Survey_of_Great_Britain_1936 in Proj4 definition: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs OGRSpatialRef 中的错误(dsn,层,morphFromESRI = morphFromESRI,dumpSRS = dumpSRS,:(从警告转换)Proj4 定义中丢弃的数据 Ordnance_Survey_of_Great_Britain_1936:+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs

  1. broom::tidy() is deprecated: broom::tidy()已弃用:

Error: (converted from warning) Data frame tidiers are deprecated and will be removed in an upcoming release of broom.错误:(从警告转换)数据框整理器已弃用,并将在即将发布的 broom 中删除。


library(ggplot2)
library(sf)

eng_reg_map <- 
  st_read("./Region_(December_2015)_Boundaries/Region_(December_2015)_Boundaries.shp")

eng_reg_map |>
  ggplot() +
  geom_sf(fill   = "white",
          colour = "black") +
  theme_void()

Output: Output:

在此处输入图像描述

Update:更新:

The import is also in this case tidy and allows for a join:在这种情况下,导入也是整洁的,并允许加入:

library(dplyr)
library(ggplot2)
library(sf)

eng_reg_map |> 
  left_join(tibble(rgn15nm = c("North East",
                               "North West",
                               "Yorkshire and The Humber",
                               "East Midlands",
                               "West Midlands",
                               "East of England",
                               "London", 
                               "South East",
                               "South West"),
                   pop = seq(1000, 9000, by = 1000))) |>
  ggplot(aes(fill = pop)) +
  geom_sf(colour = "black") +
  theme_void()

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM