Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cartogram + choropleth map in R

I've been using ggplot2 lately to create a bunch of choropleths. I was wondering if it was possible to use ggplot2 to create a map similar to this (from WorldMapper):

enter image description here

It's a choropleth where the shapefile polygons are distorted to represent relative population counts. I believe this is called a cartogram. They do this with a bunch of other variables. In the spirit of the Choropleth R Challenge, does anyone know how to do this using R?

like image 291
baha-kev Avatar asked Feb 16 '12 21:02

baha-kev


People also ask

What is the difference between a Cartogram and Choropleth?

Choropleth maps show us how much geographic area is affected. On some maps, this means that large regions where only a few people take up a lot of space on the screen. Population cartograms, on the other hand, drive more attention to populated areas.

What is Choropleth map in R?

A choropleth map displays divided geographical areas or regions that are coloured in relation to a numeric variable. This section provides many examples build with R. It focuses on the leaflet package for interactive versions, and the ggplot2 for static ones.


2 Answers

The cartogram package, available on CRAN, has the rubber sheet distortion-style cartogram you want.

like image 89
the_skua Avatar answered Sep 20 '22 12:09

the_skua


This might work:

You will need to pre-install FFTW. Rcartogram and getcartr you will need devtools.

Not sure how to do it in ggplot2, but here is another option.

Here I am using a shapefile from Thematic World Map, after you download and unzip, you will get a folder called TM_WORLD_BORDERS-0.3.

For the choropleth/cartogram, you would reshape first with the size, and shade with a feature:

library(rgdal)#needed for readOGR library(sp) #needed for spplot library(Rcartogram) library(getcartr) setwd("<your_directory_with_shapefile>") #to the file that has your shapefile and your information file (in this case, a csv named datR) #read shapefile #here i have a folder with a shapefile and a csv with columns as ISO (IS02 for convenience) country and value worldR <- readOGR(dsn = getwd(), layer= "TM_WORLD_BORDERS-0.3") # If reading a shapefile, the data source name (dsn= argument) is the folder (directory) where the shapefile is, and the layer is the name of the shapefile (without the .shp extension) #names(worldR) #note how here there are columns for ISO2 (which matches a column named 'iso' in datR and LAT\LON #[1] "FIPS"      "ISO2"      "ISO3"      "UN"        "NAME"      "AREA"      "POP2005"   "REGION"    "SUBREGION" "LON"       "LAT" proj4string(worldR) datR <- read.csv("datR.csv") #this is a file that has one column called 'score' and one column called size':     head(datR)   #  iso size     score   #1  AE  323 0.9819077   #2  AR  262 0.9591067   #3  AT 7481 0.9987313   #4  AU 5425 0.9837414   #5  BA   31 0.9871938   #6  BB   99 0.9715991    ##Merge SpatialPolygonsDataFrame with other info   map_dat <- merge(worldR, datR, by.x="ISO2",by.y="iso")   #remove coordinate reference system arguments   proj4string(map_dat) <- CRS(as.character(NA)) # from here https://github.com/chrisbrunsdon/getcartr/issues/1   world.carto <- quick.carto(map_dat, map_dat$size, blur = 0)   #plot(world.carto) #cartogram without anything   #spplot size, color   my.palette = c("#ff0000", "#ff8000", "#ffff00", "#bfff00","#00ff00") #red, orange, yellow, light green, dark green   spplot(world.carto, 'score', col.regions = my.palette, cuts = length(my.palette)-1,main="Choropleth of score and cartogram of size") 

This should give you a plot similar to this one:

enter image description here

I did this in a haste, let me know if it works

like image 29
erasmortg Avatar answered Sep 21 '22 12:09

erasmortg