Announcement

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

  • Bar chart of dummy variable with one more pooled group bar

    Hello everyone,

    I need to generate the bar graph with dummy variables (for example, the variable "foreign" of the data below). However, I would like to add one more bar with pooled information.
    Take the following figure as an example, how could I add one more bar, which pooled "domestic" and "foreign" for each category 1 to 5? Therefore, there should be three bars for each category 1 to 5 (blue bar: domestic; red bar: foreign; yellow bar: pooled as both)


    Click image for larger version

Name:	Screen Shot 2022-09-07 at 12.22.55 AM.png
Views:	1
Size:	15.9 KB
ID:	1680980


    Here is my old code generated the figure above. Thank you very much in advance.

    Code:
    sysuse auto, clear
    graph bar,  ///
        over(foreign, lab(nolab))   ///
        over(rep78) ///
        asyvars showyvars  ///
        graphregion(color(white))  ///
        bar(1, fcolor(red) lcolor("grey%45")) ymtick(##2) ///
        bar(2, fcolor(blue) lcolor("grey%45")) ymtick(##2) ///
        ytitle("Percent", size(small)) ///
        ylabel(0(10)30, gmin gmax labsize(small)) yscale(range(0 30))  ///
        title(""" ", size(normal)) ///
        legend(size(*.5) region(col(white))) name(g1, replace)
    Last edited by mws macekk; 06 Sep 2022, 16:23.

  • #2
    You may duplicate the data to create a total category, then manipulate the y-axis labels. Why you want the total bars to be colored differently does not make much sense to me, but you may use the graph editor (I do not do that here). Otherwise do everything with twoway which is more flexible.

    Code:
    sysuse auto, clear
    drop if missing(rep78)
    expand 2, g(new)
    replace rep78=99 if new
    lab def rep78 99 "Total"
    lab values rep78 rep78
    graph bar,  ///
        over(foreign, lab(nolab))   ///
        over(rep78, lab) ///
        asyvars showyvars  ///
        graphregion(color(white))  ///
        bar(1, fcolor(red) lcolor("grey%45")) ymtick(##2) ///
        bar(2, fcolor(blue) lcolor("grey%45")) ymtick(##2) ///
        ytitle("Percent", size(small)) ///
        ylabel(0 "0" 5 "10" 10 "20" 15 "30" 20 "40" 25 "50" 30 "60" 35 "70" 40 "80", ///
        gmin gmax labsize(small)) yscale(range(0 30))  ///
        title(""" ", size(normal)) ///
        legend(size(*.5) region(col(white))) name(g1, replace)
    Res.:

    Click image for larger version

Name:	g1.png
Views:	1
Size:	25.1 KB
ID:	1681016

    Last edited by Andrew Musau; 07 Sep 2022, 00:30.

    Comment


    • #3
      Sorry, I misread the question in #2. You want

      Code:
      sysuse auto, clear
      drop if missing(rep78)
      expand 2, g(new)
      replace foreign= 3 if new 
      graph bar,  ///
          over(foreign, lab(nolab))   ///
          over(rep78) ///
          asyvars showyvars  ///
          graphregion(color(white))  ///
          bar(1, fcolor(red) lcolor("grey%45")) ymtick(##2) ///
          bar(2, fcolor(blue) lcolor("grey%45")) ymtick(##2) ///
          bar(3, fcolor(yellow) lcolor("grey%45")) ymtick(##2) ///
          ytitle("Percent", size(small)) ///
          ylabel(0 "0" 5 "10" 10 "20" 15 "30" 20 "40" 25 "50" , ///
          gmin gmax labsize(small)) yscale(range(0 30))  ///
          title(""" ", size(normal)) ///
          legend(order(1 2 3 "Total")size(*.5) row(1) region(col(white))) name(g1, replace)
      Res.:
      Click image for larger version

Name:	g1.png
Views:	1
Size:	24.3 KB
ID:	1681021

      Comment


      • #4
        For related discussion see also https://www.stata-journal.com/articl...article=gr0058

        Comment


        • #5
          Originally posted by Andrew Musau View Post
          Sorry, I misread the question in #2. You want

          Code:
          sysuse auto, clear
          drop if missing(rep78)
          expand 2, g(new)
          replace foreign= 3 if new 
          graph bar, ///
          over(foreign, lab(nolab)) ///
          over(rep78) ///
          asyvars showyvars ///
          graphregion(color(white)) ///
          bar(1, fcolor(red) lcolor("grey%45")) ymtick(##2) ///
          bar(2, fcolor(blue) lcolor("grey%45")) ymtick(##2) ///
          bar(3, fcolor(yellow) lcolor("grey%45")) ymtick(##2) ///
          ytitle("Percent", size(small)) ///
          ylabel(0 "0" 5 "10" 10 "20" 15 "30" 20 "40" 25 "50" , ///
          gmin gmax labsize(small)) yscale(range(0 30)) ///
          title(""" ", size(normal)) ///
          legend(order(1 2 3 "Total")size(*.5) row(1) region(col(white))) name(g1, replace)
          Res.:
          [ATTACH=CONFIG]n1681021[/ATTACH]
          Exactly. Thanks a lot.

          Comment

          Working...
          X