Creating a Cartogram in R

后端 未结 1 454
生来不讨喜
生来不讨喜 2021-01-21 10:44

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                          


        
相关标签:
1条回答
  • 2021-01-21 10:54

    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)
    
    0 讨论(0)
提交回复
热议问题