Bug introduced in 10.0 and persisting through 10.3 or later
Bug behavior is version dependent
When I'm plotting data with GeoRegionValuePlot
, I'm getting some strange coloring errors:
The grid included shows the top 10 countries with arable land per population. However, Australia is colored the as if it were one of the least arable.
I thought it may be due to units, so I tried messing around with TargetUnits
to no luck.
I figure that it's due to the fact that the data is being stored as exponents (i.e., the US's arable land/population is 4.81068*10^-3, so that's why it would be colored as if it were higher), but when I made all data have the same exponent, that didn't change anything either.
Any ideas?
Code, for reference:
(* Utility functions *)
removeMissingData[ data_ ] := DeleteCases[ data, _?( ! FreeQ[ #, _Missing ] & ) ]
divideStatsData[ data1_, data2_ ] := ( Module[ { data }, (* Module makes declared variables local *)
data = { #, CountryData[ #, data1 ], CountryData[ #, data2 ] } & /@
CountryData[];
(* Missing data / missing data = 1 *)
data = removeMissingData[ data ];
{ #[[1]], #[[2]] / #[[3]] } & /@ data
]
)
extractData[ data_ ] := QuantityMagnitude @ Flatten[ Transpose[ data ][[ 2 ]] ]
drawMapWithTooltips[ data_ ] := ( Module[ { rawData, min, max },
rawData = extractData[ data ];
{ min, max } = { Min[ rawData ], Max[ rawData ] };
GeoRegionValuePlot[
data,
PlotRange -> { min, max },
GeoLabels -> (
Tooltip[
#1, (* Location *)
Extract[
Extract[ (* First extract removes ugly braces, second actually gets information *)
data,
Position[ data, #2, 2 ] + { { 0, 1 } } (* Search through table by country (#2) *)
], 1 ]
] &
)
]
]
)
sortFromGreatestToLeast[ data_ ] := Sort[ data, #1[[2]] > #2[[2]] &]
(* Data *)
arableLandPerPopulation = divideStatsData[ "ArableLandArea", "Population" ];
drawMapWithTooltips[ arableLandPerPopulation ]
greatestToLeast = sortFromGreatestToLeast[ arableLandPerPopulation ];
Grid[ Take[ greatestToLeast, 10 ], Frame -> All ]
Grid[ Take[ Reverse[ greatestToLeast ], 10 ], Frame -> All ]
Or if you don't want to load all that data:
arableLandPerPopulation = {{Entity["Country", "Australia"],
Quantity[0.0200457, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Kazakhstan"],
Quantity[0.0136973, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Canada"],
Quantity[0.0130036, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Argentina"],
Quantity[0.00939364, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Niger"],
Quantity[0.00890364, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Russia"],
Quantity[0.00840941, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Ukraine"],
Quantity[0.00731436, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Lithuania"],
Quantity[0.00692383, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Paraguay"],
Quantity[0.00637857, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Niue"],
Quantity[0.00620732, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Belarus"],
Quantity[0.0058311, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Guyana"],
Quantity[0.00551926, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Latvia"],
Quantity[0.0053108, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Moldova"],
Quantity[0.00522219, ("Kilometers")^2/("People")]}, {Entity[
"Country", "SaintHelena"],
Quantity[0.00521512, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Uruguay"],
Quantity[0.00514164, ("Kilometers")^2/("People")]}, {Entity[
"Country", "UnitedStates"],
Quantity[0.00481068, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Estonia"],
Quantity[0.00464188, ("Kilometers")^2/("People")]}, {Entity[
"Country", "Serbia"],
Quantity[0.00458116, ("Kilometers")^2/("People")]}};
greatestToLeast = arableLandPerPopulation;
extractData[ data_ ] := QuantityMagnitude @ Flatten[ Transpose[ data ][[ 2 ]] ]
drawMapWithTooltips[ data_ ] := ( Module[ { rawData, min, max },
rawData = extractData[ data ];
{ min, max } = { Min[ rawData ], Max[ rawData ] };
GeoRegionValuePlot[
data,
PlotRange -> { min, max },
GeoLabels -> (
Tooltip[
#1, (* Location *)
Extract[
Extract[ (* First extract removes ugly braces, second actually gets information *)
data,
Position[ data, #2, 2 ] + { { 0, 1 } } (* Search through table by country (#2) *)
], 1 ]
] &
)
]
]
)
drawMapWithTooltips[ arableLandPerPopulation ]
greatestToLeast = sortFromGreatestToLeast[ arableLandPerPopulation ];
Grid[ Take[ greatestToLeast, 10 ], Frame -> All ]
Grid[ Take[ Reverse[ greatestToLeast ], 10 ], Frame -> All ]
PlotRange
as one of the preconditions for this behavior. ChangePlotRange -> {min, max},
toPlotRange -> All,
and it all works. $\endgroup$