Announcement

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

  • By group bar charts only displaying results for one type of observations within each group

    Hi all,

    I use Stata 16.1 and I'm trying to make bar charts for two different groups.

    The first group is a binary variable that accounts for language (0 for German and 1 for French) called lang.

    The second group is a binary variable as well that accounts for the conjuction of language and whether the individual works in the public sector (0 for German speaker AND public worker; 1 for French speaker AND public worker). This variable is called pub_lang; missing values are for people that work in the private sector - which I'm not interested in.

    To create this variable, I used this code
    Code:
    g pub_lang=.
    replace pub_lang=1 if public==1 & lang==1
    replace pub_lang=0 if public==1 & lang==0
    Basically, I'm just trying to see the difference in mean for a variable that depicts general trust in people - called gtrust - between German and French speaker in general, and between German and French speaker among public workers (I'm studing a bilingual region).

    Here is a subset of my database:

    input byte gtrust float(lang pub_lang)
    5 1 .
    3 1 .
    3 1 .
    6 1 .
    0 1 .
    5 1 .
    . 1 .
    . 1 .
    6 1 1
    . 1 .
    8 1 1
    6 1 1
    6 1 1
    4 1 1
    . 1 .
    . 1 .
    . 1 .
    6 1 .
    . 1 .
    4 1 .
    6 1 1
    4 1 .
    7 1 .
    5 1 .
    5 1 .
    7 1 .
    . 1 .
    . 1 .
    6 1 .
    7 1 .
    3 1 .
    5 1 .
    4 1 .
    . 1 .
    4 1 .
    5 1 1
    5 1 1
    5 1 1
    5 1 1
    . 0 .
    7 0 0
    . 0 0
    8 0 0
    . 0 .
    8 1 .
    8 1 .
    8 1 .
    8 1 .

    I use the following code:

    Code:
    graph bar (mean) gtrust, over(lang) over(pub_lang)
    But for some reason, Stata only shows me the mean for the observations worthing 0 in one group and the observations worthing 1 in the other group:
    Click image for larger version

Name:	bar.png
Views:	1
Size:	28.5 KB
ID:	1639639




    Does anyone know what's going on? I suspect that it's because of pub_lang being a compound variable, but not sure.

    Thank you.
    Last edited by Zsolt Marai; 06 Dec 2021, 04:06.

  • #2
    The reason is 0×1 and 1×0 have no observation:
    Code:
    tabulate lang pub_lang

    Comment


    • #3
      Thank you for your reply. You made me realize this issue.

      The problem I have then is that the bar charts I want to make for the two different groups refer to two different sample.

      The first group takes the whole sample but the second wants only to compare public workers according their language.

      I could do it separately, but I'd like to present the two groups on the same graph.

      Do you have an idea about how to overcome this?

      Thank you.

      Comment


      • #4
        See https://www.stata-journal.com/articl...article=gr0058 on the problem posed in #3.

        Comment


        • #5
          Great solution Nick Cox

          Comment


          • #6
            Nick Cox, thank you for your suggestion. I'll soon examine your article.

            Regarding my issue, I actually decided to change my settings by comparing public vs private workers instead of public vs total population. It makes more sense this way.

            If I may however kindly ask another question, I'd like to know how can I change the name of my groups directly on the graph.

            With this code:

            Code:
            graph bar (mean) gtrust, over(lang) over(public) bar(1, fcolor(gold%85)) blabel(bar) ytitle(General trust in people (mean)) ylabel(0(1)10, labsize(medsmall)) title(Mean-comparison between German and French speakers among private and public workers, size(small))
            I got this chart:
            Click image for larger version

Name:	aa.png
Views:	1
Size:	62.9 KB
ID:	1639677



            What I would like is to have respectively "DE" and "FR" for both 0 and 1 ; and "Private" and "Public" for the 0 and 1 below.

            I'm new on Stata, so I apologize if I ask trivial questions. Morevoer, I was not able to find an answer this issue on the forum.

            Thanks anyway for your help.

            Comment


            • #7
              Define and attach value labels for your variables before you call up the graph.

              Code:
              help label

              Comment


              • #8
                Define values labels and assign them to variables respectively:
                Code:
                label define lang 0 DE 1 FR
                label values lang lang
                label define public 0 Private 1 Public
                label values public public
                
                graph bar (mean) gtrust, over(lang) over(public) bar(1, fcolor(gold%85)) blabel(bar) ytitle(General trust in people (mean)) ylabel(0(1)10, labsize(medsmall)) title(Mean-comparison between German and French speakers among private and public workers, size(small))
                If you still want to get graph in #3, codes will be:
                Code:
                label define lang 0 DE 1 FR
                label values lang lang
                label define pub_lang 0 Private 1 Public 2 All
                label values pub_lang pub_lang
                
                preserve
                expand 2
                replace pub_lang=2 if _n>_N/2
                graph bar (mean) gtrust, over(pub_lang) over(lang) blabel(bar, format(%9.2f)) nofill
                restore

                Comment


                • #9
                  Thank you all, you were really helpful to me.

                  Comment

                  Working...
                  X