Announcement

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

  • Proble with some graphs

    Dear all,

    I'm having some problems with a graph that is generated through a double for. In particular, I'm generating a graph for each of 11 different countries and conditioning the variables to three different categories of tenure. This is the code I'm dealing with

    Code:
    local save_path "C:\Users\39333\Desktop\ECB CES\graphs\expectations, country and tenure"
    
    local countries "AT BE DE EL ES FI FR IE IT NL PT"
    
    foreach tenure_class in 1 2 3 {
        foreach country in `countries' {
        twoway /// 
            (line E_i_mortg1y_pe wave if country == "`country'" & tenure == `tenure_class', lcolor(blue) lpattern(solid)) /// 
            (line Euro_cost_of_mortgage wave if country == "`country'" & tenure == `tenure_class', lcolor(blue) lpattern(dot)) /// 
            (line E_P1y_pe wave if country == "`country'" & tenure == `tenure_class', lcolor(red) lpattern(solid)) ///
            (line P_P1y_pe wave if country == "`country'" & tenure == `tenure_class', lcolor(green) lpattern(solid)) ///
            (line CPI_overall wave if country == "`country'" & tenure == `tenure_class', lcolor(red) lpattern(dot)) ///
            (line E_unempl_1y_pe wave if country == "`country'" & tenure == `tenure_class', lcolor(black) lpattern(dash) yaxis(2)), ///
            title("Various Exp. -`country'- Tenure `tenure_class'") /// 
            xtitle("Waves") ytitle("Rates") ytitle("E_unemp", axis(2)) /// 
            ylabel(0(2)12) ylabel(0(5)30, axis(2)) ///
            legend(order(1 "E_mortg" 2 "Cost_mortg" 3 "E_prices1y" 4 "P_prices1y" 5 "CPI" 6 "E_unemp"))
            graph export "`save_path'/various_exp_`country'_tenure`tenure_class'.png", replace
    }
    }
    the problem is that it generates me images with different labels of the yaxis (the xaxis is not a problem since the time period is the same for every country), not allowing me for an easy comparison.

    various_exp_AT_tenure1.pngvarious_exp_AT_tenure2.png
    Of course I know that STATA uses the maximum value of one of the variables that you put inside and rescale the axis to that, but I'm asking, is it possible, in a for cycle like the one I posted, to have a yaxis that is constant for the different tenures level within the country (the axis between countries can even change since that some of them are very different)?. The Idea would be to take the maximum level of the variables in the legend between the three different tenures, and to refer to that for each country. How can I do that?

    Thank you very much

  • #2
    What you are looking for is the -range()- suboption of -yscale()-. The axis will extend a little above the observed y-max value, so you want to add some small value to this maximum.

    Assuming y-max value is defined in a local "ymax"
    Code:
    local maxval = `ymax' + 1
    twoway ..., ... ysc(range(. `maxval'))
    See

    Code:
    help axis_scale_options

    Comment


    • #3
      On a different level: the choice of red and green together is routinely advised against for graphics given the large numbers of people who have difficulty distinguishing these colours. The stcolor scheme introduced in Stata 18 is one of several that allow good choices here.

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        What you are looking for is the -range()- suboption of -yscale()-. The axis will extend a little above the observed y-max value, so you want to add some small value to this maximum.

        Assuming y-max value is defined in a local "ymax"
        Code:
        local maxval = `ymax' + 1
        twoway ..., ... ysc(range(. `maxval'))
        See

        Code:
        help axis_scale_options
        Thank you, it works definitely better, it is still not considering that I would have the maximum value for the three tenures to rescale for the other two (so, for istance, BG of tenure 1 has the rates that go from 0 to 20, while BG for tenure 2 from 0 to 15 instead of going up to 20), I'm trying to figure it out the help code that you mentioned.

        Comment


        • #5
          You can summarize the variables and then pick up the maximum [stored as r(max)]. Please provide a reproducible data example using the dataex command for specific code suggestions. See FAQ Advice #12 for details.

          Comment


          • #6
            The big picture here is 6 outcomes X 11 countries X 3 tenure classes. The outcomes are in different units.

            Quite what you want as end result isn't clear, but

            All tenures on the same graph seems more likely to be digestible than all outcomes on the same graph.

            it is easier to control (and especially reduce) repetition if you use by() in place of one loop. Repeating the legend is particularly wasteful of space.
            Last edited by Nick Cox; 12 Dec 2024, 06:50.

            Comment


            • #7
              On Nick's penultimate point, see https://journals.sagepub.com/doi/pdf...36867X20976341.

              Comment


              • #8
                thank you very much to everyone, I sorted the problem. Next time I'll be more clear about the dataset and the aims

                Comment

                Working...
                X