Announcement

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

  • How to colour bars in a histogram or graph bar

    Hi there,

    I have been trying to colour individual bars in a histogram or graph bar with percentages, but I don't get round to it without messing the bars heights. If we follow the following MWE:

    Code:
    sysuse auto,clear
    hist mpg
    Click image for larger version

Name:	Annotation 2020-03-02 231515.png
Views:	1
Size:	12.4 KB
ID:	1539324


    I would like to colour the last 3 bars, just that.

    Any help would be appreciated.

    KR

  • #2
    There is a backstory to this. Five years ago, I taught an introductory course in econometrics and generated a histogram showing that you can get negative predicted probabilities with the linear probability model (LPM). Last year, looking through my slides, I could not figure out how I was able to shade a subset of those bars. After several failed attempts, I just figured that I had used the graph editor. So this is the code I used.

    Code:
    *THE LINEAR PROBABILITY MODEL (LPM)
    webuse lbw
    regress low age lwt i.race smoke ptl ht ui
    predict yhat, xb
    *NEGATIVE PREDICTED PROBABILITIES WITH THE LPM
    hist yhat, bin(30) color(gray)
    forval i=1/4{
          gr_edit .plotregion1.plot1.EditCustomStyle , j(`i') style(area(shadestyle(color(navy8))))
    }
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	15.4 KB
ID:	1539420

    Last edited by Andrew Musau; 03 Mar 2020, 06:21.

    Comment


    • #3
      *
      Last edited by John Mullahy; 03 Mar 2020, 08:38. Reason: edited: sent before completion

      Comment


      • #4
        Here's another case where an undocumented command can come to the rescue. The undocumented command
        Code:
        twoway__histogram_gen
        generates the underlying data for a histogram. (Note: two underscores after twoway)

        Consider this code:
        Code:
        preserve
        drop _all
        sysuse auto
        twoway__histogram_gen mpg, gen(h x, replace)
        local wx=x[2]-x[1]
        twoway (bar h x if x<30, color(orange) barwidth(`wx')) (bar h x if x>=30, color(ebblue) barwidth(`wx'))
        restore
        In general, even if perhaps tedious, including enough (bar h x if....) statements after twoway should allow a fair amount of flexibility in bar coloring.

        Comment


        • #5
          Code:
          help twoway__histogram_gen
          works, but Stata does mention the command under

          Code:
          help undocumented
          so John Mullahy is right too. It's the non-documented commands that are esoteric.

          That aside, here is another way to do it:

          Code:
          sysuse auto, clear
          histogram mpg if mpg < 30, start(0) width(5) lcolor(orange) fcolor(orange*0.2) freq addplot(histogram mpg if mpg >= 30, start(30) width(5) freq lcolor(blue) fcolor(blue*0.2)) legend(off)

          Comment

          Working...
          X