Announcement

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

  • Label of observations in Scatter plots

    Hi all,

    Nick Cox I am trying to build a scatter plot having as labels the labels assigned through lambmask (by Nick Cox) to id_mol. The output is fine so the only thing that I have some problems with is how to manipulate label option so that it assigns instead of the id_mol (which is a numeric id) the label (i.e. the name of each molecule stored as a string in another variable). Actually the following code is giving me the desired label only for the very first variable. Since I am not reporting all the ids on the x axis (but only 9687(1500)21447) I am guessing that the problem is related to that

    Code:
    use "/Users/federiconutarelli/Dropbox/Federico/mol_ctry12_qrtr48_modified.dta", clear
    gen ln_price = ln(price)
    *egen prezzo_medio = mean(price) , by(id_mol ctry)
    egen prezzo_medio = mean(ln_price) , by(id_mol ctry)
    drop if mol =="Goserelin" |mol =="Triptorelin"
    replace ctry = "italy" if ctry =="aitaly"
    
    labmask id_mol, values(molecule)
    
     
    
    local country "belgium france germany greece ireland italy netherlands poland portugal sweden uk"
    
    *local country "belgium france germany greece ireland italy netherlands poland portugal sweden uk"
    foreach c in `country'{
        capture confirm  string variable `c'
        quietly sum prezzo_medio if ctry == "`c'"
        local xmax_`c' = r(max)
        twoway (scatter prezzo_medio id_mol) if ctry == "`c'", title("`c'") legend(label(1 "Mean price")) xtitle("id mol") xlabel(9687(1500)21447, valuelabel angle(45)) ylabel(, angle(0)) ytitle("mean pricce by ctry/mol.") yscale(r(0 `xmax_`c'')) saving("new`c'", replace) 
        local new `"`new' "new`c'""'
    }
    graph combine `new'
    graph export /Users/federiconutarelli/Desktop/prezzomedio.pdf , replace
    local new ""
    Thanks a lot!

  • #2
    labmask is from the Stata Journal as written up within

    SJ-8-2 gr0034 . . . . . . . . . . Speaking Stata: Between tables and graphs
    (help labmask, seqvar if installed) . . . . . . . . . . . . N. J. Cox
    Q2/08 SJ 8(2):269--289
    outlines techniques for producing table-like graphs



    I guess you need its decode option as documented in the help

    decode specifies that the value labels of valuesname should be used as the value labels of
    varname. The default is to use the values of valuesname.

    Comment


    • #3
      Nick Cox Thank you for the reply.

      So yes I used it but the following error appears:

      Code:
      not possible with string variable
      Moreover, at first, I did not take it into account since it says that

      The default is to use the values of valuesname.
      Also if I encode the string, and use the option decode, it seems not to work. Again the xlabel option "valuelabel" seems to be ignored after the first iteration.


      So for the moment it is not working...but still when I list id_mol, the command does what it is supposed to do. The problem is the implementation on the xlabel of scatterplot.
      Last edited by Federico Nutarelli; 02 Jul 2020, 00:55.

      Comment


      • #4
        I can't easily follow this. Your code example uses your own data, which we can't access You want x axis labels at 9687(1500)21447 to show value labels at those positions

        Code:
        . numlist "9687(1500)21447"
        
        . ret li
        
        macros:
                    r(numlist) : "9687 11187 12687 14187 15687 17187 18687 20187"
        but that requires value labels to be defined and attached for all those integer values.

        Consider a silly example. With the auto data we could ask for 2 as an x axis label for
        foreign but if no value label is defined graph can only show the integer itself.
        Code:
        . sysuse auto , clear
        (1978 Automobile Data)
        
        . scatter mpg foreign, xla(0 1 2, valuelabel)
        With labmask given a variable with integer values, we can define its labels as (1) the values of another numeric variable (2) the values of a string variable (3) the value labels of another numeric variable.

        I run through all the cases here:

        Code:
        . * Example generated by -dataex-. To install: ssc install dataex
        . clear
        
        . input float var1 str4 var2 float(var3 var4)
        
                  var1       var2       var3       var4
          1. 1 "frog" 12 4
          2. 2 "toad" 34 5
          3. 3 "newt" 56 6
          4. end
        
        . 
        . label def var4 4 whatever 5 "who knows" 6 "any guess"
        
        . label val var4 var4
        
        . 
        . list
        
             +--------------------------------+
             | var1   var2   var3        var4 |
             |--------------------------------|
          1. |    1   frog     12    whatever |
          2. |    2   toad     34   who knows |
          3. |    3   newt     56   any guess |
             +--------------------------------+
        
        . 
        . labmask var1, values(var2)
        
        . list var1
        
             +------+
             | var1 |
             |------|
          1. | frog |
          2. | toad |
          3. | newt |
             +------+
        
        . 
        . labmask var1, values(var3)
        
        . list var1
        
             +------+
             | var1 |
             |------|
          1. |   12 |
          2. |   34 |
          3. |   56 |
             +------+
        
        . 
        . labmask var1, values(var4) decode
        
        . list var1
        
             +-----------+
             |      var1 |
             |-----------|
          1. |  whatever |
          2. | who knows |
          3. | any guess |
             +-----------+
        Code:
        
        


        Comment


        • #5
          Thanks a lot for the kind reply.

          I now have more clear how lambast works.
          However, apparently the problem was in the automatic selections of the id_mol. Indeed they were not shaped the way I think they were (i.e. they were basically not consecutive ids).

          Sorry and thanks a lot.

          Comment


          • #6
            lambmask lambast ... it's labmask. You know that really. The name goes back to 2002: If I were re-inventing the command now I guess the name would be labelmask, which is a little more transparent.

            A scatter plot with an identifier on the horizontal axis? I can see the point of that to explore the structure of variability, but much depends on how many distinct values how you have. Tell us more about what you are trying to do with this plot.

            Comment


            • #7
              Since the aim of the plot changed but I have still doubts on how to construct it that are unrelated to the label issue I think I will open another issue.
              However, I have solved the latter by using labmask and then selecting manually some specific values in xlabel(values selected manually, valuelabel)

              Comment

              Working...
              X