Announcement

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

  • Labelling Y-axis of a graph with different values within a loop

    Dear Statalisters,

    Here are my codes for two graphs below. What I would like to know is whether it is possible to have different values for the two loops while labelling the y-axis. For example, for the existing values in the codes below (marked red) , is it possible to have 30(2)42 for the first loop and 30(1)36 for the second?

    Similarly is it possible to have different texts (marked red) for the two loops?



    Code:
    gr drop _all
    
    loc i = 1
    foreach var of varlist mvpa vomax {
    loc t: var lab `var'
    lgraph `var' visit rand, ///
    legend(off) ///
    title("Adjusted `t'",size(large)col(black)) ///
    lop(1 lpatt(dash)lcol(red)msym(S)mcol(red)lwidth(medthick); ///
        0 lpatt(solid)lcol(orange)msym(S)mcol(orange)lwidth(medthick)) ///
    ylab(30(2)42,ang(hor)nogrid) ytitle("Adjusted Mean `t'" " ",col(black)) ///
    xtitle("")xlab(-.1 " " 0 "Baseline" 6 "6-month" 12 "12-month" 12.2 " ",notick) ///
    graphregion(col(white)) ///
    text(33 11.5 "TAU" 40 11.5 "Exercise",size(large)col(red))
    gr copy x`i',replace
    loc ++i
    }
    
    gr combine x1 x2,graphregion(col(white))ysize(3)xsize(5)
    Any comments are much appreciated.

    Regards,
    Roman

  • #2
    You can create the required elements in the loop. The example below shows how to do this for the y-axis labels.
    Code:
    foreach var of varlist mvpa vomax {
      if "`var'" == "mvpa" {
        local ylab`var' "30(2)42"
      }
      else if "`var'" == "vomax" {
        local ylab`var' "20(5)50"
      }
      * Graph command follows
    }
    In the graph command you refer to the macro that defines the labels.
    Code:
    ylab(`ylab`var'', ang(hor) nogrid)

    Comment


    • #3
      I can't express how much indebted I am to the people in this forum. Many thanks Friedrich for showing me the trick. The only confusing part here to me is the line ---local ylab`var' "30(2)42"--- I thought within inverted comma, in this case "30(2)42", anything is treated as string; but then Stata is still computing the range of values with the specified interval. How could that be possible unless Stata is treating it numerically !!. Many thanks again.
      Roman

      Comment


      • #4
        I just realized that the code can be simplified because there is no need to include the variable names in the macro names. Different macro names are only required if both macros are used at the same time, which isn't the case. The changes from the code in post #2 are highlighted in red below.
        Code:
        foreach var of varlist mvpa vomax {
          if "`var'" == "mvpa" {
            local ylab "30(2)42"
          }
          else if "`var'" == "vomax" {
            local ylab "20(5)50"
          }
          * Graph command follows
        }
        Code:
        ylab(`ylab', ang(hor) nogrid)

        Comment


        • #5
          Originally posted by Roman Mostazir View Post
          Stata is still computing the range of values with the specified interval.
          Could you please clarify what you mean? In the code, the numbers 30(2)42 are enclosed in double quotes. The macro ylab in post #4 is therefore a string.

          Edit: I think I understand your question. The macro ylab contains a string but the ylab() option interprets the numbers correctly. The macro has to be a string because it contains the characters "(" and ")". Alternatively, you could define three numerical macros, as in the example below, but that's cumbersome and unnecessary.
          Code:
          local ylab1 = 30
          local ylab2 = 2
          local ylab3 = 42
          
          ylab(`ylab1'(`ylab2')`ylab3', ang(hor) nogrid)
          Last edited by Friedrich Huebler; 19 Nov 2015, 15:08.

          Comment


          • #6
            In regards to post#4

            I think you need the `var' in the macronames, though counter intutive !! I tried both. While the first commands work perfect (with varibale names attached to the local macro), the next commands (without variable reference) only taking the last range 20(5)50 in the macro and applying to both graphs !!



            In regards to Post# 5

            For example:

            Code:
            local ylab "30(2)42"
            
            di "`ylab'"
            . 30(2)42
            We are not seeing here an increment in values as they are being treated as string. But they are increasing from 30 to 42 with an interval of 2 while being used within the graph commands, which means they are being treated numerically while they are within doble quotes.




            Roman

            Comment


            • #7
              Originally posted by Roman Mostazir View Post
              I think you need the `var' in the macronames, though counter intutive !! I tried both. While the first commands work perfect (with varibale names attached to the local macro), the next commands (without variable reference) only taking the last range 20(5)50 in the macro and applying to both graphs !!
              I find this difficult to believe. Could you show some code that demonstrates what you described? Take the example below. It is not necessary to include the variable name in the name of the local.
              Code:
              sysuse auto, clear
              foreach var of varlist mpg weight {
                if "`var'" == "mpg" {
                  local title "Mileage (mpg)"
                }
                else if "`var'" == "weight" {
                  local title "Weight (lbs.)"
                }
                graph bar `var', title("`title'") name("`var'", replace)
              }
              Originally posted by Roman Mostazir View Post
              We are not seeing here an increment in values as they are being treated as string. But they are increasing from 30 to 42 with an interval of 2 while being used within the graph commands, which means they are being treated numerically while they are within doble quotes.
              I tried to explain this in post #5. Maybe someone else can offer a better explanation.

              Comment


              • #8
                Last night when I was working in my PC at home, it was producing two different graphs with two commands (being honest !!). And now I can see why you find it hard to believe. I ran the codes again in my work PC and the variable names are not needed suggesting your firm claim to be absolutely right. However, many thanks Friedrich. That was very helpful. Forget the other query. I think I got the intution behind it.
                Roman

                Comment


                • #9
                  In a similar vein, I have a series of longitudinal graph generated in a for loop (each for every individual identified by a) as exemplified below. Is there a way i can set a xlabel(0(x)y) based on the x=median and y=max values of days of that particular individual
                  Thanks so much!

                  levelsof a if N>1 & (cat=="b"), local(id)
                  foreach i of local id {
                  twoway(line c days if a=="`i'" ,
                  options ///
                  (line d days if a=="`i'" ,
                  options ///
                  (scatter e days if a=="`i'" & virus=="f" & g==0 ,
                  more options yaxis(2)) ///
                  (scatter e days if a=="`i'" & virus=="f" & g>0 ,
                  more options yaxis(2)), ///
                  ytitle("
                  bla") ytitle("blabla", axis(2)) ///
                  yet more options ///
                  lots of options xtitle("~ days") ///
                  ylabel(0(0.5)4) ylabel(2(0.5)4, axis(2)) title("
                  TITLE `i'-title")
                  graph export "C:\Users\
                  nameandslashes_`i'.png", as(png) replace
                  }

                  Comment


                  • #10
                    #9 You've understandably blanked out much of the code that doesn't bear on your question but there is no data example and nothing we can run. Nevertheless an answer is in essence to run summarize on each subset and pass results to the graph command.

                    This dopey example may be enough to illustrate technique.

                    Code:
                    sysuse auto, clear
                    
                    foreach x in 0  1 {
                        su mpg if foreign == `x', detail
                        scatter mpg weight if foreign == `x' , yla(0 `r(p50)'  `r(max)') name(G`x')
                        
                    }
                    The instruction 0(median)max will in many cases produce only axis labels for 0 and the median, that is whenever (max - median) < median.
                    Last edited by Nick Cox; 24 Feb 2023, 02:41.

                    Comment


                    • #11
                      wonderful thank you so much! I didn't realize you can feedscalars saved by a command without a macro or calling explicitly variables. It's simple and it works great. Doh!
                      Thanks again

                      Comment


                      • #12
                        wonderful thank you so much! I didn't realize you can feed scalars saved by a command without a macro or calling explicitly variables. It's simple and it works great. Doh!
                        Thanks again

                        Comment


                        • #13
                          Nick Cox I have a follow-up question to your solution in #10.

                          How would we specify the range for both y and x labels using a loop similar to the one you have mentioned above? i.e I want to set up both x and y labels in a similar way as described in #9.

                          Comment


                          • #14
                            I don't know exactly what you are looking for in #13 but this may help. The example is silly and just to show technique.

                            Code:
                            sysuse auto, clear  su mpg, detail local  yla 0 `r(p50)' `r(max)'  su weight, detail  local xla 0 `r(p50)' `r(max)'  scatter mpg weight, yla(`yla') xla(`xla')

                            Comment

                            Working...
                            X