Announcement

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

  • Stopping my horizontal legend from overlapping with the numbers below the axis

    I wanted to create a histogram, so typed
    Code:
    hist variable, fraction width(0.2)
    Unfortunately, the 'horizontal legend' (i.e. the horizontal text just below the x-axis) now overlaps with the numbers just below the x-axis. I believe that this is because I have changed the default font for graphs to Latin Modern.

    I have tried various approaches to fix this, but none of them work for me. The closest I have got was by writing
    Code:
    hist variable, fraction width(0.2) xsize(10)
    While this seems to have created a gap, it also stretched the histogram a lot horizontally.

    Is there a simple fix to this? I feel like a solution must have been posted somewhere, but after a lot of Googling I can't find it... (This may reflect my limited Googling skills!)

    Note: I am using grstyle and would especially appreciate a solution that uses this. I do know how to drag the legend downwards using the graph editor.
    Last edited by Itzhak Rasooly; 12 Feb 2024, 11:39.

  • #2
    #1 is needlessly abstract. Please show example data leading to a graph showing the problem together the exact code used to create it.

    Comment


    • #3
      Sure, here is an example.

      Suppose that the variable is called var1 and takes values: 0, 1, 2, 3, 4, 5. I write
      Code:
      label variable var1 "Variable description"
      hist var1, fraction width(1)
      As mentioned before, my default font is Latin Modern. Here's the resulting histogram:
      Click image for larger version

Name:	example_stata.png
Views:	1
Size:	91.8 KB
ID:	1742977


      While the horizontal legend doesn't quite overlap with the numbers, I would like it to be shifted downwards.
      Last edited by Itzhak Rasooly; 12 Feb 2024, 12:17.

      Comment


      • #4
        Incidentally, my background is white so shifting the text downwards won't look strange. My code is more like
        HTML Code:
        grstyle init // initiates grstyle
        grstyle set plain, compact nogrid // imposes the plan and compact setting without gridlines
        grstyle set size small: subheading  // makes the axis titles a bit smaller
        grstyle set size small: axis_title  // makes the axis titles a bit smaller
        
        label variable var1 "Variable description"
        hist var1, fraction width(1)
        which generates the following histogram
        Click image for larger version

Name:	stata_another.png
Views:	1
Size:	98.7 KB
ID:	1742983

        Comment


        • #5
          Thanks for the detail, which does clarify the question as being about something different.

          Stata regards what you're writing about not as legend -- indeed this graph has no legend at all in Stata's sense -- but as the x axis title.

          Code:
          xsc(titlegap(*5))
          is an instance of what you can try as an extra option, where 5 may be too much or not enough, depending.

          Not the question, but for data like this, you surely need also the discrete option to ensure that the axis labels appear under the centre of each bar.

          Comment


          • #6
            Thanks so much -- this totally solves the problem! And yes, agree that discrete would be better for that dataset.

            Comment

            Working...
            X