2017-02-10 37 views
1

我正在試圖在R中製作一個Cartogram以顯示英國每個區域中出現的次數。在R中創建一個Cartogram

我的數據目前看起來是這樣的:

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個觀察的經度和緯度。到目前爲止,我已經用下面的代碼,試圖產生一種示意地圖:

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'地圖所基於的數據。

有沒有人有這樣做的經驗或知道另一種方法來實現預期的效果?謝謝!

回答

1

我假設您的數據是SpatialPointsDataFrame?你需要找到一個合適的shapefile(SpatialPolygonsDataFrame),其中每個UK區域對應一個多邊形。一個很好的來源,形狀爲http://www.naturalearthdata.com/

這是應該是類似的情況的一個例子:

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) 
+0

謝謝 - 非常有幫助! :) – George