Announcement

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

  • Display variable label instead of variable name on a graph

    Hi,

    I would like to display the variable label instead of variable name on the y-axis of a scatter plot with a linear regression fit. The two variabels are smw_meter and om_muscle_area_no_fat. Sample cod below.

    Code:
    graph twoway (scatter smw_meter om_muscle_area_no_fat) (lfit smw_meter om_muscle_area_no_fat), ylabel () legend(off)
    I have tried ylabel () and yvarlabel() but to no avail. Either the y-axis is blank or it shows the variable name again.

    Thanks!

  • #2
    Perhaps
    Code:
    graph twoway (scatter smw_meter om_muscle_area_no_fat) (lfit smw_meter om_muscle_area_no_fat), ytitle("type your variable label here")

    Comment


    • #3
      That would totally work, I know about that. However, I have to automate these graphs with two nested for loops. Needs to use something that is not custom typed in.

      Comment


      • #4
        There are various small issues here.

        One is perhaps confusion over what we are talking about: in Stata axes usually have titles; some other software calls those axis title axis labels. Conversely, what to Stata are axis labels might be called tick labels or some such in other software.

        That said, the question is how to get you want, which I guess is

        Code:
        scatter smw_meter om_muscle_area_no_fat ///  
        || lfit smw_meter om_muscle_area_no_fat, ///
        ytitle("`: var label smw_meter'") xtitle("`: var label om_muscle_area_no_fat'") legend(off)
        I find the parenthesis notation for distinguishing different twoway calls less attractive than the double pipe notation (there are enough parentheses to worry about already (that's just a personal opinion and you may disagree (syntax is sometimes a matter of taste too))).

        A higher level of abstraction is to assume that we are inside one or more loops here, so that we may be asking for something like

        Code:
        scatter `y' `x'  || lfit `y' `x', ///
        ytitle("`: var label `y''") xtitle("`: `x''") legend(off)
        See

        Code:
        help macro 
        and follow the link for macro functions.
        Last edited by Nick Cox; 23 Aug 2023, 15:38.

        Comment


        • #5
          Thanks very much, Nick! That is exactly what I was looking for. That was also a nice introduction to some new coding that I am not familiar with. That is much cleaner than all the parentheses that I was using!

          What is
          Code:
          "`:
          doing? I have seen these syntax used before. I looked up the macro, but not entirely sure I understand. Thanks again!!

          Comment


          • #6
            The roots of this syntax lie in the

            Code:
            ` '
            single quotation marks used to flag a local macro reference. When used, the instruction to Stata is

            evaluate the local macro referenced here and substitute the result within this command line for later execution.

            Another extension of this syntax is exemplified by

            Code:
            `=2 + 2'
            where there is no local macro name, but the= sign indicates that a calculation follows, and Stata is to carry out the calculation and substitute the result in the same way.

            The extension in question is at its fanciest in the post above

            Code:
            `:  var label `v''
            where the `: ' flags use of a so-called macro function.

            Just as in elementary algebra when you have nested parentheses such as (1 + (8/2)) (rather than (1 + 8)/2. say) when there are nested single quotation marks, the innermost evaluation is carried out first. So here Stata first works out which variable name is included in local macro v, and then what its variable label is. Naturally this can succeed, or otherwise fail in various ways, but that is another story.

            Using a colon is just an arbitrary choice. although most other choices would seem even more arbitrary.





            `

            Comment


            • #7
              Originally posted by Nick Cox View Post
              Using a colon is just an arbitrary choice. although most other choices would seem even more arbitrary.
              Nick: Apologies if I have misunderstood you here (or if I am simply repeating you, using different words! [ETA: on re-reading your post, I think that's exactly what I am doing; sorry!!] ), but my understanding of the colon (in this context) is specifically to flag the use of a macro function (for Jay Gold: see help macro##macro_fcn).
              Therefore, the `: ' syntax is to be read as a nesting of "colon = macro function to extract the variable name" inside "inverted-commas = immediately evaluate the expression rather than assigning it to a macro".

              So, for example, instead of the macro assignment followed by display directive:
              Code:
              local varlabel : var label foo
              disp "`varlabel'"
              ...you might wish to evaluate and display immediately, skipping the macro assignment:
              Code:
              disp "`: var label foo'"
              I agree with both of you, though, that this syntax is (a) often very useful; and (b) poorly documented/explained!
              Last edited by David Fisher; 24 Aug 2023, 03:56.

              Comment


              • #8
                #7 David Fisher I don't think we're disagreeing -- quite the contrary -- but your post is very helpful in flagging that the colon syntax is used elsewhere for this purpose, to signal that a macro function follows.

                While we're talking please may I correct a bad typo in #4?

                Code:
                 
                 xtitle("`: `x''")
                should be

                Code:
                 
                 xtitle("`: var label `x''")

                Comment

                Working...
                X