library(maptools)
library(cartogram)
library(rgdal)
data(wrld_simpl)
# Remove uninhabited regions
afr <- spTransform(wrld_simpl[wrld_simpl$REGION==2 & wrld_simpl$POP2005 > 0,],
CRS("+init=epsg:3395"))
# Create cartogram
afr_nc <- cartogram_ncont(afr, "POP2005")
# Plot
plot(afr)
plot(afr_nc, add = TRUE, col = 'red')
# Same with sf objects
library(sf)
afr_sf = st_as_sf(afr)
afr_sf_nc <- cartogram_ncont(afr_sf, "POP2005")
plot(st_geometry(afr_sf))
plot(st_geometry(afr_sf_nc), add = TRUE, col = 'red')
Run the code above in your browser using DataLab