Announcement

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

  • Graph becomes illegible when using graph combine

    Dear All,

    I want to present how the indicators evolved over time. The first group is the equity ratio: the minimum, the average and the maximum. The second group is the disposible income ratio: the minimum, the average and the maximum. The y range for the first group is from 5 to 25%, the y range for the second group is -2 to 20%. The time span for the first: 2005q4-2021q4, the second: 2014q4-2021q4. I want to present the two graphs in one graph. It is important to me that I have as many quarters as possible and only one legend on the middle on the final graph. I used the codes:



    Code:
    graph twoway line x1 x2 x3 time, sort ///
    ylabel(5(2)25, angle(0)) xlabel(183(4)247, angle(90)) ///
    legend(label(1 "Minimum") label(2 "Average") label(3 "Maximum")) ///
    ysca(titlegap(4)) xsca(titlegap(4)) ///
    name(graph1)
    
    graph twoway line z1 z2 z3 time, sort ///
    ylabel(0(2)20, angle(0) xlabel(216(4)247, angle(90)) ///
    legend(label(1 "Minimum") label(2 "Average") label(3 "Maximum")) ///
    xsca(titlegap(4)) ysca(titlegap(4)) ///
    name(graph2)
    
    graph combine graph1 graph2
    when I produce the graph 1 and 2 everything looks good. When I combine the graphs the y-axis is illegible. I want to have only one legend and as many quarters as possible. I hid the legend field of one graph when editing. However one of the graphs become bigger and I cannot set the legend field in the middle.

    Please help me

  • #2
    You have to help us to help you. There is no example data here -- that should not be hard, as you need to show us just 65 observations and 7 variables -- and no example graph either. For graph problems we really need to see what the problem is. The answer I can give on this is no more than trivial. You have two graphs and legends too and you want to squeeze them together and for everything to remain just as legible. That will not work. You know that, which is why you're posting, but a solution is much harder without the details I am asking for. Otherwise put, your goals appear incompatible, so the question is which one to sacrifice.

    Comment


    • #3
      Dear Guest,

      As per FAQ #12, please provide data that is required to create a reproducible example using your code (#1).
      Do not upload a dta, csv or txt file, but instead provide (a random selection of) your data with dataex, as per FAQ #12.2.
      Type in your Stata:
      Code:
      ssc install dataex
      h dataex
      Last edited by sladmin; 28 Aug 2023, 08:35. Reason: anonymize original poster
      http://publicationslist.org/eric.melse

      Comment


      • #4
        Thank you for your answers. Unfortunately I cannot share my data since it is sensitive. After some browsing I found grc1leg,legendfrom(graph1). I had some issues with installing it. But eventually found an ado file on the Internet. It solved my legend problem.
        However, I had to qive up on presenting the quarters

        Comment


        • #5
          The question of sensitive data is already addressed in the FAQ Advice.
          If your dataset is confidential, then provide a fake example instead.
          Without your data I can only simulate. The cardinal rules here are

          1. General. Lose the legend (if you can). Kill the key! Use direct text labelling.

          2. General. Recasting the problem so that you can use by() is often going to be much less clumsy than using graph combine. See https://journals.sagepub.com/doi/pdf...36867X20976341

          3. Particular. Even though one set of series is only available for some of the time, using a different x axis extent won't make comparison easier.

          Code:
          clear
          
          * sandbox
          
          set obs 75
          gen t = yq(2005, 3) + _n
          format t %tq
          
          local means 20 15 10 16 9 2
          
          foreach v in x1 x2 x3 z1 z2 z3 {
              gettoken mean means : means  
              gen `v' = rnormal(`mean', 1)
          }
          
          * you start here
          
          preserve
          stack t x1 x2 x3 t z1 z2 z3, into(t y1 y2 y3) clear
          label def _stack 1 "equity ratio" 2 "disposable income ratio"
          label val _stack _stack
          
          su t, meanonly
          local tmax = r(max)
          local XMAX = `tmax' + 7
          tokenize "max mean min"
          forval j = 1/3 {
              gen text`j' = "``j''" if t == `tmax'
          }
          
          
          set scheme s1color
          line y1 y2 y3 t, by(_stack, col(1) note("")  legend(off) yrescale) lcolor(blue black red) subtitle(, orient(vertical) fcolor(none) pos(9) nobox nobexpand) || scatter y1 y2 y3 t if t == `tmax', ms(none ..) mla(text1 text2 text3) mlabc(blue black red) mlabsize(medium ..) xtitle("") xsc(r(. `XMAX')) xla(180(20)240, format("%tq") grid glc(gs12))
          Click image for larger version

Name:	emilia.png
Views:	1
Size:	73.1 KB
ID:	1667465


          Comment


          • #6
            Thank you Nick for you help . I used the code for my problem and I'm glad with the final result.

            I tried to understand the second part of the code. I'm confused with local XMAX=`tmax' + 7. I know you used it in xscale so that the labels are after the lines. I was running the code piece by piece and saw that without it there was a substantial gap between the line and text labelling. However, I don't know what exactly stata has done

            Comment


            • #7
              I lengthened the x axis to allow space for the text labels. You seem to be getting that, but I can't understand your new question otherwise.

              I can only echo advice given repeatedly.

              Code. Please show your exact code.

              Data. Please show data. If your data are sensitive, use fake data or a standard dataset that show the problem and tell us what they are.

              Example. Don't talk about problems, please show us what the problem is with an example. if it's a graph, show a graph.

              Comment


              • #8
                Thank you Nick. The next time I will try to provide data example. I'm struggling with data generating process though. That's the only reason I didn't provide it

                Comment

                Working...
                X