Announcement

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

  • Legend in spmap: adding a second legend and adding variable labels / variable values as labels

    Hello everyone.

    Today I have a more complex question than usual. I am making many maps through loops, and that works just fine; it is amazing to be honest. However, my problem is that the usual spmap legend is not enough for me. As you can see in the picture I have uploaded below, I am adding some kind of "second legend" on the left with additional map information using another program. The problem is this is a very much time-consuming operation, which I hope to automatize using legend commands within spmap. I already know how to make loops and I also have a good grasp of most legend commands, but I am no expert to be fair.

    Basically, my questions are:

    a) is it possibile to put a second spmap legend? Or, at least, more subtitles than just one? If not possibile, is there some gimmick which allows to move specific labels around?
    b) is it possible to have variable labels/category labels/values as labels in the legend? If so, what does the command look like?

    I would like to obtain the very same result shown in the picture below, but without using another program. For example, "Cremona" (which is a category label from a string variable) should come up automatically and also number 123 (which is a value from a numeric variable) depending on certain conditions. I am not sure you need the whole picture of what I am doing to give me an answer, if so I will provide additional information, of course.

    Thanks in advance!

    PS: I hope I have not broken any rule attaching an image: I honestly thought it could help to understand better my request.

    Click image for larger version

Name:	Sample_04_CC_08_APRILE_POP_TXT.PNG
Views:	1
Size:	521.0 KB
ID:	1552193

  • #2
    1. Additional text information
    You could add additional text information using title() subtitle() l1title(), & l2title(). Another way would be to add text to an empty graph and the combine the text and map. See below
    2. Using label in the legend
    The default method is to create the categories using -pctile-. You could categorize your variable and add labels before calling -spmap- and then use the clmethod( unique ):
    Code:
    use "Italy-RegionsData.dta", clear  
    sum relig1,
    local max = round(r(max),.01)
    pctile _clbreaks = relig1, nq(4)
    
    gen catrelig = 1 if relig1 <= r(r1)
    replace catr = 2 if relig1 > r(r1) & relig1 <= r(r2)
    
    replace cat = 3 if relig1 > r(r2)   & relig1 <= r(r3)
    replace cat = 4 if relig1 > r(r3)
    
    label define cat 4 "Horton Hears a Who! (max. `max')"
    label values cat cat
    spmap cat using "Italy-RegionsCoordinates.dta", id(id)          ///
        clm(u) fcolor(Blues) name(gr1,replace) 
    
    clear
    set obs 5
    gen x = _n
    gen y = _n
    scatter y x, msymbol(none) xlabel(,nogrid) ylabel(,nogrid) yscale(off) xscale(off) fxsize(30) ///  
        text(5 1 "`c(current_date)'"   , size(medsmall) j(left) placement(3)) /// 
        text(4.5 1 "Casi su 10mila persone:" "(max prov. e nazionale)" , size(medsmall)  placement(3) j(left)) /// 
        text(4 1 "-Cremona 123" "-Nazionale 23", size(medsmall)  placement(3) j(left))  /// 
        text(3 1 "Casi complessivi" "(max prov. e nazionale)" "-Milana: 12039" "-Nazionale: 139422", size(medsmall)  placement(3) j(left))  
    graph combine Graph gr1, row(1) name(gr2,replace)
    Click image for larger version

Name:	gr2.png
Views:	1
Size:	67.3 KB
ID:	1552226

    Comment


    • #3
      Thank you Scott for your kind reply! However, I have still one major issue with the scatter command and text options: I actually would like to put as text some labels / values depending on certain conditions. The reason is I am making many maps through loops, so I cannot afford to write down everything each time. For example, "Cremona" is the corresponding value label (within the variable PROVINCIA) of the province with the highest value of a certain variable in the loop (say CC_08_APRILE_POP), and 123 is the numeric value. My data structure is like this:
      .
      PROVINCIA CC_08_APRILE_POP CC_08_APRILE_POP_MAX CC_10_APRILE_POP CC_10_APRILE_POP_MAX
      CREMONA 123 123 127 132
      LODI 117 123 132 132
      BRESCIA 108 123 119 132
      .
      Given this data structure, I would like the text option to capture "Cremona" on its own, because I am making something like 50-60 maps through a loop. I have already tried using the "if" option with no success, since STATA will not allow it within the text option of scatter. An example of what I would like given your kind explanation and the data structure above (just to be clearer, since this cannot be done like this):

      text(4 1 "- label of PROVINCIA if CC_08_APRILE_POP==CC_08_APRILE_POP_MAX: ///
      value of CC_08_APRILE_POP if CC_08_APRILE_POP==CC_08_APRILE_MAX", size...

      which should produce something like this: "- Cremona: 123", but automatically, within the loop logic. Of course, looking at CC_10_APRILE_POP, "- Lodi: 132" should replace "- Cremona: 123" in the next map made through the loop.

      I should have given you additional information in my original post, anyway thanks for helping me out, I appreciated it a lot.
      Last edited by Andrea Berni; 11 May 2020, 09:41.

      Comment


      • #4
        PS: actually I am studying about "macros" in STATA and I am making it No need for further explanation! Still, a huge thank you!

        Comment


        • #5
          Andrea Berni

          It is possible to extract the information you need from the data to place in the text statements. Below is an example of extracting the name of the region with the largest relig1 percent:
          Code:
          clear*
          forv i = 1/2 {
          use "Italy-RegionsData.dta", clear  
          
          qui sum relig`i'
          local max = round(r(max),.01)
          qui levelsof region if relig`i' == r(max), local(regionname) clean
          
          
          pctile _clbreaks = relig`i', nq(4)
          
          gen catrelig = 1 if relig`i' <= r(r1)
          replace catr = 2 if relig`i' > r(r1) & relig`i' <= r(r2)
          
          replace cat = 3 if relig`i' > r(r2)   & relig`i' <= r(r3)
          replace cat = 4 if relig`i' > r(r3)
          
          label define cat 4 "Horton Hears a Who! (max. `max')"
          label values cat cat
          spmap cat using "Italy-RegionsCoordinates.dta", id(id)          ///
              clm(u) fcolor(Blues) name(gr_map,replace) 
          
           
          clear
          set obs 5
          gen x = _n
          gen y = _n
          scatter y x, msymbol(none) xlabel(,nogrid) ylabel(,nogrid) yscale(off) xscale(off) fxsize(30) ///  
              text(5 1 "`c(current_date)'"   , size(medsmall) j(left) placement(3)) /// 
              text(4.5 1 "Largest Region: `regionname' "  , size(*.9)  placement(3) j(left))
          
          graph combine Graph gr_map, row(1) name(gr`i',replace)
          }
          graph combine gr1 gr2, row(1) xsize(10in)
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	76.2 KB
ID:	1552749

          Comment


          • #6
            Thank you again Scott and sorry for the late reply. I have managed successfully to achieve my objective through using "levelsof" and macros, in some similar manner compared to your proposal. Moreover, I have decided to use the "text" option within spmap instead of "graph combine", but this is just an aesthetical choice by now. However, I am disappointed there is no way to overlay graphs outside of twoway scatter (which will not allow overlaying a spmap graph with scatter), even though I guess it does not make sense in most cases.

            Comment


            • #7
              Originally posted by Andrea Berni View Post
              Thank you again Scott and sorry for the late reply. I have managed successfully to achieve my objective through using "levelsof" and macros, in some similar manner compared to your proposal. Moreover, I have decided to use the "text" option within spmap instead of "graph combine", but this is just an aesthetical choice by now. However, I am disappointed there is no way to overlay graphs outside of twoway scatter (which will not allow overlaying a spmap graph with scatter), even though I guess it does not make sense in most cases.
              Hi Andrea, how did you add text directly through spmap? How did you manage the coordinates X Y for text, eg. text(X Y "something").

              Thanks!

              Comment

              Working...
              X