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

  • stacked bar chart


    I have a dataset of sales that can be attributed to one of three buyer-organizations (1,2,3). The products bought by these organizations can in turn be attributed to underlying concerns. Organization 1 has 2 underlying concerns, x and y, organization 2 also has 2, p and q, organization 3 only has one, z.

    I experience some difficulty in creating a nice descriptive stacked horizontal bar graph.

    I would like to show a stacked horizontal bar graph, with the bars visiually displaying how the percentage of sales per buyer are devided among their underlying concerns.

    Something like 'graph hbar concern, over(buyer) stack' does not seem to do the trick.

    example data:
    * Example generated by -dataex-. To install: ssc install dataex
    input float(price amount buyer) str1 concern
    100 1 1 "x"
    200 1 1 "x"
    101 1 1 "x"
    200 1 1 "x"
    400 1 1 "x"
    600 1 1 "x"
    250 1 1 "y"
    700 1 1 "y"
     87 1 1 "y"
     56 1 2 "p"
     34 1 2 "p"
     78 1 2 "q"
     88 1 2 "q"
     90 1 3 "z"
     92 1 3 "z"
    Help is much appreciated!
    Thanks in advance!

  • #2
    Any variation on

    graph hbar concern
    is illegal, given that concern is a string variable.


    graph hbar , over(concern) over(buyer) stack asyvars


    • #3
      It did the job! Thanks a lot for the information and the code itself!


      • #4

        graph hbar (percent) amount, over(concern) by(buyer, col(1) note("")) asyvars stack subtitle(, fcolor(none) nobox nobexpand pos(9)) legend(row(1))


        • #5
          I do have one other question, with the blabel(bar, format...) option I can display the actual percentage of sales right next to the bars. However, in this instance the bars that are stacked on top of each other logically show the individual percentages per concern. I would like to show just the percentage of sales per buyer organization. How can I do this?


          • #6
            You can still use

            blabel(bar, pos(inside))
            but such a design gets awkward and unclear very quickly. At a minimum you'll need to reach in to insist on pale fill colours, unless your scheme insists on them.

            With your data I also tried tabplot (Stata Journal) with this result:

            tabplot concern buyer [w=amount], percent(buyer) showval bfcolor(eltgreen)

            Click image for larger version

Name:	buyer_concern.png
Views:	1
Size:	13.8 KB
ID:	1501293

            That to me is clearer than anything I could get with graph hbar, but much depends on how many buyers and concerns there are in your real, full example.

            For tabplot see e.g.