Announcement

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

  • Struggling with Hbar: Conditional Display of Grade Levels by School Type in Bar Chart

    Hi Statalist,

    I am working with a German panel dataset in Stata 18 containing grade levels (1-9) ("grade_self") and school types ("sample_school"), along with a binary treatment variable (1 = treated, 0 = control).

    I would like to display the distribution of treatment and control groups across both grade levels and school types. My current code is:

    Code:
     graph hbar (count), over(treatment, relabel(1 "not treated" 2 "treated")) ///
    over(grade_self, lab(labsize(tiny))) ///
    over(sample_school, relabel(1 "lower secondary track (Hauptschule)" 2 "intermediate secondary track (Realschule)" 3 "upper secondary track (Gymnasium)" 4 "integrated comprehensive school (ICS)" 5 "cooperative comprehensive school (CCS)" 6 "elementary school") ///
    sort(1) reverse lab(labsize(vsmall))) asyvars stack title("") ///
    ytitle(number of observations, size (small)) scheme(s1color)

    The problem: in the bar chart produced with the code above, for each school type, all grade levels (1-9) are displayed. However, elementary/primary schools only have valid observations for grades 1-4. I would like the chart to reflect this, showing only grades 1-4 for elementary schools and grades 5-9 for other school types. I only managed to achieve this using the graph editor (see below). Is there any way to achieve this with code?

    Click image for larger version

Name:	Figure1_mod_v2[1].png
Views:	1
Size:	25.8 KB
ID:	1767030


    Any suggestions would be greatly appreciated! Thanks.

    Best,
    Samuel

  • #2
    Try to add nofill option in your code to omit empty categories:
    Code:
    graph hbar (count), over(treatment, relabel(1 "not treated" 2 "treated")) ///
    over(grade_self, lab(labsize(tiny))) ///
    over(sample_school, relabel(1 "lower secondary track (Hauptschule)" 2 "intermediate secondary track (Realschule)" 3 "upper secondary track (Gymnasium)" 4 "integrated comprehensive school (ICS)" 5 "cooperative comprehensive school (CCS)" 6 "elementary school") ///
    sort(1) reverse lab(labsize(vsmall))) asyvars stack title("") ///
    ytitle(number of observations, size (small)) scheme(s1color) nofill

    Comment


    • #3
      Originally posted by Chen Samulsion View Post
      Try to add nofill option in your code to omit empty categories:
      Code:
      graph hbar (count), over(treatment, relabel(1 "not treated" 2 "treated")) ///
      over(grade_self, lab(labsize(tiny))) ///
      over(sample_school, relabel(1 "lower secondary track (Hauptschule)" 2 "intermediate secondary track (Realschule)" 3 "upper secondary track (Gymnasium)" 4 "integrated comprehensive school (ICS)" 5 "cooperative comprehensive school (CCS)" 6 "elementary school") ///
      sort(1) reverse lab(labsize(vsmall))) asyvars stack title("") ///
      ytitle(number of observations, size (small)) scheme(s1color) nofill
      Hi, thanks for the quick response! That solved my problem. Now it works just as I wanted. Thank you very much!

      Comment

      Working...
      X