What would a minimal example for a choropleth map in Mathematica look like?
Graphics[
{
ColorData["ThermometerColors"][
Rescale[CountryData[#, "GDPPerCapita"], {100, 50000}]
] /. HoldPattern[Blend[___]] -> Yellow,
CountryData[#, "Polygon"]
} & /@
CountryData[]
]
And why the replacement? If there are no data of the required type for a given country CountryData returns Missing["NotAvailable"], causing ColorData
, and its underlying Blend
function not to return a specific RGB value. I replace this unevaluated Blend
with the color Yellow.
Just for reference, here some tips for working with ESRI Shapefiles. CountryData
does not provide county-level data for Germany (the administrative unit is called "Kreis"), which is why I wrote my own KreisData
function. The shape file I used can be downloaded for free, however there are terms of use to consider.
The KreisData
function is then created as follows:
shp = Import["C:/TEMP/map/VG2500/vg2500_krs.shp", "Data"];
polys = "Geometry" /. First[shp];
ags = "RS" /. ("LabeledData" /. First[shp]);
names = "GEN" /. ("LabeledData" /. First[shp]);
area = "SHAPE_AREA" /. ("LabeledData" /. First[shp]);
KreisDataRules =
Dispatch[MapThread[
Rule[#1, #2] &, {ags, Transpose[{polys, area, names}]}]];
KreisData[tag_String, "Polygon"] := First[tag /. KreisDataRules];
KreisData[tag_String, "Area"] := Part[tag /. KreisDataRules, 2];
KreisData[tag_String, "Name"] := Last[tag /. KreisDataRules];
KreisData[] := ags;
With this function, and the example code by Sjoerd C. de Vries, a map of Germany is created thus:
renderMap[scheme_String] :=
Graphics[{ColorData[scheme][
Rescale[KreisData[#, "Area"], {3.63067036816521*10^7,
3.08469540395003*10^9}]] /.
HoldPattern[Blend[___]] -> Yellow, KreisData[#, "Polygon"]} & /@
KreisData[]];
Manipulate[renderMap[s], {s, ColorData["Gradients"]}]
A throw on Minimal in the code golf sense:
Graphics@Function[f,{Hue[f[#,"Area"]/10^7],f[#,"Polygon"]} &/@ f[]]@CountryData