Announcement

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

  • Bar Graph the my specific data

    Hallo everyone, I need some help for my Bachelor Thesis, I want to plot with bar graph but I have anytime the same problem " class
    member function not found " Before I did the bar graph I have done fisher exact test for one handed hypothesis checking. I need a bar graph which shows the percentage of coming to the walk (Teilnahme) for both groups Persönliche Gruppe(treatment) and Formelle Gruppe(control). The first numeric variable show in which group they are and the second if they come to the walk or not. Someone have an Idea how I can solve this problem ? That could be very helpful for me. Thank you.

    Here is my Data

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte ID str3 Mitarbeiter str20 Treatment byte(Dummy Teilnahme)
     1 "K01" "Persönliche Gruppe " 1 0
     2 "H02" "Persönliche Gruppe " 1 0
     3 "G03" "Persönliche Gruppe " 1 0
     4 "D04" "Persönliche Gruppe " 1 0
     5 "H05" "Persönliche Gruppe " 1 0
     6 "M06" "Persönliche Gruppe " 1 0
     7 "A07" "Persönliche Gruppe " 1 0
     8 "L08" "Persönliche Gruppe " 1 0
     9 "N09" "Persönliche Gruppe " 1 0
    10 "P10" "Persönliche Gruppe " 1 0
    11 "D11" "Persönliche Gruppe " 1 0
    12 "K12" "Persönliche Gruppe " 1 0
    13 "P13" "Persönliche Gruppe " 1 1
    14 "B14" "Persönliche Gruppe " 1 0
    15 "H15" "Persönliche Gruppe " 1 0
    16 "F16" "Persönliche Gruppe " 1 0
    17 "E17" "Persönliche Gruppe " 1 0
    18 "B18" "Persönliche Gruppe " 1 0
    19 "R19" "Persönliche Gruppe " 1 0
    20 "K20" "Persönliche Gruppe " 1 0
    21 "W21" "Persönliche Gruppe " 1 0
    22 "L22" "Persönliche Gruppe " 1 0
    23 "M23" "Persönliche Gruppe " 1 0
    24 "T24" "Persönliche Gruppe " 1 0
    25 "W25" "Formelle Gruppe"      0 0
    26 "W26" "Formelle Gruppe"      0 0
    27 "B27" "Formelle Gruppe"      0 0
    28 "C28" "Formelle Gruppe"      0 0
    29 "M29" "Formelle Gruppe"      0 0
    30 "S30" "Formelle Gruppe"      0 0
    31 "A31" "Formelle Gruppe"      0 0
    32 "D32" "Formelle Gruppe"      0 0
    33 "B33" "Formelle Gruppe"      0 0
    34 "S34" "Formelle Gruppe"      0 0
    35 "O35" "Formelle Gruppe"      0 0
    36 "M36" "Formelle Gruppe"      0 0
    37 "H37" "Formelle Gruppe"      0 0
    38 "G38" "Formelle Gruppe"      0 0
    39 "B39" "Formelle Gruppe"      0 1
    40 "K40" "Formelle Gruppe"      0 0
    41 "S41" "Formelle Gruppe"      0 0
    42 "H42" "Formelle Gruppe"      0 0
    43 "F43" "Formelle Gruppe"      0 0
    44 "W44" "Formelle Gruppe"      0 0
    45 "D45" "Formelle Gruppe"      0 0
    46 "K46" "Formelle Gruppe"      0 0
    47 "M47" "Formelle Gruppe"      0 0
    48 "B48" "Formelle Gruppe"      0 0
    end
    ------------------ copy up to and including the previous line ------------------

    0
    Yes or No
    0%
    0
    .
    0%
    0

  • #2
    There is a problem with the code you are trying. To get specific advice on what you did wrong, please show us that code.

    You seek answers Yes or No, but the only question here is

    Someone have an Idea how I can solve this problem?
    to which my reply is that this code may help.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte ID str3 Mitarbeiter str20 Treatment byte(Dummy Teilnahme)
     1 "K01" "Persönliche Gruppe " 1 0
     2 "H02" "Persönliche Gruppe " 1 0
     3 "G03" "Persönliche Gruppe " 1 0
     4 "D04" "Persönliche Gruppe " 1 0
     5 "H05" "Persönliche Gruppe " 1 0
     6 "M06" "Persönliche Gruppe " 1 0
     7 "A07" "Persönliche Gruppe " 1 0
     8 "L08" "Persönliche Gruppe " 1 0
     9 "N09" "Persönliche Gruppe " 1 0
    10 "P10" "Persönliche Gruppe " 1 0
    11 "D11" "Persönliche Gruppe " 1 0
    12 "K12" "Persönliche Gruppe " 1 0
    13 "P13" "Persönliche Gruppe " 1 1
    14 "B14" "Persönliche Gruppe " 1 0
    15 "H15" "Persönliche Gruppe " 1 0
    16 "F16" "Persönliche Gruppe " 1 0
    17 "E17" "Persönliche Gruppe " 1 0
    18 "B18" "Persönliche Gruppe " 1 0
    19 "R19" "Persönliche Gruppe " 1 0
    20 "K20" "Persönliche Gruppe " 1 0
    21 "W21" "Persönliche Gruppe " 1 0
    22 "L22" "Persönliche Gruppe " 1 0
    23 "M23" "Persönliche Gruppe " 1 0
    24 "T24" "Persönliche Gruppe " 1 0
    25 "W25" "Formelle Gruppe"      0 0
    26 "W26" "Formelle Gruppe"      0 0
    27 "B27" "Formelle Gruppe"      0 0
    28 "C28" "Formelle Gruppe"      0 0
    29 "M29" "Formelle Gruppe"      0 0
    30 "S30" "Formelle Gruppe"      0 0
    31 "A31" "Formelle Gruppe"      0 0
    32 "D32" "Formelle Gruppe"      0 0
    33 "B33" "Formelle Gruppe"      0 0
    34 "S34" "Formelle Gruppe"      0 0
    35 "O35" "Formelle Gruppe"      0 0
    36 "M36" "Formelle Gruppe"      0 0
    37 "H37" "Formelle Gruppe"      0 0
    38 "G38" "Formelle Gruppe"      0 0
    39 "B39" "Formelle Gruppe"      0 1
    40 "K40" "Formelle Gruppe"      0 0
    41 "S41" "Formelle Gruppe"      0 0
    42 "H42" "Formelle Gruppe"      0 0
    43 "F43" "Formelle Gruppe"      0 0
    44 "W44" "Formelle Gruppe"      0 0
    45 "D45" "Formelle Gruppe"      0 0
    46 "K46" "Formelle Gruppe"      0 0
    47 "M47" "Formelle Gruppe"      0 0
    48 "B48" "Formelle Gruppe"      0 0
    end
    
    graph hbar Teilnahme, over(Treatment) over(Dummy) yla(0 0.01 "1" 0.02 "2" 0.03 "3" 0.04 "4") ytitle(Percent) name(G1, replace)
    
    graph hbar Teilnahme, over(Dummy) over(Treatment) yla(0 0.01 "1" 0.02 "2" 0.03 "3" 0.04 "4") ytitle(Percent) name(G2, replace)

    If I understand your question correctly, you have a 2 x 2 classification but only 2 out of 4 possibilities are present in your data example. But then I am not sure that I understand your set-up as the percent coming to a walk will be zero for those not coming to a walk.

    You may prefer graph bar to graph hbar.

    Here is one of those graphs. The other just flips the order of the categories.
    Click image for larger version

Name:	Teilnahme1.png
Views:	1
Size:	33.7 KB
ID:	1771887

    Comment


    • #3
      Thank you Mr. Cox for your help. I think i understand that you mean with a 2x2 classification so I correct my data. Gruppe means 1 Persönliche Gruppe 0 Formelle Gruppe. 1 Teilgenommen and 0 nicht teilgenommen means going to a walk or not.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte ID str3 Mitarbeiter byte(Gruppe Teilnahme)
       1 "K01" 1 0
       2 "H02" 1 0
       3 "G03" 1 0
       4 "D04" 1 0
       5 "H05" 1 0
       6 "M06" 1 0
       7 "A07" 1 0
       8 "L08" 1 0
       9 "N09" 1 0
      10 "P10" 1 0
      11 "D11" 1 0
      12 "K12" 1 0
      13 "P13" 1 1
      14 "B14" 1 0
      15 "H15" 1 0
      16 "F16" 1 0
      17 "E17" 1 0
      18 "B18" 1 0
      19 "R19" 1 0
      20 "K20" 1 0
      21 "W21" 1 0
      22 "L22" 1 0
      23 "M23" 1 0
      25 "W25" 0 0
      26 "W26" 0 0
      27 "B27" 0 0
      28 "C28" 0 0
      29 "M29" 0 0
      30 "S30" 0 0
      31 "A31" 0 0
      32 "D32" 0 0
      33 "B33" 0 0
      34 "S34" 0 0
      35 "O35" 0 0
      36 "M36" 0 0
      37 "H37" 0 0
      38 "G38" 0 0
      39 "B39" 0 1
      40 "K40" 0 0
      41 "S41" 0 0
      42 "H42" 0 0
      43 "F43" 0 0
      44 "W44" 0 0
      45 "D45" 0 0
      46 "K46" 0 0
      47 "M47" 0 0
      48 "B48" 0 0
      end
      I wanted to use this code but Stata said bargraph_g.new ID, over(Teilnahme) over(Gruppe) title("Teilnahme am Spaziergang nach Gruppe") legend(order(1 "Formelle Gruppe" 2 "Persönliche
      Gruppe")): class member function not found and sometimes command legend, title unrecognized

      I used this code for the graph bar
      * 4. Balkendiagramm
      graph bar (count), over(Teilnahme) over(Gruppe) bar(1, color(blue)) bar(2, color(red)) title("Teilnahme am Spaziergang nach Gruppe") legend(label(1 "Formelle Gruppe") label(2 "Persönliche Gruppe"))

      Comment


      • #4
        I get two colours if I add the asyvars option. The command you give is legal and works. The problems you report must arise with code you don't show us, perhaps because you are splitting a command line.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte ID str3 Mitarbeiter byte(Gruppe Teilnahme)
         1 "K01" 1 0
         2 "H02" 1 0
         3 "G03" 1 0
         4 "D04" 1 0
         5 "H05" 1 0
         6 "M06" 1 0
         7 "A07" 1 0
         8 "L08" 1 0
         9 "N09" 1 0
        10 "P10" 1 0
        11 "D11" 1 0
        12 "K12" 1 0
        13 "P13" 1 1
        14 "B14" 1 0
        15 "H15" 1 0
        16 "F16" 1 0
        17 "E17" 1 0
        18 "B18" 1 0
        19 "R19" 1 0
        20 "K20" 1 0
        21 "W21" 1 0
        22 "L22" 1 0
        23 "M23" 1 0
        25 "W25" 0 0
        26 "W26" 0 0
        27 "B27" 0 0
        28 "C28" 0 0
        29 "M29" 0 0
        30 "S30" 0 0
        31 "A31" 0 0
        32 "D32" 0 0
        33 "B33" 0 0
        34 "S34" 0 0
        35 "O35" 0 0
        36 "M36" 0 0
        37 "H37" 0 0
        38 "G38" 0 0
        39 "B39" 0 1
        40 "K40" 0 0
        41 "S41" 0 0
        42 "H42" 0 0
        43 "F43" 0 0
        44 "W44" 0 0
        45 "D45" 0 0
        46 "K46" 0 0
        47 "M47" 0 0
        48 "B48" 0 0
        end
        
        graph bar (count), over(Teilnahme) over(Gruppe) asyvars bar(1, color(blue)) bar(2, color(red)) title("Teilnahme am Spaziergang nach Gruppe") legend(label(1 "Formelle Gruppe") label(2 "Persönliche Gruppe"))
        Click image for larger version

Name:	teilnahme2.png
Views:	1
Size:	35.9 KB
ID:	1771949

        Comment


        • #5
          Hallo and thank you Mr. Cox . Here is my complete Code. I had to do fisher exact Test and nothing else before.

          * 1. Lade die Daten
          import excel "Hierist der OrdnerPfad", sheet("Tabelle1") firstrow clear

          * 2. Überprüfe die geladenen Variablen
          describe

          * 3. Fisher-Exakt-Test durchführen
          tabulate Gruppe_Persoenlich Spaziergang, exact

          * 4. Balkendiagramm
          graph bar (count), over(Teilnahme) over(Gruppe) bar(1, color(blue)) bar(2, color(red)) title("Teilnahme am Spaziergang nach Gruppe") legend(label(1 "Formelle Gruppe") label(2 "Persönliche Gruppe"))

          Comment


          • #6
            I can't run your code as I don't have your spreadsheet. As said, the problems mentioned in

            I wanted to use this code but Stata said bargraph_g.new ID, over(Teilnahme) over(Gruppe) title("Teilnahme am Spaziergang nach Gruppe") legend(order(1 "Formelle Gruppe" 2 "Persönliche
            Gruppe")): class member function not found and sometimes command legend, title unrecognized

            must arise from code you don't show us.

            Comment


            • #7
              Hallo Mr. Cox I fix my Problem. I installed Stata on an other Laptop and it works to plot graphs. Now I have a new Problem with titles . Here my complete Code

              * Berechnung der Gesamtanzahl pro Gruppe
              egen Gesamt = count(Teilnahme), by(Gruppe)

              * Berechnung der Anzahl der Teilnehmer pro Gruppe
              egen Teilnehmer = total(Teilnahme), by(Gruppe)

              * Berechnung des prozentualen Anteils der Teilnehmer
              gen Teilnahme_Prozent = (Teilnehmer / Gesamt) * 100 if Teilnahme == 1

              graph bar Teilnahme_Prozent, over(Gruppe, label(labsize(large))) bar(1, color(blue))
              > blabel(bar, format(%6.1f)) title("Teilnahme am Spaziergang nach Gruppe") ylabel(0(1
              > 0)100, labsize(large)) ytitle("Prozentuale Teilnahme (%)", labsize(large)) xlabel(0
              > "Formelle Gruppe" 1 "Persönliche Gruppe", labsize(large))
              xlabels(0 Formelle not found


              I want to rename my y to "Teilnahme in Prozent" and x for 0 "FormelleGruppe" and 1 "Persönliche Gruppe". Thank you vor helping me

              Comment


              • #8
                Options xlabel(), ..., xmtick() are irrelevant for bar charts.

                Code:
                label define Gruppe 0 "Formelle Gruppe" 1 "Persönliche Gruppe"
                label values Gruppe Gruppe
                graph bar Teilnahme_Prozent, over(Gruppe, label(labsize(large))) bar(1, color(blue)) blabel(bar, format(%6.1f)) title("Teilnahme am Spaziergang nach Gruppe") ylabel(0(10)100, labsize(large)) ytitle("Teilnahme in Prozent(%)")
                Last edited by Chen Samulsion; 18 Feb 2025, 06:34.

                Comment


                • #9
                  The suboption labsize() doesn't apply to title options. graph bar doesn't consider the categorical axis to be an x axis.

                  This may help:

                  Code:
                  clear
                  input byte ID str3 Mitarbeiter byte(Gruppe Teilnahme)
                   1 "K01" 1 0
                   2 "H02" 1 0
                   3 "G03" 1 0
                   4 "D04" 1 0
                   5 "H05" 1 0
                   6 "M06" 1 0
                   7 "A07" 1 0
                   8 "L08" 1 0
                   9 "N09" 1 0
                  10 "P10" 1 0
                  11 "D11" 1 0
                  12 "K12" 1 0
                  13 "P13" 1 1
                  14 "B14" 1 0
                  15 "H15" 1 0
                  16 "F16" 1 0
                  17 "E17" 1 0
                  18 "B18" 1 0
                  19 "R19" 1 0
                  20 "K20" 1 0
                  21 "W21" 1 0
                  22 "L22" 1 0
                  23 "M23" 1 0
                  25 "W25" 0 0
                  26 "W26" 0 0
                  27 "B27" 0 0
                  28 "C28" 0 0
                  29 "M29" 0 0
                  30 "S30" 0 0
                  31 "A31" 0 0
                  32 "D32" 0 0
                  33 "B33" 0 0
                  34 "S34" 0 0
                  35 "O35" 0 0
                  36 "M36" 0 0
                  37 "H37" 0 0
                  38 "G38" 0 0
                  39 "B39" 0 1
                  40 "K40" 0 0
                  41 "S41" 0 0
                  42 "H42" 0 0
                  43 "F43" 0 0
                  44 "W44" 0 0
                  45 "D45" 0 0
                  46 "K46" 0 0
                  47 "M47" 0 0
                  48 "B48" 0 0
                  end
                  
                  egen Gesamt = count(Teilnahme), by(Gruppe)
                  egen Teilnehmer = total(Teilnahme), by(Gruppe)
                  
                  gen Teilnahme_Prozent = (Teilnehmer / Gesamt) * 100 if Teilnahme == 1
                  
                  graph bar Teilnahme_Prozent, over(Gruppe, relabel(1 "Formelle Gruppe" 2 "Persönliche Gruppe")  ///
                  label(labsize(large))) bar(1, color(blue)) ///
                  blabel(bar, format(%6.1f)) title("Teilnahme am Spaziergang nach Gruppe") ///
                  ylabel(0(10)100, labsize(large)) ytitle("Prozentuale Teilnahme (%)", size(large))

                  Comment


                  • #10
                    Chen Samulsion wrote

                    Options xlabel(), ..., xmtick() are irrelevant for bar charts.
                    True for graph bar and graph hbar. Not true for twoway bar etc.

                    Comment


                    • #11
                      Thank you for helping me, Mr. Cox and Mr. Samulsion. Now I have the graph I need.

                      Comment


                      • #12

                        Dear Professor Cox,

                        I hope this message finds you well. I am currently working on generating a bar graph in Stata and have encountered an issue with the axis labels. The figure I have attached illustrates the desired outcome: displaying bars for each year but showing x-axis labels only for specific years (e.g., 2007, 2010, 2013, etc.).

                        In my current approach, I am using the following code:


                        graph bar (asis) share_other share_same if eic==1, ///
                        over(year) stack ///
                        bar(1, color(blue%60)) bar(2, color(green%30)) ///
                        legend(order(1 "Other Cities" 2 "Within Cities") size(small) region(lcolor(white))) ///
                        ytitle("Percentage, %", size(small)) ylabel(0(20)100, labsize(small)) ///
                        graphregion(fcolor(white))

                        However, this results in all years being displayed on the x-axis (i.e., 2007, 2008, 2009, etc.), leading to a cluttered appearance. I was able to achieve the desired labeling manually through the "Add or customize individual ticks and labels (major)" option in the Graph Editor, as shown in the second attached figure.

                        I am seeking a way to replicate this selective labeling programmatically within the code. Could you please advise on how to modify the Stata code to display bars for all years but include x-axis labels only for specific years, such as 2007, 2010, 2013, and so on?

                        Thank you for your time and assistance.

                        Best regards,
                        Meng

                        Click image for larger version

Name:	Graph.png
Views:	1
Size:	54.5 KB
ID:	1777905 Click image for larger version

Name:	image_37274.png
Views:	1
Size:	24.6 KB
ID:	1777904
                        Last edited by Meng Zhang; 24 May 2025, 03:55.

                        Comment


                        • #13
                          See https://journals.sagepub.com/doi/pdf...6867X211000032 for a detailed discussion of your problem.

                          Comment


                          • #14
                            Thanks for your quick reply—it worked perfectly. I used this code:

                            label val year
                            local call
                            forvalues j = 2(2)20 {
                            local call `call' `j' " "
                            }

                            graph bar (asis) share_other share_same if eic==1, ///
                            over(year, relabel(`call')) stack ///
                            bar(1, color(blue%60)) bar(2, color(green%30)) ///
                            legend(order(1 "Other Cities" 2 "Within Cities") size(small) region(lcolor(white))) ///
                            ytitle("Percentage, %", size(small)) ylabel(0(20)100, labsize(small)) ///
                            graphregion(fcolor(white))

                            The result looks much better. However, I noticed a minor issue: the x-axis now labels 2007, 2009, 2011, etc., but I’d like it to show 2007, 2010, 2013, and so on.

                            Comment


                            • #15
                              Your years are 2007 to 2019 in 13 bars and you want to blank out labels for bars (2 3) (5 6) (8 9) (11 12) so that you only get labels for bars 1 4 7 10 13. So
                              the code could be

                              Code:
                              local call
                              
                              forvalues j = 2(3)11 {
                              local call `call' `j' ""
                              local k = `j' + 1
                              local call `call' `k' ""
                              }
                              So first time around the loop you blank out the label for bar 2 -- and also for bar 3. Then 5 and 6, and so on.

                              Comment

                              Working...
                              X