Creating a Cartogram in R

百般思念 提交于 2019-12-02 05:45:55

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!