首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R中创建地图

在R中创建地图
EN

Stack Overflow用户
提问于 2017-02-10 08:47:51
回答 1查看 1.2K关注 0票数 2

我正试图在R中制作一个地图,以显示英国每个地区发生的次数。

我目前的数据如下:

代码语言:javascript
复制
Area                           Occurences        lon      lat
1 Greater London East North UK        200 -0.0936496 51.43092
2     Lambeth and Southwark UK        16 -0.1178424 51.49351
3             Black Country UK        58 -2.0752861 52.52005
4                   Glasgow UK        45 -4.2518060 55.86424
5                     Leeds UK        331 -1.5490774 53.80076
6     Sth Herts or  Watford UK        210 -0.3903200 51.65649

我有所有120个观测的经度和纬度。到目前为止,我已经使用了以下代码试图生成一个制图图:

代码语言:javascript
复制
library(rgdal)
library(cartogram)
library(tmap)
library(maptools)

ukgrid = "+init=epsg:27700"

data(wrld_simpl)

afr <- wrld_simpl[wrld_simpl$NAME == "United Kingdom",]
afr <- spTransform(afr, CRS(ukgrid))

# construct cartogram
afrc <- cartogram(afr, "POP2005", itermax=5)

# plot it
tm_shape(afrc) + tm_fill("POP2005", style="jenks") + 
tm_borders() + tm_layout(frame=F)

这产生了英国地图,但我不知道如何使用我自己的数据为地图,而不是人口数据在'wrld_simpl‘数据的地图是基于。

是否有人有这样做的经验,或知道另一种方法,以达到预期的结果?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-11 16:30:09

我假设你的数据是SpatialPointsDataFrame?您需要找到一个正确的shapefile (SpatialPolygonsDataFrame),其中每个英国区域对应于一个多边形。形状的一个好来源是http://www.naturalearthdata.com/

这是一个与您的情况类似的示例:

代码语言:javascript
复制
library(rgeos)
library(sp)
library(maptools)
library(tmap)
library(tmaptools)
library(cartogram)

data(wrld_simpl)
data(metro)

## count occurences per polygon: in this case, the number of cities per country
x <- over(metro, wrld_simpl)
res <- table(x$ISO3)
dat <- data.frame(iso_a3=names(res), count=as.vector(res))

## add counts to polygon shape
wrld_simpl <- append_data(wrld_simpl, dat, key.shp = "ISO3", key.data = "iso_a3", ignore.na = TRUE)

## remove 0 counts
wrld_simpl_sel <- wrld_simpl[which(wrld_simpl$count>0), ]

## apply cartogram (doesn't result in a nice cartogram because the shape is too detailed and the counts are too few)
wrld_simpl_carto <- cartogram(wrld_simpl_sel, weight = "count", itermax = 1)

## plot it
qtm(wrld_simpl_carto)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42155028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档