Announcement

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

  • Allignement of Histogram Bars

    How can I change the alignment of the bars in a histogram without changing the x variable. The bars are centered with the x label. But I would like to have the x labels at the left corner of each bar. Here is my code:

    Code:
    twoway (histogram cont if cont>=-50 & cont<0  , freq discrete lcolor(black) fcolor(blue)) ///
           (histogram cont if cont>=0   & cont<=50, freq discrete lcolor(black) fcolor(red) ),  ///
    xline(0) xlabel(-50(10)50) graphregion(color(white)) xtitle(Continous Measure) ytitle(Number of Observations) legend(off)

  • #2
    You do not present a data example, but the bin, start and width information can guide you in creating the label.

    Code:
    webuse sp500, clear
    histogram volume, bin(5) nodraw
    Res.:

    Code:
     histogram volume, bin(5) nodraw
    (bin=5, start=4103, width=3841.06)
    Implies:

    Code:
    histogram volume, bin(5) xlab(4103(3841)`=3841*6') scheme(s1mono)
    Res.:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	17.5 KB
ID:	1596994

    Last edited by Andrew Musau; 09 Mar 2021, 13:06.

    Comment


    • #3
      Andrew Musau it doesn't work on my end what you are suggesting. Maybe because I am using discrete x variable? There is no built in option in histogram to center the bars manually?

      Comment


      • #4
        What you want is not clear to me, but spikeplot might be closer in spirit here.

        Comment


        • #5
          Originally posted by Trevor Andrew View Post
          Andrew Musau it doesn't work on my end what you are suggesting. Maybe because I am using discrete x variable? There is no built in option in histogram to center the bars manually?

          For most cases, you will not be able to show all the labels if using the -discrete()- option as this will include each observation as a bar (or vertical line). If the bars look like bars, you do not have many observations and there will be some binning going on. Therefore, you have to figure out how Stata is doing the binning and as hinted in #2, the start and width information can guide you on this. If you want anything more specific, the onus is on you to present a data example (see FAQ Advice #12).

          Comment


          • #6
            Originally posted by Nick Cox View Post
            What you want is not clear to me, but spikeplot might be closer in spirit here.
            Nick Cox I am trying to align the x label at the left corner of each discrete histogram bar. Usually the x label is centered. I thought that there is a built in option in the histogram command.

            Comment


            • #7
              You can put the axis labels wherever you want. You may need to spell out binning correspondingly:


              Code:
              sysuse auto, clear
              histogram mpg, width(5) start(10) xla(10(5)40)

              Comment


              • #8
                Nick Cox it does not work with discrete histograms. Here is my data example:


                Code:
                sysuse auto, clear
                histogram turn if turn>=35 & turn<=45, freq discrete xlab(35(1)45)
                I would like that each x label tic is aligned to the left corner of the corresponding bar. E.g. the 35 tic is aligned to the left corner of the first bar 36 tic is aligned to the left corner of the second bar etc.

                Comment


                • #9
                  I’d say the request is contradictory. Discrete bins with width 1 imply integer values and centred labels. I agree with Stata’s logic and would not try to subvert it.
                  Last edited by Nick Cox; 14 Mar 2021, 14:49.

                  Comment


                  • #10
                    Nick Cox the issue is that you have to create the discrete histogram with left or right aligned labels when you want to show graphically the distribution of observations around a cutoff in a regression discontinuity design with a discrete running variable. I have attached an example picture. Is it possible to do something like this?
                    Click image for larger version

Name:	example.PNG
Views:	1
Size:	15.5 KB
ID:	1597791

                    Comment


                    • #11
                      I don't have a different answer. A histogram like that in #10 can be produced by code like this. I try to match the example by using bins of width 0.1 and a start that implies origin 0.

                      Code:
                      clear
                      set seed 2803
                      set obs 1000
                      gen y = rnormal(-1, 0.7)
                      histogram y, width(0.1) start(-4) freq xla(-4/2) xli(0) bfcolor(none) blcolor(gs4) scheme(s1color)
                      Click image for larger version

Name:	trevor_andrew.png
Views:	1
Size:	19.4 KB
ID:	1597811

                      As before, I don't see any need for discrete as an option. .

                      Different colours for different subsets are a different issue, new in #10.

                      Comment


                      • #12
                        Nick Cox thanks a lot for your help. However your solution works only if the x variable is continuous. If x is is discrete you get spaces between the bars and if you change the width to account for the spaces, the height of each bar does not reflect the number of observations. So what I did is, I used the option discrete, moved the x labels by 0.5 to the left so that the each label is aligned to the left corner of the corresponding bar and renamed each label to represent the discrete category.

                        Comment

                        Working...
                        X