Announcement

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

  • Renaming graph labels

    For the bar graphs that are created using the data below, I am having trouble renaming the macro variable names in the title and ytitle of the resulting graphs. I would like to replace "business" with "Business Activity" and "outlook" with "Company Outlook". I could just create two separate graphs and manually label them, but the actual data have a lot more variables and I would preferably like to use a loop if I can. Also, is there a better way to tlabel the quarters on the x-axis instead of manually labeling each date? I appreciate any suggestions.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float date double(business outlook)
    674 -42.1 -24.5
    677  13.8    19
    680  26.7  19.6
    683  40.1  57.1
    686  41.8  45.6
    689  37.3  20.3
    692  27.3  28.2
    695  38.1    52
    698  40.7  43.2
    701  44.5  45.3
    704  43.3  46.4
    707   2.3 -10.2
    710  10.8  23.3
    713   -.6  -4.5
    716  -7.4     0
    719  -4.2   1.9
    722 -50.9   -75
    725 -66.1   -51
    728  -6.6   1.9
    731  18.5  21.6
    734  53.6  70.6
    737    53  71.9
    740  44.3  58.9
    743  42.6  53.2
    746    56  76.3
    749  57.7  65.9
    752    46  33.1
    755  30.3  13.1
    758   2.1 -14.1
    end
    format %tm date
    
    
    
    foreach var of varlist business outlook {
        twoway (bar `var' date), ///
                tlabel(2016m3 "2016 Q1" 2016m6 "2016 Q2" 2016m9 "2016 Q3" 2016m12 "2016 Q4" 2017m3 "2017 Q1" 2017m6 "2017 Q2" 2017m9 "2017 Q3" 2017m12 "2017 Q4" 2018m3 "2018 Q1" 2018m6 "2018 Q2" 2018m9 "2018 Q3" 2018m12 "2018 Q4" ///
                    2019m3 "2019 Q1" 2019m6 "2019 Q2" 2019m9 "2019 Q3" 2019m12 "2019 Q4" 2020m3 "2020 Q1" 2020m6 "2020 Q2" 2020m9 "2020 Q3" 2020m12 "2020 Q4" 2021m3 "2021 Q1" 2021m6 "2021 Q2" 2021m9 "2021 Q3" 2021m12 "2021 Q4" ///
                    2022m3 "2022 Q1" 2022m6 "2022 Q2" 2022m9 "2022 Q3" 2022m12 "2022 Q4" 2023m3 "2023 Q1", labsize(medsmall) angle(45)) ///
                ylabel(-80(20)60, angle(0) labsize(medsmall)) ///
                ttitle("") ///
                title("Survey - `var' Index") ///
                ytitle("`var' Index (prior quarter)")
    
        graph save "Survey - `var' Index", replace
    }
    Last edited by Navi Reddy; 05 May 2023, 15:46.

  • #2
    Hey Navi,

    you could define two "variable" locals in the following way:

    Code:
    local t_business "Business Activity"
    local t_outlook "Company Outlook"
    
    foreach var of varlist business outlook {
        twoway (bar `var' date), ///
                tlabel(2016m3 "2016 Q1" 2016m6 "2016 Q2" 2016m9 "2016 Q3" 2016m12 "2016 Q4" 2017m3 "2017 Q1" 2017m6 "2017 Q2" 2017m9 "2017 Q3" 2017m12 "2017 Q4" 2018m3 "2018 Q1" 2018m6 "2018 Q2" 2018m9 "2018 Q3" 2018m12 "2018 Q4" ///
                    2019m3 "2019 Q1" 2019m6 "2019 Q2" 2019m9 "2019 Q3" 2019m12 "2019 Q4" 2020m3 "2020 Q1" 2020m6 "2020 Q2" 2020m9 "2020 Q3" 2020m12 "2020 Q4" 2021m3 "2021 Q1" 2021m6 "2021 Q2" 2021m9 "2021 Q3" 2021m12 "2021 Q4" ///
                    2022m3 "2022 Q1" 2022m6 "2022 Q2" 2022m9 "2022 Q3" 2022m12 "2022 Q4" 2023m3 "2023 Q1", labsize(medsmall) angle(45)) ///
                ylabel(-80(20)60, angle(0) labsize(medsmall)) ///
                ttitle("") ///
                title("Survey - `t_`var'' Index") ///
                ytitle("`t_`var'' Index (prior quarter)")
    
        graph save "Survey - `var' Index", replace
    }
    Best
    Sebastian

    Comment


    • #3
      You have more problems than stated. You don't show the graphs so far but by labelling every date your axis labels are numerous -- which is why you have to resort to ang(45)-- and repetitive..

      Much of the difficulty here is that you have quarterly data organized by monthly dates. So, use quarterly dates instead.

      It is possible to be more ambitious in ways explained at https://www.stata-journal.com/articl...article=gr0030 and https://journals.sagepub.com/doi/pdf...6867X221141058

      What I suggest is a combination of minor labels for each quarter, labels for each year, and a subtle grid showing year ends. Here is the code using mylabels and myticks from the second source above. In an up-to-date Stata

      Code:
      search mylabels, sj
      will point to gr0092 to download the files. Note that myticks is just being used to find where to put the grid lines (not for ticks). Here is the code and a sample graph. If this were my data I would use lighter fill color for the bars.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float date double(business outlook)
      674 -42.1 -24.5
      677  13.8    19
      680  26.7  19.6
      683  40.1  57.1
      686  41.8  45.6
      689  37.3  20.3
      692  27.3  28.2
      695  38.1    52
      698  40.7  43.2
      701  44.5  45.3
      704  43.3  46.4
      707   2.3 -10.2
      710  10.8  23.3
      713   -.6  -4.5
      716  -7.4     0
      719  -4.2   1.9
      722 -50.9   -75
      725 -66.1   -51
      728  -6.6   1.9
      731  18.5  21.6
      734  53.6  70.6
      737    53  71.9
      740  44.3  58.9
      743  42.6  53.2
      746    56  76.3
      749  57.7  65.9
      752    46  33.1
      755  30.3  13.1
      758   2.1 -14.1
      end
      format %tm date
      
      * get quarterly dates, and put those in data into a local macro
      gen qdate = qofd(dofm(date))
      levelsof qdate, local(qlevels)
      
      * yearly dates ditto
      gen ydate = year(dofq(qdate))
      levelsof ydate, local(ylevels)
      
      * we are going to put years as labels in the middle of each year, so
      * that is halfway between quarter 2 and quarter 3
      mylabels `ylevels', myscale(yq(@, 2) + 0.5) local(years)
      
      * we are going to put lines at the beginning of each year, so
      * subtract 0.5 from the position of bar for quarter 1
      myticks `ylevels', myscale(yq(@, 1) - 0.5) local(lines)
      
      
      local g = 1
      
      * version 18 default if you have it installed
      local scheme = cond(c(version) == 18, "stcolor", "s1color")
      set scheme `scheme'
      
      foreach var of varlist business outlook {
          twoway (bar `var' qdate), xmlabel(`qlevels', noticks format("%tqq") labsize(small)) ///
                  xlabel(`years', tlc(none) tlength(*3)) ///
                  ylabel(-80(20)60, angle(0) labsize(medsmall)) ///
                  ttitle("") ///
                  title("Survey - `var' Index") ///
                  xtitle("") ///
                  xli(`lines', lc(gs12) lp(solid))
                  ytitle("`var' Index (prior quarter)") name(G`g', replace)
                  
          local ++g
      }
      Click image for larger version

Name:	quarterly.png
Views:	1
Size:	37.2 KB
ID:	1712501


      EDIT I didn't see the post in #2 while I was writing this, but I can't see that it solves any of the real problems with the code. It just makes the code more complicated.
      Last edited by Nick Cox; 06 May 2023, 03:18.

      Comment


      • #4
        Sorry, the EDIT in #3 is unfair and wrong. The code in #2 solves the specific problem it addresses. I would fix the problem by using better variable labels myself.

        Comment


        • #5
          Thank you Sebastian Schirner and Nick Cox. Some great suggestions. Here is what I came up with using both your inputs.

          Click image for larger version

Name:	G2.png
Views:	1
Size:	47.2 KB
ID:	1712528


          Nick - thank you also for some useful articles on automating axis labels. There is so much to learn. The final graph looks much nicer without the cluttered x-axis. I am using Stata 17. You also mention in #3 that "you would fix the problem by using better labels". Can you please elaborate? Thanks again for your time.

          Comment


          • #6
            If you define variable labels ahead of graphics they will appear automatically on the y axis.

            Code:
            label var business "Business Activity"
            label var outlook "Company Outlook"

            Comment


            • #7
              Got it. Thank you, Nick!

              Comment

              Working...
              X