Announcement

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

  • reordering bars in bar graph

    Hi,

    I've written code to produce a bar graph that shows the mean of a score by education level. I'd like to make sure the y-axis labels are from the lowest level of education to the highest, i.e. from less than high school, high school, some college, college, and more than 4 years of college. Is there any way I could do this regardless of the mean value of the score?

    Kindly find my code below:

    gen education_level="Less than high school" if yearsofschooling<9
    replace education_level="High school" if yearsofschooling==10
    replace education_level="Some college" if yearsofschooling>10 & yearsofschooling<14
    replace education_level="College" if yearsofschooling==14
    replace education_level="More than 4 years of college" if yearsofschooling>14

    graph bar (mean) score [weight=employment], over(education_level)

    Thanks in advance!

  • #2
    I would suggest creating a numerical variable with value labels, instead of a string variable. Something like this (ignore the part before #delimit ; for your own code):

    Code:
    sysuse nlsw88, clear
    
    rename grade yearsofschooling
    rename wage score
    rename hours employment
    
    #delimit ;
    gen byte education_level  =    
           cond(yearsofschooling <=  9, 1,
           cond(yearsofschooling == 10, 2,
           cond(inrange(yearsofschooling,11,13) , 3 ,
           cond(yearsofschooling == 14, 4,
           cond(yearsofschooling > 14 & !missing(yearsofschooling) , 5, .)))))
           ;
    
    label define ed_level
            1     "Less than high school"
            2    "High school"
            3    "Some college"
            4    "College"
            5    "More than 4 years of college"
            ;
    #delimit cr
    
    label values education_level ed_level
    
    graph bar (mean) score [weight=employment], over(education_level, label(labsize(small))) ytitle(Score) scheme(s1color)
    which produces:
    Click image for larger version

Name:	Screenshot 2022-10-02 at 11.55.45 PM.png
Views:	1
Size:	114.6 KB
ID:	1684021

    Last edited by Hemanshu Kumar; 02 Oct 2022, 12:29.

    Comment

    Working...
    X