Announcement

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

  • Histogram Unequal Margins Question (where the bars begin on the left)

    I have a question about graph histogram, specifically about how to control the space between the first bar and the left side of the plotregion

    I would appreciate it if someone could explain the logic behind the large gap from the left side of the plotregion and the first bar on the graph compared to the small gap on the right side of the graph. Further, is there a way to reduce the size of the gap on the left side?

    Here is a reproducible example showing the issue:
    Code:
    sysuse auto, clear
    hist rep78, percent discrete
    And, here is the resulting graph with red lines I have drawn to bring your attention to the area I am interested in:
    rep78.png


    Thank you in advance for your help.

    Sincerely,
    Alan

  • #2
    I cannot explain the logic behind the default behavior, but you can start by looking at your data and then labeling your axis appropriately.

    Code:
    sysuse auto, clear
    tab rep78
    hist rep78, percent discrete xlab(0(1)6)

    Comment


    • #3
      I think the main principle here is that twoway wants to show zero if it is nearby. Axes start at zero. as you learned early on.

      The easiest work-around I know here is to switch to graph bar

      Code:
      graph bar (percent), over(rep78)

      Comment


      • #4
        Nick Cox , it appears that the opposite is true. Explicitly calling twoway solves this issue, so the behavior can be attributed to the histogram command.

        Code:
        twoway (hist rep78, percent discrete)

        Comment


        • #5
          Interesting. As histogram is itself a wrapper for twoway histogram the question then is what histogram is doing differently from the default.

          Comment


          • #6
            Andrew and Nick,

            Thank you for your replies. I did not know that graph bar could be used without specifying any yvars as Nick suggested. What a great thing to learn! I will pursue that path for the time being.

            I was aware that twoway hist does not exhibit this behavior as suggested by Andrew but I was trying to use the simplified hist command and not specify xlabels.

            This issue may come up because of the discrete option and having the smallest value equal to 1. Interestingly, the following examples do not seem to have the gap:

            Code:
            hist mpg
            hist mpg, discrete
            Again, thank you for your comments and suggestions.

            Sincerely,
            Alan

            Comment


            • #7
              I have played around with this a bit more and tried specifying the xlabel as Andrew suggested, but the gap is still there. So, it seems that using a combination of twoway hist and graph bar is the best solution.

              Sincerely,
              Alan

              Comment


              • #8
                #6 That syntax was added 9 October 2014

                See also the concurrent thread. https://www.statalist.org/forums/for...sic-bar-graphs

                graph bar (originally) not supporting some charts based on counts and percents which I and many students and colleagues wanted was the motive for catplot (SSC), back in 2003.


                Code:
                catplot rep78, recast(bar) percent

                Comment


                • #9
                  I just wanted to note that this issue isn't related to the x-value of zero.

                  Code:
                  webuse auto
                  histogram foreign, discrete xlabel(0 1, valuelabel)
                  Click image for larger version

Name:	06.png
Views:	1
Size:	23.7 KB
ID:	1709961





                  Avoiding the discrete option doesn't help, either:

                  Code:
                  histogram foreign, w(1) start(-0.5) xlabel(0 1,valuelabel) xscale(range(-0.5 1.5))
                  This produces the same figure as above.

                  I'd very much love a solution to this!

                  We do note that the bar graph is a possible work-around, depending on what you're trying to do.

                  Code:
                  graph bar, over(foreign)
                  Click image for larger version

Name:	08.png
Views:	1
Size:	22.7 KB
ID:	1709962



                  More on this here.
                  Last edited by lucas reddinger; 15 Apr 2023, 16:24. Reason: add link to more

                  Comment

                  Working...
                  X