Announcement

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

  • Change in y-axis range when using -graph combine ..., ycommon

    Sometimes when I combine two graph panels into one figure using graph combine ..., ycommon, it extends the range of the y-axis longer than the original ranges. The following minimum working example illustrates the problem:

    Code:
    ** graph combine adding extra yaxis space
    
    clear
    input x y1 y2
        1 6.6 6.4
        2 1   3
    end
    
    summarize y1, meanonly
    local max_y1 = r(max)
    
    summarize y2, meanonly 
    local max_y2 = r(max)
    
    twoway scatter y1 x, yscale(range(0 `max_y1')) ylabel(0(1)6) ///
        name(y1graph, replace)
    
    twoway scatter y2 x, yscale(range(0 `max_y2')) ylabel(0(1)6)  ///
        name(y2graph, replace)
    
    graph combine y1graph y2graph, ycommon
    The original ranges are from 0 to 6.6 and 0 to 6.4, but graph combine adds a lot of extra vertical space (changing the range to go up to 8). The attached images show the issue.

    I know one solution is to use max(`max_y1',`max_y2') as the upper limit of the range of each original scatterplot, then use graph combine without ycommon. Although this solution works in this minimum working example, it won't do in my actual application. Is there a way to get graph combine, ... ycommon to respect the original graph ranges without adding the extra vertical space that can be seen in the attached two-graph figure?
    Attached Files

  • #2
    Omitting the 'ycommon' option should solve the problem:

    Code:
    graph combine y1graph y2graph
    Added: Not sure why 'grcombine' command occupying the extra space.
    Roman

    Comment


    • #3
      Thanks Roman. That does not solve the problem though because I want the same y-axis in both graphs and they initially have different y-axis ranges. As I mentioned in my original post:

      I know one solution is to use max(`max_y1',`max_y2') as the upper limit of the range of each original scatterplot, then use graph combine without ycommon. Although this solution works in this minimum working example, it won't do in my actual application. Is there a way to get graph combine, ... ycommon to respect the original graph ranges without adding the extra vertical space that can be seen in the attached two-graph figure?

      Comment


      • #4
        Some weird things going on here:

        Code:
        forval i=1/2 {
        su y`i'
        ret li
        }
        
            Variable |        Obs        Mean    Std. Dev.       Min        Max
        -------------+---------------------------------------------------------
                  y1 |          2         3.8    3.959798          1        6.6
        
        scalars:
                          r(N) =  2
                      r(sum_w) =  2
                       r(mean) =  3.799999952316284
                        r(Var) =  15.67999946594239
                         r(sd) =  3.959797907209709
                        r(min) =  1
                        r(max) =  6.599999904632568
                        r(sum) =  7.599999904632568
        
            Variable |        Obs        Mean    Std. Dev.       Min        Max
        -------------+---------------------------------------------------------
                  y2 |          2         4.7    2.404163          3        6.4
        
        scalars:
                          r(N) =  2
                      r(sum_w) =  2
                       r(mean) =  4.700000047683716
                        r(Var) =  5.780000324249272
                         r(sd) =  2.404163123469219
                        r(min) =  3
                        r(max) =  6.400000095367432
                        r(sum) =  9.400000095367432
        If we do a simple scatter of y1 on x,

        Code:
        twoway scatter y1 x


        We get a default yaxis of 8 while the highest observed value for y1 is 6.59. I assumed 'scatter' command is defining the yaxis from the scalar r(sum) which is 7.59. But this is not even true because if you run the next graph:

        Code:
        twoway scatter y2 x
        This gives us a default yaxis with 7 which is based on r(max) = 6.4 rounded whereas the scalar r(sum) is 9.40.

        I would like to see what solution comes to this as the bottomline is that you are not getting what you are asking Stata to accomplish. At least from the context of your local macros r(max) being defined beforehand. If no solution comes, may be StataCorp people can help.



        Roman

        Comment


        • #5
          Response from Stata Technical Support is:

          Stata tries to find the "best fit" for the number of tick marks, labels, etc.
          In this case, the -ycommon- option is finding the best fit by using 8, and
          is overwriting the -ylabel- and -yscale- options. It's best to use -ycommon-
          when you want Stata to calculate the common scales.

          So basically -ycommon- does a poor job at finding the "best fit" common scale in this case, and there is no way to ask Stata to graph combine with a common axis that respects the original graph's ranges (taking the min and max of them to form the range of the combined graph), which in my view would be a desirable feature.

          Comment


          • #6
            Has anyone solved this problem?
            I am having the same nightmare: combining graphs yields the final graph with a weird y-axis extension. I have never really understood why -graph combine , ycommon - does not come with the ability to specify something about the common y-axis...
            Best I can do is export to PDF then crop it.
            Scott

            Comment

            Working...
            X