Announcement

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

  • How to return label (string) and not variable value - for graphs name

    I have a code where I divide a country in regions. So, the I create a variable like.

    Code:
    gen regiao = 1 if inlist(uf, "RJ", "SP", "ES", "MG")  
        replace regiao = 2 if inlist(uf, "SC", "RS", "PR")
        replace regiao = 3 if inlist(uf, "EX")
    But then, in order to facilatate, I create labels, just like:
    Code:
        label define reg_label 1 "sudeste" 2 "sul" 3  "exterior" 
           label values regiao reg_label

    But now I want to generate some graphs using the structure foreach (below), where I want to gen a graph for each region. But inside the title, I would like that appear the name/label of the region ("sudeste", "sul", "exterior") and not the numbers (1,2,3).

    Code:
    levelsof regiao, local(levels)
    foreach l of local levels {
    tw line cont ano_ativ if regiao_name == "`l'" , title("Firmas ativas por Ano - `l'")
    I know this structure above works fine for strings. It also work here in fact, but the variable is indeed numbers, so I can't use it also in graph title.

    Well, of course I can gen a new variable with strings if regiao == 1 etc., or even have build that variable (regiao) as a string since the beginning, but I thought it was easier to work along the code with numeric (makes sense, right?).

    However, now that I want to use it as a string, I don't know how I can return the name of the label and not variable's name itself. Is there a good way to work this out?



  • #2
    Consider:

    Code:
    sysuse auto, clear
    set scheme s1mono
    gr bar if !foreign, over(rep78) title("`: lab (foreign) 0'")
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	16.3 KB
ID:	1699922

    Comment


    • #3
      This example may point you in a useful direction. In the auto dataset, the variable foreign is a 0/1 variable with value labels.
      Code:
      sysuse auto, clear
      levelsof foreign, local(levels)
      foreach l of local levels {
          macro list _l
          local lbl: label (foreign) `l'
          tw scatter price weight if foreign == `l' , title("My title - `lbl'") name(g`l')
      }
      graph combine g0 g1
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	40.7 KB
ID:	1699924

      Comment

      Working...
      X