如何在 R 中构建英国邮政编码区域地图?

Posted

技术标签:

【中文标题】如何在 R 中构建英国邮政编码区域地图?【英文标题】:How do I build a UK postcode area map in R? 【发布时间】:2017-03-30 12:19:52 【问题描述】:

输入

我以这种形式按英国邮政编码的前 2 个字母计算数据:

Postcode Count
BD       45
DE       123
L8       90

期望的输出

我想使用基于 2 个字母邮政编码的 shapefile 创建一个邮政编码地图,并根据计数为地图着色,类似于:

我的问题是,我怎样才能在 R 中生成这样的地图?

【问题讨论】:

【参考方案1】:

使用相同的 Shapefile 但使用 ggplot 绘制的类似结果:

library(ggplot2)
library(rgdal)
library(maptools)
if (!require(gpclib)) install.packages("gpclib", type="source");library(gpclib)
gpclibPermit()  # Gives maptool permisssion to use gpclib

# Download UK postcode polygon Shapefile
download.file(
  "http://www.opendoorlogistics.com/wp-content/uploads/Data/UK-postcode-boundaries-Jan-2015.zip",
  "postal_shapefile"
)
unzip("postal_shapefile")

# Read the downloaded Shapefile from disk
postal <- maptools::readShapeSpatial("./Distribution/Areas")

# Assign each "region" an unique id
postal.count <- nrow(postal@data)
postal@data$id <- 1:postal.count

# Transform SpatialPolygonsDataFrame to regular data.frame in ggplot format
postal.fort <- ggplot2::fortify(postal, region='id')

# Generate random data for each postal area
some_area_codes <- c("AB","AL","B","BA","BB","BD","BH","BL","BN","BR","BS","CA","CB","CF","CH","CM","CO","CR","CT","CV","CW","DA","DD","DE","DG","DH","DL","DN","DT","DY","E","EC","EH","EN","EX","FK","FY","G","GL","GU","HA","HD","HG","HP","HR","HS","HU","HX","IG","IP","IV","KA","KT","KW","KY","L","LA","LD","LE","LL","LN","LS","LU","M","ME","MK","ML","N","NE","NG","NN","NP","NR","NW","OL","OX","PA","PE","PH","PL","PO","PR","RG","RH","RM","S","SA","SE","SG","SK","SL","SM","SN","SO","SP","SR","SS","ST","SW","SY","TA","TD","TF","TN","TQ","TR","TS","TW","UB","W","WA","WC","WD","WF","WN","WR","WS","WV","YO","ZE","BT","GY","IM","JE")
df <- data.frame(postal_area_code=some_area_codes, freq=sample.int(100, length(some_area_codes), replace=TRUE))

# Add "region" id to frequency data
df <- merge(df, postal@data, by.x="postal_area_code", by.y="name")

# Merge frequency data onto geogrphical postal polygons
postal.fort <- merge(postal.fort,  df, by="id", all.x=T, all.y=F)
postal.fort <- postal.fort[order(postal.fort$order),] # Reordering since ggplot expect data.fram in same order as "order" column

ggplot(postal.fort) + 
  geom_polygon(aes(x = long, y = lat, group = group, fill=freq), colour='white') + 
  coord_fixed()

【讨论】:

【参考方案2】:

这是你的想法吗?确保每个邮政编码都有一个值,并且包含每个邮政编码名称的列称为name

library(tidyverse)
library(maptools)
library(raster)
library(plotrix)

# Generate dummy data
dta <-
  tibble(
    name = c(
      "AB",
      "AL",
      "B",
      "BA",
      "BB",
      "BD",
      "BH",
      "BL",
      "BN",
      "BR",
      "BS",
      "CA",
      "CB",
      "CF",
      "CH",
      "CM",
      "CO",
      "CR",
      "CT",
      "CV",
      "CW",
      "DA",
      "DD",
      "DE",
      "DG",
      "DH",
      "DL",
      "DN",
      "DT",
      "DY",
      "E",
      "EC",
      "EH",
      "EN",
      "EX",
      "FK",
      "FY",
      "G",
      "GL",
      "GU",
      "HA",
      "HD",
      "HG",
      "HP",
      "HR",
      "HS",
      "HU",
      "HX",
      "IG",
      "IP",
      "IV",
      "KA",
      "KT",
      "KW",
      "KY",
      "L",
      "LA",
      "LD",
      "LE",
      "LL",
      "LN",
      "LS",
      "LU",
      "M",
      "ME",
      "MK",
      "ML",
      "N",
      "NE",
      "NG",
      "NN",
      "NP",
      "NR",
      "NW",
      "OL",
      "OX",
      "PA",
      "PE",
      "PH",
      "PL",
      "PO",
      "PR",
      "RG",
      "RH",
      "RM",
      "S",
      "SA",
      "SE",
      "SG",
      "SK",
      "SL",
      "SM",
      "SN",
      "SO",
      "SP",
      "SR",
      "SS",
      "ST",
      "SW",
      "SY",
      "TA",
      "TD",
      "TF",
      "TN",
      "TQ",
      "TR",
      "TS",
      "TW",
      "UB",
      "W",
      "WA",
      "WC",
      "WD",
      "WF",
      "WN",
      "WR",
      "WS",
      "WV",
      "YO",
      "ZE",
      "BT",
      "GY",
      "IM",
      "JE"
    ),
    value = rnorm(124)
  )

# Make sure your postal codes are stored in a column called name
# Example:
# dta <- rename(dta, name = name)

# OPTIONAL: Depending on your data, you may need to rescale it for the color ramp to work
dta$value <- rescale(dta$value, newrange = c(0, 1))

# Download a shapefile of postal codes into your working directory
download.file(
  "http://www.opendoorlogistics.com/wp-content/uploads/Data/UK-postcode-boundaries-Jan-2015.zip",
  "postal_shapefile"
)

# Unzip the shapefile
unzip("postal_shapefile")

# Read the shapefile
postal <- readShapeSpatial("./Distribution/Areas")

# Join your data to the shapefile
postal <- raster::merge(postal, dta, by = "name")

# Use the gray function to determine the proper black-and-white color for each postal code
plot(postal, col = gray(postal$value))

【讨论】:

以上是关于如何在 R 中构建英国邮政编码区域地图?的主要内容,如果未能解决你的问题,请参考以下文章

谷歌是如何得到地图上邮政编码的大纲的?

谷歌地图区域边界

带有 SimpleMaps 的英国邮政编码

如何在谷歌地图中锁定国家

谷歌地图检测到一个国家特定区域内的点击

如何使用谷歌地图 API 获取阿联酋国家/地区的邮政编码区域?