Announcement

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

  • Constrain plotregion proportions

    Dear All,

    I would like to build a graph where the proportions of the plotregion (not the whole graph, but specifically the plotregion) are predetermined.

    Here is an example:
    Code:
    clear all
    sysuse auto
    
    regress price length weight foreign
    predict pricehat
    
    twoway scatter price pricehat if foreign==0  ///
        || scatter price pricehat if foreign==1, ///
           xsize(4) ysize(4) legend(rows(2)) ///
           xlabel(,grid) ylabel(,grid) ///
           title("Some title")
    Below is the output, where I want the size of the red line to be equal to the size of the green line, to match the 1:1 proportion desirable in this case.
    How can I specify the proportions of the plotregion specifically, so that everything else is fit around it automatically? I don't want to have a million different versions of proportions depending on how long the title is or how many categories in the legend, etc.

    My ideal graph would then be slimmer, leaving larger margins left and right, for the same height, to match the 1:1 proportion.

    I am hoping that at least for a 1:1 proportion this can be done using the built-in machinery, as an undocumented property autosquare100 exists, which hopefully serves this purpose.

    Thank you, Sergiy Radyakin

    Click image for larger version

Name:	prop.png
Views:	1
Size:	54.1 KB
ID:	1568760

  • #2
    Specifying an aspect ratio of 1 guarantees that the height and width of the plot region are equal.


    The aspectratio() option controls the relationship between the height and width of a graph's plot region. For example, when #=1, the height and width will be equal (their ratio is 1), and the plot
    region will be square.
    So the only issue that prevents the 1:1 proportion in your example is Stata's tendency to maximize the available space. I do not know an automatic way to prevent Stata from doing this, but you can insist that the range of the axes be at least 10% more than the maximum value of either axis. This combined with the aspect ratio will guarantee the 1:1 proportion regardless of the length of the title or the number of rows in the legend.



    Code:
    clear all
    sysuse auto
    regress price length weight foreign
    predict pricehat
    qui sum price
    local max1= r(max)
    qui sum pricehat
    local max2= r(max)
    local max= max(`max1', `max2') + (0.1* max(`max1', `max2'))
    twoway scatter price pricehat if foreign==0  ///
        || scatter price pricehat if foreign==1, ///
           aspectratio(1) legend(rows(2)) ///
           xlabel(,grid) ylabel(,grid) ///
           xsc(r(. `max')) ysc(r(. `max'))
           title("Some title")

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	37.0 KB
ID:	1568779

    Comment


    • #3
      Dear Andrew,

      thank you very much for pointing me towards the aspectratio() option.
      This is exactly what I needed.

      Apparently I am not the first (and not the only) one who needs this option.
      As a matter of fact, it has been described in a Stata Tip #12 by Nicholas J. Cox
      https://www.stata-journal.com/sjpdf....iclenum=gr0007

      and formal documentation is here:
      https://www.stata.com/manuals/g-3aspect_option.pdf

      PS : for unknown reasons the option appears in the Graph Editor in the properties of the Graph itself, not the plotregion where it belongs according to the class definition, so:
      • HERE: Graph properties -->Aspect/Size --> Aspect ratio
      • NOT HERE: Plotregion1 --> Region --> Aspect ratio
      Thank you very much, and best regards,
      Sergiy Radyakin

      Comment

      Working...
      X