Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Colors in spmap

    Hi!

    I'm having a problem with how my colors are displayed in spmap. Here's the code (I'm not copying and pasting all the code needed to create the map, because everything else works perfectly—it's just when I change the colors that I run into this issue) :

    gen ratio = (amount_share/circ_share)

    centile ratio, centile( 20 40 60 80 100)
    forvalues i = 1/4 {
    local c`i' = round(`r(c_`i')',0.10)
    }

    local max = ceil(`r(c_5)')
    spmap ratio if period == 0 using "$Maps/india_statecoord", id(id) clmethod(custom) clbreaks(0 `c1' `c2' `c3' `c4' `max') fcolor(Blues) ndfcolor(gray) title("Before 2015") legend(size(medium)) saving(graph0, replace)
    spmap ratio if period == 1 using "$Maps/india_statecoord", id(id) clmethod(custom) clbreaks(0 `c1' `c2' `c3' `c4' `max') fcolor(Blues) ndfcolor(gray) title("After 2015") legend(size(medium)) saving(graph1, replace)
    graph combine graph0.gph graph1.gph, col(2) graphregion(color(white))
    graph export "$Figures\ratio_state_map_daily.pdf", as(pdf) replace
    graph export "$Figures\ratio_state_map_daily.pdf", as(pdf) replace
    erase graph0.gph
    erase graph1.gph


    gen sur = 1
    replace sur=2 if ratio>=0.5 & ratio<0.9
    replace sur=3 if ratio>= 0.9 & ratio<=1.1
    replace sur=4 if ratio>1.1 & ratio<2
    replace sur=5 if ratio>=2


    // Vérification rapide (à faire dans le log)
    // tab sur if period == 0
    // tab sur if period == 1


    spmap sur if period == 1 using "$Maps/india_statecoord", ///
    id(id) ///
    clmethod(custom) ///
    clbreaks(0 1.5 2.5 3.5 4.5) ///
    fcolor(blue*0.7 blue*1 green*0.3 red*0.3 red*0.7) ///
    title("After 2015") ///
    legend(size(large) ring(1) position(6) ///
    order(1 2 3 4 5) ///
    label(1 "[0-0.5[") ///
    label(2 "[0.5-0.9[") ///
    label(3 "[0.9-1.1]") ///
    label(4 "]1.1-2[") ///
    label(5 ">2") ///
    region(fcolor(white) lcolor(none))) ///
    ndfcolor(white) ///
    saving(graph1, replace)

    The problem is that all my colors are off. Specifically, Category 1 is shown as white in the legend instead of blue*0.7. Therefore, the last category is supposed to be dark red, not light red (I've listed the colors in the order of my “sur” variable). Aside from the fact that the colors in my legend don’t match my code, the legend doesn’t correspond to what’s shown on the map. For example, Tamil has a ratio of 4. According to my code, it should therefore be light red. Admittedly, it is light red on the map, but the legend states that light red represents states with a ratio > 2, whereas Tamil Nadu has a ratio between 1.1 and 2 (1.5), so the legend is incorrect, but the color is correct. For Andhra Pradesh, sur=1. According to my code, it should be light blue. That’s the case (blue*0.7), but the legend indicates that for Andhra Pradesh, sur=2...



    Click image for larger version

Name:	Capture d'écran 2026-06-29 104205.png
Views:	1
Size:	48.4 KB
ID:	1786430


    Thanks in advance to the Stata community !


  • #2
    There are two problems in your code. 1. Your custom range does not extend to when your data values are greater than 4.5 and 2. the first key is assigned to missing values.

    Using an example data set (-ssc desc spmap-) provided with Maurizio Pisati's smap, I think what you want is:

    Code:
     use "Italy-RegionsData.dta", clear
    gen sur = 1
    replace sur=2 if relig1>=40 & relig1<45
    replace sur=3 if relig1>= 45 & relig1<=50
    replace sur=4 if relig1>50 & relig1<60
    replace sur=5 if relig1>=60
    
    spmap sur using "Italy-RegionsCoordinates.dta", id(id) /// 
        clmethod(custom)  ///
        clbreaks(0  1.5 2.5 3.5 4.5 5.5 ) ///
        fcolor(blue*0.7 blue*1 green red*0.3 red*0.7) /// 
        legend(order( 2 3 4 5 6 )  /// 
        label(2 "[0-0.5[")  /// 
        label(3 "[0.5-0.9[")  ///
        label(4 "[0.9-1.1]") ///
        label(5 "]1.1-2[") ///
        label(6 ">2") ///
        region(fcolor(white) lcolor(none)))

    Comment


    • #3
      It works! Thank you so much. However, I don't understand your explanation regarding why starting the legend order at 2 prevents the misalignment?

      Comment


      • #4
        The first key is for missing values; the actual data values begin with key #2. Normally, since you do not have missing values the first key is not displayed but in your original code you explicit reference it in the order() and label() statements. You actually do not need the order statement, but the label() statements need to start at 2.

        Comment


        • #5
          Ok, thanks !

          Comment

          Working...
          X