3

I am working with the community-contributed command coefplot in Stata.

I have a large number of estimated coefficients, which I would like to plot on the same graph.

As such, I would like to reduce the spacing between coefficients.

Consider the following toy example using Stata's auto toy dataset:

quietly sysuse auto, clear
quietly regress price mpg trunk length turn
coefplot, drop(_cons) xline(0)

enter image description here

How can the spacing between Mileage (mpg) and Trunk space (cu. ft.) be decreased?

user5477262
  • 33
  • 1
  • 4
  • I am not an expert, but the spacing seems to be automatic, the more variables you add, the smaller the spacing becomes. For example, if your regression was `quietly regress price mpg trunk length turn rep78 trunk weight displacement gear_ratio`, then the labels would be closer because there are more things to graph. You can use the option `coeflabels(,labsize(vsmall))` to make the text size smaller if that helps – Eric HB Aug 20 '17 at 23:39
  • Why not choose a small `ysize()`? – Nick Cox Aug 21 '17 at 07:14
  • @Eric choosing a smaller text size does not reduce the amount of space. – user5477262 Aug 21 '17 at 09:13
  • @Nick choosing a smaller ysize() makes the graph harder to read without reducing the spacing. I tried it, but perhaps I am missing something. – user5477262 Aug 21 '17 at 09:16
  • The spacing must be less on a smaller graph. Perhaps I am missing something in turn. What kind of graph do you imagine that the same size but has smaller spacing? (Note that you can tweak the label text size too.) Another thing you can do is change the aspect ratio. – Nick Cox Aug 21 '17 at 09:36
  • @Nick - roughly I'm interested in reducing the spacing/axis font size ratio. The aspect ratio suggestion works well but has the downside of producing a lot of white space at the top and bottom of the plot region. This is my code: quietly sysuse auto, clear /// quietly regress price mpg trunk length turn /// coefplot, drop(_cons) xline(0) aspectratio(0.2) plotregion(margin(0 0 0 0)) – user5477262 Aug 21 '17 at 10:10
  • So, you know ways of tweaking the graph settings. Bottom line is that you have the graphical equivalent of a four line table, and it doesn't need that much space. – Nick Cox Aug 21 '17 at 10:14
  • @Nick yes in this working example it is the equivalent of a small table, but the graph that I want to produce should plot point estimates and confidence intervals for about ~20 variables (hence the need to conserve space) I'm not sure if there is a solution to the extra white space having looked here http://ageconsearch.umn.edu/bitstream/116256/2/sjart_gr0007.pdf and here http://www.stata.com/manuals13/g-3aspect_option.pdf – user5477262 Aug 21 '17 at 10:30
  • I'd ask about your real problem with a realistic example then. If you have 20 variables, where is the extra space that you need to remove? You must have space to show names of variables; else the graph uses most of its value. – Nick Cox Aug 21 '17 at 10:39
  • @Nick I will in future ask a question with a more realistic example. Even with 20 variables the space between the names remains the same. The names would be clearly legible even with half the space between them. I can manually edit the image with the altered aspect ratio after it has been saved to remove the white space. Am I correct in assuming that that would be the best way forward? – user5477262 Aug 21 '17 at 10:50
  • I can't see your real example data or graph or code to comment. The ways of changing what you see that I know are to modify `aspect() xsize() ysize()` and the text through `ylabel()`. I still have no clear idea of what other space you're talking about. – Nick Cox Aug 21 '17 at 10:55
  • @Nick. The spacing between the lines seems to be automatic but I would like to control this. Using xsize(10) and ysize(5) reduces the space between the lines but seems to also reduce the font sizes of the names. Using aspect(0.5) fixes the problem but generates a lot of white space around the plot region which would need to be edited manually. I'll go with this option. Thank you. – user5477262 Aug 21 '17 at 11:13

1 Answers1

2

Some white space around the graph is an unavoidable limitation because of how Stata's graphics system works. With that said, an alternative way around this (which does not tinker with the aspect ratio of the graph) is to increase the range of the y-axis.

For example:

forvalues i = 1 / 4 {
    coefplot, drop(_cons) xline(0) yscale(range(-`i' `=6+`i''))                                                 
}

enter image description here

A different but related approach, is to turn off the y labels entirely and use marker labels instead:

forvalues i = 1 / 4 {
    coefplot, drop(_cons) ///
              xline(0) ///
              yscale(range(-`i' `=6+`i''))  ///
              yscale(off) ///
              mlabels(mpg = 12 "Mileage" ///
                      trunk = 12 "Trunk space (cu. ft.)" ///
                      length = 12 "Length (in.)" ///
                      turn = 12 "Turn Circle (ft.)")
}

enter image description here

In both approaches, the starting and ending positions (i.e. the amount of space above and below the labels) can be set by tweaking the values specified within the range() suboption.

Note that the grid lines can be turned off by using the option grid(none).

In addition, by combining the at(matrix()) option and yscale(range()) one can allow for unequal reductions in the distance of the coefficients:

matrix A = (0.2,0.21,0.22,0.225,0.255)

coefplot, drop(_cons) ///
          xline(0) ///
          yscale(range(0.18 0.26)) ///
          yscale(off) ///
          mlabels(mpg = 12 "Mileage" ///
                  trunk = 12 "Trunk space (cu. ft.)" ///
                  length = 12 "Length (in.)" ///
                  turn = 12 "Turn Circle (ft.)") ///
          at(matrix(A)) ///
          horizontal

enter image description here