Announcement

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

  • Histogram misbehaving

    I was plotting the simplest of histograms (observations by year) in Stata 14 and finding that the histogram plot was lumping the last year (2016) into the next-to-last year (2015). For example, the following code generates the tabulation and histogram below. What is going on here?

    clear all
    set obs 200
    set seed 10
    gen year = floor(runiform(2010,2017))
    tab year
    histogram year, start(2010) width(1)

    Click image for larger version

Name:	Tabulation.PNG
Views:	1
Size:	8.2 KB
ID:	1493608


    Click image for larger version

Name:	Histogram.png
Views:	1
Size:	9.5 KB
ID:	1493609

  • #2
    Code:
     histogram year, discrete
    solves this problem while also helpfully producing more natural centred (centered) labels. For your example I would want to add further options

    Code:
    xla(2010/2016, noticks) xtitle("")
    as there is enough space to label every year; ticks no longer seem needed; and the reader who needs a title "year" is probably in the wrong field.

    Comment


    • #3
      try using the "discrete" option as in:
      Code:
      histogram year, discrete w(1)

      Comment


      • #4
        Thanks, Nick and Rich.

        The discrete option prevents the improper binning of 2016 into 2015 and I can see that it is worthwhile to specify whether one's data discrete or continuous. Nonetheless, if the histogram options start(#) and width(#) function in the way the documentation suggests, I'm perplexed by the final bin seeming to be defined differently than the preceding bins.

        Comment


        • #5
          #4 I agree. I have to wonder whether there is small bug somewhere.

          Note that

          Code:
          histogram year, start(2009.5) width(1) freq
          works fine.

          Comment

          Working...
          X