Announcement

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

  • Hisgram / Bar Graph Color code based on each value and show in legend

    Hello, I am very new to Stata and I cannot figure out how I can color-code each value of this variable and make it appear in the legend section at the bottom so it's visually appealing.
    Click image for larger version

Name:	Screen Shot 2022-01-02 at 3.27.34 AM.png
Views:	4
Size:	282.6 KB
ID:	1643309

    Code used: histogram babo2, discrete percent fcolor(maroon) lcolor(white) addlabel addlabopts(mlabsize(small)) ytitle(Percent) xtitle(Years of Education Completed) xmtick(, labels labcolor(black) format(%9.0g) glcolor(pink)) xlabel(0"None" 1"Incomplete Primary" 2 "Complete Primary" 3 "Incomplete Secondary" 4 "Complete Secondary") title(Education Completed) legend(on)

    Click image for larger version

Name:	Screen Shot 2022-01-02 at 3.29.20 AM.png
Views:	1
Size:	17.1 KB
ID:	1643310

    If there's any instruction through the do-file or the graphic editor that I can do this from, that would be so helpful.

    I am doing this for a class that requires very basic Stata knowledge so I believe the solution is not too complicated.
    Thank you so much.
    Attached Files
    Dear statalist, I have a problem of plotting a bar chart in STATA. I have 3 categorical variables, and I'd like to count var1 over var2 by/over var3 (it's

  • #2
    To get different colours use graph bar -- or even better graph hbar to avoid the horrible ang(45) --- rather than histogram.

    I don't think different colours help much.

    Even less is a legend needed or helpful compared with direct labelling. A legend implies mental back and forth (what does this colour mean? What is primary on this graph?) whereas direct labelling gives you information, hmm, directly.

    Detail: axis ticks aren't needed or helpful either.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str20 var1 float(var2 var3)
    "None"                 38.37 1
    "Incomplete Primary"    38.9 2
    "Complete Primary"     9.233 3
    "Incomplete Secondary"  6.88 4
    "Complete Secondary"   6.619 5
    end
    
    label values var3 var3
    label def var3 1 "None", modify
    label def var3 2 "Incomplete Primary", modify
    label def var3 3 "Complete Primary", modify
    label def var3 4 "Incomplete Secondary", modify
    label def var3 5 "Complete Secondary", modify
    
    set scheme s1color
    graph hbar (asis) var2, over(var3) blabel(total, format(%3.2f)) ysc(r(0 42) off) bar(1, lcolor(blue) fcolor(ltblue)) subtitle(Education level (%)) name(hanvit, replace)
    Click image for larger version

Name:	hanvil.png
Views:	1
Size:	18.5 KB
ID:	1643318



    EDIT

    Different colours, avoiding legend:


    Code:
    separate var2, by(var3)
    
    graph hbar (asis) var2?, nofill over(var3) blabel(total, format(%3.2f)) ysc(r(0 42) off) subtitle(Education level (%)) name(hanvit2, replace) legend(off)
    Last edited by Nick Cox; 02 Jan 2022, 06:31.

    Comment

    Working...
    X