Announcement

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

  • Removing the gap between bars of a twoway histogram

    I'd like to completely remove the gap between bars of a twoway histogram. The option 'gap' defaults to gap(0), which is the shortest gap as gap values must be between 0 and 100. To replicate what I've done:
    Code:
    sysuse auto
    twoway histogram displacement, freq width(10) bcolor(black) blcolor(black) blwidth(none) gap(0) plotregion(margin(zero) lwidth(none))

    Changing the blwidth to something other than 'none' helps but doesn't completely remove the gap between bars:
    Code:
    twoway histogram displacement, freq gap(0) width(10) bcolor(black) blcolor(black) blwidth(medthin) plotregion(margin(zero) lwidth(none))

    Changing the blcolor to something different than the bar color helps to show the problem:
    Code:
    twoway histogram displacement, freq gap(0) width(10) bcolor(black) blcolor(pink) blwidth(medthin) plotregion(margin(zero) lwidth(none))

    I'm guessing there's simply no way to remove the gap completely, but hoping I'm wrong.

  • #2
    EDITED: The question asks how to eliminate any gaps. Get rid of -blwidth()-

    Code:
    sysuse auto, clear
    twoway histogram displacement, freq width(10) bcolor(black) gap(0) plotregion(margin(zero) lwidth(none))
    Last edited by Andrew Musau; 21 Dec 2023, 13:16.

    Comment


    • #3
      Better, but the gaps are still visible between bars. This is easier to see with some color changes:
      Code:
      sysuse auto
      twoway histogram displacement, freq width(10) bcolor(sand) gap(0) plotregion(margin(zero) lwidth(none) icolor(black))
      Last edited by Adam Zahm; 21 Dec 2023, 13:42.

      Comment


      • #4
        That's how the histogram is generated. First, bins are created, and then they are plotted as the attached bars. If you wish to make the individual bar gaps invisible, simply increase the line width.

        Code:
        sysuse auto, clear
        twoway histogram displacement, freq width(10) bcolor(black)  lwidth(3) gap(0) plotregion(margin(zero) lwidth(none))
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	30.8 KB
ID:	1737941

        Last edited by Andrew Musau; 21 Dec 2023, 13:55.

        Comment


        • #5
          To clarify, I don't want to make any bars invisible, I just want to remove the gap between bars.

          The gaps are visibile in your image (meaning the gap between two adjacent bars, not the gaps in the histogram corresponding to lack of data points)

          Comment


          • #6
            It's a typo that is fixed, I meant bar gaps. As I said, you need to increase the width of the bar lines at the risk of making longer bars appear wider than they are. Try -lwidth(5)-, for example. Otherwise, recreate the histogram using twoway bar to achieve what you want.

            Comment


            • #7
              Thanks, so looks like the answer is no, twoway histogram bar gaps cannot be eliminated. Using increased lwidth (e.g. 5) pushes bars out of the inner region onto axes.

              Comment


              • #8
                The example in #3 hinges on the lowest value of displacement being 79 and the highest value being 425. So with a width specified of 10, bars start at 79 and end at 89(10)429.

                That is, a width of 10 doesn't compel bin edges being at multiples of 10. You can see offsets of 1 round about 299 and 399.

                In this case, and many others, a start() option is needed to ensure that bins are aligned with conventionally nice numbers, i.e. multiples of 2, 5 or 10 here.

                I wonder how far what you think you're seeing is a side-effect of this fact.

                Comment


                • #9
                  I can run without the width() option and still observe the gap between bars using lwidth(5):

                  Code:
                  sysuse auto
                  histogram displacement, bcolor(sand) plotregion(margin(zero) lwidth(5) icolor(black))
                  or even lwidth(50)
                  Code:
                  histogram displacement, bcolor(sand) plotregion(margin(zero) lwidth(50) icolor(black))
                  adding start(10), start(11), or start(50) options doesn't eliminate the gaps on my end

                  adding start(10) and width(20) doesn't eliminate the gaps on my end
                  Last edited by Adam Zahm; 21 Dec 2023, 15:43.

                  Comment


                  • #10
                    It appears that the needed options are -lc(none)- -fcolor(something)- combined with -barwidth()-. For the last graph, -barwidth(45)- and for the graph in #4, -barwidth(10.5)- will do it. You set the values based on -width()-.

                    Code:
                    sysuse auto, clear
                    histogram displacement, fcolor(sand) lc(none) plotregion(margin(zero) color(black)) barwidth(45)
                    Click image for larger version

Name:	Graph.png
Views:	1
Size:	31.7 KB
ID:	1737960

                    Comment


                    • #11
                      Thanks, that seems to work well. I can also get rid of the gaps by instead specifying the width and then increasing the barwidth() by 1% of the width. This works without specifying -lc(none)-, but only works if barwidth() is at the end (???)

                      Code:
                      histogram displacement, width(20) bcolor(sand) plotregion(margin(zero) color(black)) barwidth(20.2)

                      below errors as "option barwidth() not allowed"
                      Code:
                      histogram displacement, width(20) barwidth(20.2) bcolor(sand) plotregion(margin(zero) color(black))

                      Comment

                      Working...
                      X