benjann / geoplot

Stata module to draw maps
MIT License
33 stars 3 forks source link

Order when using colorpalettes #15

Closed Monefield closed 1 year ago

Monefield commented 1 year ago

In my work I have to use specific colors in a specific order, so I have to use colorpalettes. I would like to use the colors in the order they are placed in the colorpalette (meaning that I always use the first X colors in the palette), but the reverse color-option seems to work differently than I would think when using colorpalettes. I have used your Italy-data for this small example:

label def pop98_cat3_lab 1 "Type 1" 2 "Type 2" 3 "Type 3", modify label val pop98_cat3 pop98_cat3_lab

When I don't use a colorpalette the reverse color-option works like I would think it did (uses the same colors, but reverses their order): No reverse color-option - Right colors (1 2 3), wrong order (1 2 3) geoplot (area regions pop98_cat3, discrete) /// , legend(reverse) Italy_colors_1

Reverse color-option - Right colors (1 2 3), right order (3 2 1) geoplot (area regions pop98_cat3, discrete color(, reverse)) /// , legend(reverse) Italy_colors_2

Using colorpalettes combined with the reverse color-option reverses both the order, but also the colors:

Colorpalette program colorpalette_colors_test c_local P #FF0000, /// 1 - Red

FF8000, /// 2 - Orange

        #FFFF00,    /// 3 - Yellow
        #00FF00,    /// 4 - Green
        #00FFFF,    /// 5 - Cyan
        #0000FF     //  6 - Blue

end colorpalette colors_test

If I don't use the reverse color-option I get the right colors, but in the wrong order Right colors (1 2 3), wrong order (1 2 3): geoplot (area regions pop98_cat3, discrete color(colors_test)) /// , legend(reverse) Italy_colors_3

If I use the reverse color-option it reverses the palette as well Wrong colors (4 5 6), wrong order (6 5 4): geoplot (area regions pop98_cat3, discrete color(colors_test, reverse)) /// , legend(reverse) Italy_colors_4

I found a possible workaround where I use only the appropriate subselection of the colorpalette

Right colors (1 2 3), right order (3 2 1): colorpalette colors_test, select(1 2 3) nograph local colors_subselect `r(p)'

geoplot (area regions pop98_cat3, discrete color("`colors_subselect'", reverse)) /// , legend(reverse) Italy_colors_5

Since I've already found a workaround this is just wishfull thinking on my part, but it would be nice, that those two extra lines of code wasn't necessary :)

benjann commented 1 year ago

This is an issue related to colorpalette, not geoplot. The problem is that colorpalette does not know that this is a qualitative palette. Things work fine of you declare the palette as qualitative. That is, define the palette as:

program colorpalette_colors_test
    c_local P #FF0000, /// 1 - Red
              #FF8000, /// 2 - Orange
              #FFFF00, /// 3 - Yellow
              #00FF00, /// 4 - Green
              #00FFFF, /// 5 - Cyan
              #0000FF  //  6 - Blue
    c_local class qualitative
end

Alternatively, you could leave the palette definition as is and then type

geoplot (area regions pop98_cat3, discrete color(colors_test, class(qualitative) reverse))

or

geoplot (area regions pop98_cat3, discrete color(colors_test, select(1/3) reverse))
Monefield commented 1 year ago

That I didn't know! Very clever and nice with the alternative solutions! Thank you :)