Announcement

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

  • Graph: Large gap between xlabel and legend

    Hi

    I am trying to make a graph similar to this one:
    Code:
    sysuse auto, clear
    gen xvar = _n
    keep if xvar<7
    
    la def myxlabel 1 "Not very long", replace
    la def myxlabel 2 "Also short", add
    la def myxlabel 3 `" "Quite a" "long label" "', add
    la def myxlabel 4 `" "This one is" "even longer" "', add
    la def myxlabel 5 `" "Again a very" "very long label" "', add
    la def myxlabel 6 "Short again", add
    la val xvar myxlabel
    
    twoway scatter trunk xvar || scatter rep78 xvar, xlabel(1(1)6, val angle(90) labsize(medsmall)) xtitle("") legend(order(1 "See space above" 2 "how to remove it?"))
    As you see if you run the code, there is a large gap between the labels of the x-axis and the legend. If I remove the legend, the large space below the xlabel remains. Does anyone know how to remove this extra-space?

    Thanks and best wishes,
    Thomas


    Last edited by Thomas Moehr; 07 May 2021, 02:41. Reason: add tags

  • #2
    I guess "Don't even try that; the graph will look horrible even with the problem fixed" is not the answer you seek. Nor perhaps is advice to move the legend to pos(12).

    I imagine the problem is that graph is not taking account of the split lines. It's looking at something like the total length of the text for the axis label. in calculating where to put the legend.

    Comment


    • #3
      Originally posted by Thomas Moehr View Post
      Does anyone know how to remove this extra-space?
      You can force a negative margin outside the x-axis title.

      Code:
      sysuse auto, clear
      gen xvar = _n
      keep if xvar<7
      
      la def myxlabel 1 "Not very long", replace
      la def myxlabel 2 "Also short", add
      la def myxlabel 3 `" "Quite a" "long label" "', add
      la def myxlabel 4 `" "This one is" "even longer" "', add
      la def myxlabel 5 `" "Again a very" "very long label" "', add
      la def myxlabel 6 "Short again", add
      la val xvar myxlabel
      
      twoway scatter trunk xvar || scatter rep78 xvar, xlabel(1(1)6, val angle(90) labsize(medsmall)) ///
      xtitle("") legend(order(1 "See space above" 2 "how to remove it?")) xsc(outergap(-30))
      Res.:
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.8 KB
ID:	1608189

      Comment


      • #4
        Originally posted by Andrew Musau View Post

        You can force a negative margin outside the x-axis title.

        [...]
        Thank you very much! It works perfectly.

        Comment

        Working...
        X