4
$\begingroup$

I am trying to get a map with 4 colors, I have several regions with values from 0 to 3 and I want to color those regions with value 0 in white, with value 1 in yellow, with value 2 in orange and with value 3 in red.

I have tried both using ColorFunction and ColorRules but Mathematica ignores these directives. Below are my attempts. Using ColorRules:

GeoRegionValuePlot[{
  Entity["AdministrativeDivision", {"Arkansas", "UnitedStates"}] -> 1,
  Entity["AdministrativeDivision", {"California", "UnitedStates"}] -> 0,
  Entity["AdministrativeDivision", {"Connecticut", "UnitedStates"}] -> 2,
  Entity["AdministrativeDivision", {"Illinois", "UnitedStates"}] -> 3,
  Entity["AdministrativeDivision", {"Maryland", "UnitedStates"}] -> 3,
  Entity["AdministrativeDivision", {"Massachusetts", "UnitedStates"}] -> 3,
  Entity["AdministrativeDivision", {"Montana", "UnitedStates"}] -> 0,
  Entity["AdministrativeDivision", {"NewYork", "UnitedStates"}] -> 0,
  Entity["AdministrativeDivision", {"NewJersey", "UnitedStates"}] -> 1,
  Entity["AdministrativeDivision", {"Ohio", "UnitedStates"}] -> 1,
  Entity["AdministrativeDivision", {"RhodeIsland", "UnitedStates"}] -> 2,
  Entity["AdministrativeDivision", {"Texas", "UnitedStates"}] -> 0,
  Entity["AdministrativeDivision", {"WestVirginia", "UnitedStates"}] -> 0
  }, ColorRules -> {0 -> White, 1 -> Yellow, 2 -> Orange, 3 -> Red}]

Using ColorFunction:

f[x_] := Piecewise[{{White, x == 0}, {Yellow, x == 1}, {Orange, x == 2}, {Red, x == 3}}, 0];
GeoRegionValuePlot[{
  Entity["AdministrativeDivision", {"Arkansas", "UnitedStates"}] -> 1,
  Entity["AdministrativeDivision", {"California", "UnitedStates"}] -> 0,
  Entity["AdministrativeDivision", {"Connecticut", "UnitedStates"}] -> 2,
  Entity["AdministrativeDivision", {"Illinois", "UnitedStates"}] -> 3,
  Entity["AdministrativeDivision", {"Maryland", "UnitedStates"}] -> 3,
  Entity["AdministrativeDivision", {"Massachusetts", "UnitedStates"}] -> 3,
  Entity["AdministrativeDivision", {"Montana", "UnitedStates"}] -> 0,
  Entity["AdministrativeDivision", {"NewYork", "UnitedStates"}] -> 0,
  Entity["AdministrativeDivision", {"NewJersey", "UnitedStates"}] -> 1,
  Entity["AdministrativeDivision", {"Ohio", "UnitedStates"}] -> 1,
  Entity["AdministrativeDivision", {"RhodeIsland", "UnitedStates"}] -> 2,
  Entity["AdministrativeDivision", {"Texas", "UnitedStates"}] -> 0,
  Entity["AdministrativeDivision", {"WestVirginia", "UnitedStates"}] -> 0
  }, ColorFunction -> f]

With ColorRules, Mathematica ignores the rules and with ColorFunction Mathematica gives five instances of the error 0 is not a Graphics primitive or directive. I'm using Mathematica 10.4.1.0 on OS X El Capitan.

$\endgroup$
  • 2
    $\begingroup$ Here is a minimal example: GeoRegionValuePlot[{Entity["AdministrativeDivision", {"Texas", "UnitedStates"}] -> 0, Entity["AdministrativeDivision", {"WestVirginia", "UnitedStates"}] -> 0}, ColorRules -> {0 -> White, 1 -> Blue, 2 -> Orange, 3 -> Red}] Note that this failure happens when two states are supposed to have the same corresponding color. $\endgroup$ – J. M. will be back soon Jun 8 '16 at 16:34
  • $\begingroup$ so that's a bug? what about the approach using ColorFunction? $\endgroup$ – amrods Jun 8 '16 at 16:35
  • 1
    $\begingroup$ That's easier; try adding ColorFunctionScaling -> False. (N.B. can somebody with access to multiple versions add the customary header to localize this bug?) $\endgroup$ – J. M. will be back soon Jun 8 '16 at 16:39
2
$\begingroup$

What is the bug?

It seems that GeoRegionValuePlot will not work correctly when two or more entities have the exact same value. Consider these examples (and ignore the legend, which is always wrong unless you give an explicit ColorFunction as below):

GeoRegionValuePlot[{Entity[
    "AdministrativeDivision", {"Arkansas", "UnitedStates"}] -> 1, 
  Entity["AdministrativeDivision", {"California", "UnitedStates"}] -> 
   0}, ColorRules -> {0 -> White, 1 -> Purple}]
GeoRegionValuePlot[{Entity[
    "AdministrativeDivision", {"Arkansas", "UnitedStates"}] -> 1, 
  Entity["AdministrativeDivision", {"California", "UnitedStates"}] -> 
   1}, ColorRules -> {0 -> White, 1 -> Purple}]

Mathematica graphics

Workaround - make sure that each value is slightly different.

valuelist = {Entity[
     "AdministrativeDivision", {"Arkansas", "UnitedStates"}] -> 1, 
   Entity["AdministrativeDivision", {"California", "UnitedStates"}] ->
     0, Entity[
     "AdministrativeDivision", {"Connecticut", "UnitedStates"}] -> 2, 
   Entity["AdministrativeDivision", {"Illinois", "UnitedStates"}] -> 
    3, Entity[
     "AdministrativeDivision", {"Maryland", "UnitedStates"}] -> 3, 
   Entity["AdministrativeDivision", {"Massachusetts", 
      "UnitedStates"}] -> 3, 
   Entity["AdministrativeDivision", {"Montana", "UnitedStates"}] -> 0,
    Entity["AdministrativeDivision", {"NewYork", "UnitedStates"}] -> 
    0, Entity[
     "AdministrativeDivision", {"NewJersey", "UnitedStates"}] -> 1, 
   Entity["AdministrativeDivision", {"Ohio", "UnitedStates"}] -> 1, 
   Entity["AdministrativeDivision", {"RhodeIsland", 
      "UnitedStates"}] -> 2, 
   Entity["AdministrativeDivision", {"Texas", "UnitedStates"}] -> 0, 
   Entity["AdministrativeDivision", {"WestVirginia", 
      "UnitedStates"}] -> 0};
colorlist = {0 -> White, 1 -> Yellow, 2 -> Orange, 3 -> Red};
colorlist = valuelist[[All, 2]] /. colorlist;
valuelist[[All, 2]] = # + RandomReal[0.1] & /@ valuelist[[All, 2]];
colorlist = Thread[valuelist[[All, 2]] -> colorlist];
GeoRegionValuePlot[valuelist, ColorRules -> colorlist]

Mathematica graphics

It is ugly, but this is the only way I can find to make the ColorRules work, and even here the legend doesn't work right.

Workaround - use a ColorFunction instead

This is similar to the code in OP's example, except here we define the Piecewise function for all x values, which means we get a decent legend,

f[x_] := Piecewise[{{White, x < 1}, {Yellow, 
     1 <= x < 2}, {Orange, 2 <= x < 3}, {Red, 3 <= x < 4}}];
GeoRegionValuePlot[{Entity[
    "AdministrativeDivision", {"Arkansas", "UnitedStates"}] -> 1, 
  Entity["AdministrativeDivision", {"California", "UnitedStates"}] -> 
   0, Entity[
    "AdministrativeDivision", {"Connecticut", "UnitedStates"}] -> 2, 
  Entity["AdministrativeDivision", {"Illinois", "UnitedStates"}] -> 3,
   Entity["AdministrativeDivision", {"Maryland", "UnitedStates"}] -> 
   3, Entity[
    "AdministrativeDivision", {"Massachusetts", "UnitedStates"}] -> 3,
   Entity["AdministrativeDivision", {"Montana", "UnitedStates"}] -> 0,
   Entity["AdministrativeDivision", {"NewYork", "UnitedStates"}] -> 0,
   Entity["AdministrativeDivision", {"NewJersey", "UnitedStates"}] -> 
   1, Entity["AdministrativeDivision", {"Ohio", "UnitedStates"}] -> 1,
   Entity["AdministrativeDivision", {"RhodeIsland", 
     "UnitedStates"}] -> 2, 
  Entity["AdministrativeDivision", {"Texas", "UnitedStates"}] -> 0, 
  Entity["AdministrativeDivision", {"WestVirginia", 
     "UnitedStates"}] -> 0}, ColorFunction -> f, 
 ColorFunctionScaling -> False]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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