Sunday, November 15, 2015

Mathematica 10 geo mapping

I needed to plot some data based on the state code that was contained in my data. But to useGeoRegionValuePlot I need to have a mapping between my state abbreviation and the full state name.
To use GeoRegionValuePlot you need data in the form:
GeoRegionValuePlot[{Entity["AdministrativeDivision", {"Texas", "UnitedStates}] -> 10, 
  Entity["AdministrativeDivision", {"Virginia", "UnitedStates}]->20}]
You can see this form of input if you take one of the GeoRegionValuePlot examples and convert the cell to InputForm so its not displaying in StandardForm. InputForm gives you the textual form of what is displayed in the input cell as if you typed it in yourself.
Hence, given a set of data like:
yourdata = <| "TX"->10, "VA"->20|>
you can use the following to generate the mappping:
(* Create a map from state abbreviations to state full names *)
abbrevmapping = 
 Association @@ ((AdministrativeDivisionData[{#, "UnitedStates"}, 
        "StateAbbreviation"] -> #) & /@ 
    CountryData[Entity["Country", "UnitedStates"], "Regions"])
And then your plotting is as easy as:
counts = 
  KeyMap[Entity["AdministrativeDivision", {abbrevmapping[#], "UnitedStates"}] &, yourdata] // Normal;
GeoRegionValuePlot[counts]       

No comments:

Post a Comment