Announcement

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

  • Colors in Histogram



    Hi.
    I want to give both the Yes bars a different color from the two No bars. How can this be done.

    Here's the code that I used for the graph at the end. Thanks

    histogram Difficult_Readings_edit, discrete percent by(Outstation, title("Difficulty in Accessing Readings" ) noixtick ///
    legend(off)) graphregion(fcolor(white)) lcolor(white) gap(8) ///
    addlabel addlabopts(mlabsize(small) mlabposition(6) mlabgap(2)) xtitle("") by(, note("")) xla(0 1, valuelabel ) xscale(noline)
    Click image for larger version

Name:	Screenshot 2021-11-23 at 1.49.16 PM.png
Views:	1
Size:	806.0 KB
ID:	1637719


  • #2
    Create bar graphs instead.

    Code:
    sysuse auto, clear
    set seed 11232021
    lab def yes 0 "no" 1 "yes"
    gen yes=runiformint(0,1)
    lab values yes yes
    
    *YOUR GRAPH
    histogram yes, discrete percent by(foreign, title("Whatever" ) noixtick ///
    legend(off)) graphregion(fcolor(white)) lcolor(white) gap(8) ///
    addlabel addlabopts(mlabsize(small) mlabposition(6) mlabgap(2)) ///
    xtitle("") by(, note("")) xla(0 1, valuelabel ) xscale(noline) saving(gr1, replace)
    
    *GRAPH BAR
    gen no=!yes
    gr bar no yes, by(foreign, note("")leg(off) title("Whatever") ) bargap(20) ///
    asyvars showyvars percent leg(off) bar(1, color(blue)) bar(2, color(red)) ///
    blabel(total, pos(inside) format("%2.0f"))  yvaropt(relabel(1 "no" 2 "yes")) ///
    leg(off) ytitle("Percent") saving(gr2, replace)
    
    gr combine gr1.gph gr2.gph, row(2)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	48.4 KB
ID:	1637730

    Comment


    • #3
      Thanks a lot.

      Comment


      • #4
        If you don't mind changing the original layout a little bit, below is another solution (Thanks to Andrew's code).

        Code:
        sysuse auto, clear
        set seed 11232021
        gen Yes = runiformint(0,1)*100
        gen No = 100 - Yes
        
        graph bar No Yes, over(foreign) bar(1, col(navy)) bar(2, col(maroon)) leg(off) ///
              showyvars nolab blab(bar, format(%4.1f)) ytitle("Percent") title("Whatever")
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	242.4 KB
ID:	1637762

        Comment

        Working...
        X