Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

To create a means of displaying information about the distribution of primary care providers, the population density and the average yearly Medicare expenditure per enrollee for each state, I would like to create a US map with the state boundaries indicated, and then display a tool tip box containing the data for that state when the cursor hovers over that state. CountryData["UnitedStates"] does not have a map that includes the state boundaries.

In addition, I would like to colorize the states to indicate the value of a given parameter for that state, such as age-adjusted average yearly cost per Medicare Enrollee, by reference to a color scale.

I have not been able to find the Mathematica 9 code for an interactive US map in the Mathematica demonstrations and I am interested in suggestions or working code that I could use. My level of proficiency with Mathematica 9 is somewhere between beginner and intermediate, so I appreciate the details that might not be necessary for explaining the solution to an experienced user.

share|improve this question
There are three or four questions here that have tried to make USA state maps because CountryData doesn't do them. Have a search through them and see if anything gives you a starting point. –  cormullion Jul 29 '13 at 21:31
How about this kml file? –  Sjoerd C. de Vries Jul 29 '13 at 21:33
@SjoerdC.deVries from this question perhaps? –  cormullion Jul 29 '13 at 21:35
@cormullion actually just from a Google search. About the 4th hit on the page. –  Sjoerd C. de Vries Jul 29 '13 at 21:38
@bobthechemist They are in there. Look for the STATES.state.name field name. –  Sjoerd C. de Vries Jul 30 '13 at 7:05

1 Answer 1

Part of the code taken from the question posted by @Cormullion

       crimestats =   WolframAlpha[   "Murder rate by state", {{"PropertyRanking:CrimeData", 1}, 
        crimestats =   MapThread[Rule, {crimestats[[All, 2]], #[[1]] & /@ crimestats[[All, 3]]}];
        usa = Import["http://code.google.com/apis/kml/documentation/us_states.kml","Data"];
        transform[s_] := StringTrim[s, Whitespace ~~ "(" ~~ ___ ~~ ")"];
        polygons =   Thread[transform["PlacemarkNames" /. usa[[1]]] -> ("Geometry" /. usa[[1]])];
        stateNames = Extract[stateNames,Position[stateNames, x_ /; x != "Alaska" && x !="Hawaii"]];
        origdata = (stateNames /. polygons)[[All, 2 ;;, 1]];
        values = stateNames /. crimestats;
        colors = Module[{val = values, min, max, int}, min = Min@val; 
                         max = Max@val; int = Floor[#/((max - min)/4)] + 1 & /@ val; 
                         ColorData[8, "ColorList"][[#]] & /@ int];
                       Table[{EdgeForm[Directive[Black, AbsoluteThickness[0.5]]], 
                       colors[[i]], Tooltip[Polygon[origdata[[i]]], 
                                         Column[{stateNames[[i]], values[[i]]}]]}, {i, Length@origdata}], 
                       AspectRatio -> Automatic, ImageSize -> Large]]
share|improve this answer
You're missing a preliminary definition for stateNamesstateNames = polygons[[All, 1]]; –  rm -rf Jul 30 '13 at 2:51
@rm-rf thanks for fixing. Skipped it during cut an paste. –  PatoCriollo Jul 30 '13 at 13:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.