在R中创建制图 [英] Creating a Cartogram in R

查看:96
本文介绍了在R中创建制图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a cartogram in R to show the number of occurrences in each area of the UK.

我的数据当前如下所示:

My data currently looks like this:

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个观测值都有经度和纬度.到目前为止,我已经使用以下代码来尝试绘制地图:

I have the longitude and latitude for all of the 120 observations. So far I have used the following code in an attempt to produce a cartogram:

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"数据中的人口数据.

This produces the UK map but I am unsure how to use my own data for the cartogram as opposed to the population data in the 'wrld_simpl' data that the map is based on.

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

Does anyone have any experience of doing this or know another method to achieve the desired result? Thanks!

推荐答案

我假设您的数据是SpatialPointsDataFrame?您需要找到一个合适的shapefile(SpatialPolygonsDataFrame),其中每个UK区域都对应一个多边形.形状的好来源是 http://www.naturalearthdata.com/

I assume your data is a SpatialPointsDataFrame? You'll need to find a proper shapefile (SpatialPolygonsDataFrame) where each UK area corresponds to a polygon. A good source for shapes is http://www.naturalearthdata.com/

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

This is an example that should be similar to your case:

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)

这篇关于在R中创建制图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆