Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would a minimal example for a choropleth map in Mathematica look like?

What would a minimal example for a choropleth map in Mathematica look like?

I can read in a ESRI Shapefile using Import, but do not know how to work with the imported result.

like image 892
Karsten W. Avatar asked Sep 19 '11 08:09

Karsten W.


3 Answers

Graphics[
   {
    ColorData["ThermometerColors"][
                            Rescale[CountryData[#, "GDPPerCapita"], {100, 50000}]
                                  ] /. HoldPattern[Blend[___]] -> Yellow, 
    CountryData[#, "Polygon"]
   } & /@
   CountryData[]
]

enter image description here

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.

like image 200
Sjoerd C. de Vries Avatar answered Oct 21 '22 05:10

Sjoerd C. de Vries


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"]}]

Result with "GrayTones" color scheme

like image 37
Karsten W. Avatar answered Oct 21 '22 05:10

Karsten W.


A throw on Minimal in the code golf sense:

Graphics@Function[f,{Hue[f[#,"Area"]/10^7],f[#,"Polygon"]} &/@ f[]]@CountryData

enter image description here

like image 33
Dr. belisarius Avatar answered Oct 21 '22 05:10

Dr. belisarius