Skip to main content

visualization - Can GeoLabels be controlled?


I am making maps of stuff like population density and would like to add labels but GeoLabels makes it awfully busy. Here is an example. I would like to be able to control what gets written, the color, the location. In the following I plot the population density for each county in New Mexico. GeoLabels makes black labels that say stuff like: "Santa Fe County, New Mexico, United States" which is a tad superfluous. I'd prefer it just said "Sante Fe".


Side comment:


I've learned an enormous amount from this site and appreciate it. I'm wondering whether you wizards would've used MapThread the way I did in the call to GeoRegionValue or if you'd have done it some other way. I'm a little slow grocking functional programming.


nmcounties = AdministrativeDivisionData[Entity["AdministrativeDivision", {"NewMexico", "UnitedStates"}], "Subdivisions"];

nmpopdensity = AdministrativeDivisionData[#, "PopulationDensity"] & /@ nmcounties;
GeoRegionValuePlot[MapThread[Rule, {nmcounties, nmpopdensity}], ColorFunction -> ColorData["Rainbow"]]
GeoRegionValuePlot[MapThread[Rule, {nmcounties, nmpopdensity}], ColorFunction -> ColorData["Rainbow"], GeoLabels -> True]

Answer



This required a bit more work than I initially anticipated. To your second question first, per the documentation, GeoRegionValuePlot is quite versatile in what it accepts, and when you are working with a common, queryable propery, you should use the form


GeoRegionValuePlot[enityList -> "property"]

or


GeoRegionValuePlot[EntityClass -> "property"]


as it simplifies what you need to do considerably. So, you could use


GeoRegionValuePlot[
AdministrativeDivisionData[
Entity["AdministrativeDivision", {"NewMexico", "UnitedStates"}]
,
"Subdivisions"] -> "PopulationDensity"]

But, I'm partial to the more queryable form


GeoRegionValuePlot[
Entity["AdministrativeDivision", {_, "NewMexico", "UnitedStates"}}] ->

"PopulationDensity"]

On to the actual question. According to the GeoLabels documentation, the function form of GeoLabels accepts 4 parameters which are polygon, entity, position, and data. So, we need to use this function:


Function[{polygon, entity, pos, data}, 
{polygon, Black, Text[StringSplit[CommonName@entity, {",", " County"}][[1]], pos]}
]

which results in


enter image description here


Comments