Announcement

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

  • mutiple graphs in single graph

    Hi,
    Does anyone knows how to make side by side bar graphs in a single graph? I did the following code, but the results show all in vertical ways (graph_1), but I would like to see all these into horizontal side by side (Wanted_graph.png) in the attachments.


    Code:
    graph hbar group_0 group_1,  over(a14var, sort(1) label(labsize(2)) ) over(type) ///
              ytitle("Percentage")  ///
              ylabel(0(10)100) blabel(bar, format(%2.1f) size(2)) ///
              legend(label(1 "Control") label(2 "Treatment") span pos(6))  ///
              title("Education status", span  pos(14) size(3) placement(top))
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str14 type str42 a14var double(group_1 group_0)
    "Pregnant"       "None/ Monastic or religious/ Grades 1-5"    22.16749  27.8607
    "Pregnant"       "Grade 6-10"                                 51.72414 47.76119
    "Pregnant"       "Grade 11 and above (College, Postgraduate)" 26.10837 24.37811
    "Respondent"     "None/ Monastic or religious/ Grades 1-5"    12.16749  20.8607
    "Respondent"     "Grade 6-10"                                 14.72414       21
    "Respondent"     "Grade 11 and above (College, Postgraduate)" 26.10837       35
    "Household head" "None/ Monastic or religious/ Grades 1-5"          13       20
    "Household head" "Grade 6-10"                                       14       21
    "Household head" "Grade 11 and above (College, Postgraduate)"       15       22
    end


    Attached Files

  • #2
    Hi Anyone, help me with this !!

    Comment


    • #3
      The basic thing to produce side-by-side graphs is to use the -by- option instead of -over-, and within -by-, specify row(1).

      The problem is, this produces category labels on each subgraph, and looks like a mess. Fixing it so that labels are only on the left-most subgraph turns out to be non-trivial. Here is some code that should produce something quite close to what you want. It is inspired by the solution provided on this thread. I also use the graph scheme tufte, which can be installed by entering
      Code:
      ssc install scheme_tufte
      .

      Code:
      clear
      input str14 type str42 a14var double(group_1 group_0)
      "Pregnant"       "None/ Monastic or religious/ Grades 1-5"    22.16749  27.8607
      "Pregnant"       "Grade 6-10"                                 51.72414 47.76119
      "Pregnant"       "Grade 11 and above (College, Postgraduate)" 26.10837 24.37811
      "Respondent"     "None/ Monastic or religious/ Grades 1-5"    12.16749  20.8607
      "Respondent"     "Grade 6-10"                                 14.72414       21
      "Respondent"     "Grade 11 and above (College, Postgraduate)" 26.10837       35
      "Household head" "None/ Monastic or religious/ Grades 1-5"          13       20
      "Household head" "Grade 6-10"                                       14       21
      "Household head" "Grade 11 and above (College, Postgraduate)"       15       22
      end
      
      encode a14var, gen(which)
      replace which = which + 10 if type == "Pregnant"
      replace which = which + 20 if type == "Respondent"
      
      levelsof which if inlist(type, "Pregnant", "Respondent"), local(emptylevels)
      
      foreach lvl of local emptylevels {
          label define which `lvl' "`:di "`=uchar(28)'"'", modify
      }
      
      expand 2, gen(new)
      replace which = 50 if new
      replace group_0 = . if new
      replace group_1 = . if new
      
      #delimit ;
      graph hbar group_1 group_0,
              over(which, label(labsize(vsmall)) reverse)  
              blabel(bar, format(%2.1f))
              bargap(10)
              bar(1, color("31 119 180"*0.6)) bar(2, color("44 160 44"*0.6))
              ylabel(, labsize(small) gstyle(dot))
              ytitle("Percentage", size(small))
              nofill          
              legend(label(1 "Control") label(2 "Treatment") row(1) size(small))
              by(type, iyaxes iytitle row(1) note(""))
              aspectratio(0.75)
              scheme(tufte)        
          ;
      #delimit cr
      You might want to reduce the length of the strings in a14var; this will help produce a nicer graph.

      Click image for larger version

Name:	image_28420.png
Views:	1
Size:	143.6 KB
ID:	1679581

      Comment


      • #4
        #3 did a good job of giving what was asked, but is it really preferable to a one-way display? Here are some other takes on the data.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str14 type str42 a14var double(group_1 group_0)
        "Pregnant"       "None/ Monastic or religious/ Grades 1-5"    22.16749  27.8607
        "Pregnant"       "Grade 6-10"                                 51.72414 47.76119
        "Pregnant"       "Grade 11 and above (College, Postgraduate)" 26.10837 24.37811
        "Respondent"     "None/ Monastic or religious/ Grades 1-5"    12.16749  20.8607
        "Respondent"     "Grade 6-10"                                 14.72414       21
        "Respondent"     "Grade 11 and above (College, Postgraduate)" 26.10837       35
        "Household head" "None/ Monastic or religious/ Grades 1-5"          13       20
        "Household head" "Grade 6-10"                                       14       21
        "Household head" "Grade 11 and above (College, Postgraduate)"       15       22
        end
        
        clonevar a15var = a14var
        replace a15var = " {&le} Grade 5" if word(a14var, 1) == "None/"
        replace a15var = "{&ge} Grade 11" if word(a14var, 2) == "11"
        
        set scheme s1color
        
        graph dot (asis) group_0 group_1, over(type) over(a15var) ///
        legend(order( 2 "Treatment" 1 "Control") pos(12)) marker(1, ms(Oh)) marker(2, ms(+)) linetype(line) lines(lc(gs12) lw(thin)) ytitle(Outcome) ysc(alt) name(G1, replace)
        
        graph dot (asis) group_0 group_1,  over(a15var) over(type) ///
        legend(order( 2 "Treatment" 1 "Control") pos(12)) marker(1, ms(Oh)) marker(2, ms(+)) linetype(line) lines(lc(gs12) lw(thin)) ytitle(Outcome) ysc(alt) name(G2, replace)
        
        graph hbar (asis) group_0 group_1,  over(a15var) over(type) blabel(total, format(%2.0f)) ///
        legend(order( 1 "Control"  2 "Treatment") pos(12)) bar(1, color(orange)) bar(2, color(blue)) ytitle(Outcome) ysc(r(0 54) alt) name(G3, replace)
        Click image for larger version

Name:	zaw1.png
Views:	1
Size:	32.5 KB
ID:	1679610



        Click image for larger version

Name:	zaw2.png
Views:	1
Size:	33.0 KB
ID:	1679611


        Last edited by Nick Cox; 28 Aug 2022, 04:32.

        Comment


        • #5
          Third graph

          Click image for larger version

Name:	zaw3.png
Views:	1
Size:	35.4 KB
ID:	1679615

          Comment


          • #6
            Thein Zaw sorry, one error in my code, please switch the numbers for Treatment and Control, like so:
            Code:
            legend(label(1 "Treatment") label(2 "Control") row(1) size(small))
            Nick Cox / others: I was not able to make the iytitle option within -by- work. Would you have any idea what is going wrong?

            Comment


            • #7
              Some details are tuned by by(, ytitle()) and some by ytitle().

              Comment


              • #8
                I wanted the word "Percentage" to show up on each of the subgraphs, and I thought -iytitle- within -by- would do it, but was just not able to make it work.

                Comment


                • #9
                  Some details are tuned by by(, ytitle()) and some by ytitle().

                  Comment


                  • #10
                    Apologies for the repetition of #9, which was accidental.

                    Hemanshu Kumar

                    Is this what you want?

                    Code:
                    by(type,  iytitle row(1) note(""))
                            aspectratio(0.75) b1title("Percentage", size(small))

                    Comment


                    • #11
                      Nick Cox Oh brilliant, works like a charm!

                      But I would never have guessed this from the Stata help.

                      Indeed, I've never used the b/l/r/t titles till now! And if anything, the Stata documentation seems to minimise their usefulness and dissuades you from using them:

                      {t|b|l|r}{1|2}title() are rarely specified. It is generally better to specify the axis_title_options ytitle() or xtitle(); see [G-3]
                      axis_title_options. The {t|b|l|r}{1|2}title() options are included for backward compatibility with previous versions of Stata.
                      Arguably the documentation should be improved?

                      NB. This extract was from
                      Code:
                      help title_options
                      Last edited by Hemanshu Kumar; 29 Aug 2022, 00:53.

                      Comment


                      • #12
                        Now you know that they exist, it's just a matter of seeing if they fill a gap that seemingly xtitle() or ytitle() can't fill. For example, with graph hbar or graph dot, the y axis is horizontal and you're told that the other axis isn't the x axis. So, how can you put a title on the left? I use l1title().

                        Comment


                        • #13
                          Right, the other axis is categorical and so not an x-axis per se. But with graph hbar, the horizontal axis is a y axis, and as such ytitle() works to put a title on it, with a single graph. But it does not seem to work when you try to specify separate titles for separate subgraphs. Frankly, I would think of this as a bug. This should not have needed the solution you provided, which I think of more as a workaround rather than the way Stata leads you to believe things should work.

                          But in the meantime, I really appreciate learning about these!

                          Comment


                          • #14
                            I am always reluctant to shout "Bug" unless I can be confident that I understand completely what is going on. Here I don't. There are some quirks to graph, certainly, in terms of graph not always understanding what I want. What a surprise!

                            A pet example is that it is seemingly difficult to turn off axis ticks with by() but setting their colour to bg or their length to zero can do what is wanted.

                            Comment

                            Working...
                            X