Announcement

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

  • Help twoway

    Dear All,
    I am trying to create a bar chart (see attached), but am having difficulty adding a little gap between the bars for each year. I.e., I want a little space added between the bar for Whites in a given year and the bar for All of the subsequent year.
    Below are the dataex and code that created the attached graph.

    Would be grateful for any help you may be able to offer.
    Sumedha

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year double(pctMOUDcondOUDA pctMOUDcondOUDB pctMOUDcondOUDH pctMOUDcondOUDW) float pctMOUDcondOUDwoU str3 payer float(type1 type2 type3 type4 type0 type5 type6 type7 type8 type9)
    2016 3.39 4.62 2.61 4.33 4.1372952 "MCR" 2015.6 2015.8 2016.2 2016.4 2016.6 2015.47 2015.68 2015.88 2016.08 2016.28
    2017 2.89 4.09 2.64  4.5 4.1770897 "MCR" 2016.6 2016.8 2017.2 2017.4 2017.6 2016.47 2016.68 2016.88 2017.08 2017.28
    2018 2.75 4.49 2.57 4.82 4.4405007 "MCR" 2017.6 2017.8 2018.2 2018.4 2018.6 2017.47 2017.68 2017.88 2018.08 2018.28
    2019  2.9  5.2 2.88 5.57  5.120433 "MCR" 2018.6 2018.8 2019.2 2019.4 2019.6 2018.47 2018.68 2018.88 2019.08 2019.28
    2020 3.86 6.92  3.2 6.64  6.177121 "MCR" 2019.6 2019.8 2020.2 2020.4 2020.6 2019.47 2019.68 2019.88 2020.08 2020.28
    2021 4.95 7.55 3.85 7.32  6.910174 "MCR" 2020.6 2020.8 2021.2 2021.4 2021.6 2020.47 2020.68 2020.88 2021.08 2021.28
    end
    Code:
    twoway  (bar pctMOUDcondOUDwoU type1 if payer=="MCR", barwidth(.2) bcol(black)) || ///
            (bar pctMOUDcondOUDA type2 if payer=="MCR", barwidth(.2) bcol(khaki)) || ///
            (bar pctMOUDcondOUDB type if payer=="MCR", barwidth(.2) bcol(gs7*.35)) || ///
            (bar pctMOUDcondOUDH type3 if payer=="MCR", barwidth(.2) bcol(cranberry*.55)) /*||*/ ///
            (bar pctMOUDcondOUDW type4 if payer=="MCR", barwidth(.2) bcol(navy)) || ///
            (scatter pctMOUDcondOUDwoU type5 if payer=="MCR", mlabp(1) msymbol(none) mlabel(pctMOUDcondOUDwoU) mcol(black) mlabsize(2)) || ///
            (scatter pctMOUDcondOUDA type6 if payer=="MCR", mlabp(1) msymbol(none) mlabel(pctMOUDcondOUDA) mcol(black) mlabsize(2)) || ///
            (scatter pctMOUDcondOUDB type7 if payer=="MCR", mlabp(1) msymbol(none) mlabel(pctMOUDcondOUDB) mcol(black) mlabsize(2)) || ///
            (scatter pctMOUDcondOUDH type8 if payer=="MCR", mlabp(1) msymbol(none) mlabel(pctMOUDcondOUDH) mcol(black) mlabsize(2)) || ///
            (scatter pctMOUDcondOUDW type9 if payer=="MCR", mlabp(1) msymbol(none) mlabel(pctMOUDcondOUDW) mcol(black) mlabsize(2)), ///
      title("(b) Medicare Advantage", pos(11)) ///
      legend(order(1 " All" 2 "Asian" 3 "Black" 4 "Hispanic" 5 "White") pos(6) cols(5)) ///
      xlabel( 2016 "2016" 2017 "2017"  2018 "2018" 2019 "2019" 2020 "2020" 2021 "2021") ///
      ytitle("% with OUD Dx receiving MOUD", size(2.5)) xtitle("")  name(b, replace)

    Click image for larger version

Name:	MOUD.png
Views:	2
Size:	37.7 KB
ID:	1779477
    Attached Files

  • #2
    Do the type0-type9 original in your data, or just generated by yourself to make a twoway graph? I think you seek far and neglect what lies close at hand. -graph bar- is more simpler than -twoway bar- for your task here.
    Code:
    graph bar pctMOUDcondOUDA pctMOUDcondOUDB pctMOUDcondOUDH pctMOUDcondOUDW pctMOUDcondOUDwoU, over(year, gap(*2)) ///
    blabel(bar, format(%9.1f) size(vsmall)) bar(1, color(black)) bar(2, color(khaki)) bar(3, color(gs7*.35)) bar(4, color(cranberry*.55)) bar(5, color(navy)) bargap(10) ///
    title("(b) Medicare Advantage", pos(11)) legend(order(1 " All" 2 "Asian" 3 "Black" 4 "Hispanic" 5 "White") pos(6) cols(5)) ytitle("% with OUD Dx receiving MOUD", size(2.5)) ///
    name(b, replace)
    Click image for larger version

Name:	b.png
Views:	1
Size:	186.7 KB
ID:	1779484

    Comment


    • #3
      I didn't notice your order of variables, the code below will reproduce exactly what you posted in #1:
      Code:
      graph bar pctMOUDcondOUDwoU pctMOUDcondOUDA pctMOUDcondOUDB pctMOUDcondOUDH pctMOUDcondOUDW , over(year, gap(*2)) ///
      blabel(bar, format(%9.2f) size(vsmall)) bar(1, color(black)) bar(2, color(khaki)) bar(3, color(gs7*.35)) bar(4, color(cranberry*.55)) bar(5, color(navy)) bargap(10) ///
      title("(b) Medicare Advantage", pos(11)) legend(order(1 " All" 2 "Asian" 3 "Black" 4 "Hispanic" 5 "White") pos(6) cols(5)) ytitle("% with OUD Dx receiving MOUD", size(2.5)) ///
      name(b, replace)
      Click image for larger version

Name:	b.png
Views:	1
Size:	198.8 KB
ID:	1779486

      Comment


      • #4
        Chen Samulsion has answered your question. For these data, I would consider tabplot from the Stata Journal.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input int year double(pctMOUDcondOUDA pctMOUDcondOUDB pctMOUDcondOUDH pctMOUDcondOUDW) float pctMOUDcondOUDwoU str3 payer float(type1 type2 type3 type4 type0 type5 type6 type7 type8 type9)
        2016 3.39 4.62 2.61 4.33 4.1372952 "MCR" 2015.6 2015.8 2016.2 2016.4 2016.6 2015.47 2015.68 2015.88 2016.08 2016.28
        2017 2.89 4.09 2.64  4.5 4.1770897 "MCR" 2016.6 2016.8 2017.2 2017.4 2017.6 2016.47 2016.68 2016.88 2017.08 2017.28
        2018 2.75 4.49 2.57 4.82 4.4405007 "MCR" 2017.6 2017.8 2018.2 2018.4 2018.6 2017.47 2017.68 2017.88 2018.08 2018.28
        2019  2.9  5.2 2.88 5.57  5.120433 "MCR" 2018.6 2018.8 2019.2 2019.4 2019.6 2018.47 2018.68 2018.88 2019.08 2019.28
        2020 3.86 6.92  3.2 6.64  6.177121 "MCR" 2019.6 2019.8 2020.2 2020.4 2020.6 2019.47 2019.68 2019.88 2020.08 2020.28
        2021 4.95 7.55 3.85 7.32  6.910174 "MCR" 2020.6 2020.8 2021.2 2021.4 2021.6 2020.47 2020.68 2020.88 2021.08 2021.28
        end
        
        rename pctMOUDcondOUD* Percent*
        reshape long Percent, i(year) j(which) string
        tabplot which year  [iw=Percent],  title("(b) Medicare Advantage") sep(which) ///
        showval(format(%3.2f)) ytitle("% with OUD Dx receiving MOUD") xtitle("")
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	46.8 KB
ID:	1779503

        Comment


        • #5
          Q. Have you considered using multiple overlaid line graphs here?
          --
          Bruce Weaver
          Email: [email protected]
          Version: Stata/MP 19.5 (Windows)

          Comment

          Working...
          X