Announcement

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

  • Stacked bar chart, for multiple stacked bars within different categories

    Well, I want to create a diagram, that is a mixture of these two:
    http://www.stata.com/support/faqs/gr...ther-variable/ and http://www.stata.com/support/faqs/gr...art/index.html

    I basically want a bar chart, that is stacked on one variable but has multiple columns in each "row". I attach a random picture from excel.





    Assume using the auto dataset.

    Code:
    sysuse auto, clear
     Graph hbar (sum) price, over(rep78)  over(gear_ratio)  by(foreign) asyvars   stack
    and now you have 2 graphs, one for foreign, one for abroad. And as mentioned above, I would wish to have both locations as 2 bars in one diagram (in the language of the picture above, i want one stacked bar domestic (as act.) and one stacked bar (as Bdgt.)).

    Is there any way to do that in Stata?

    Best and many thanks.

  • #2
    You introduce 2 graphs by using the -by()- option. What you want is to use the -over()- option once again and then eliminate any spaces between bars. For illustration purposes, I recreate something similar to your original diagram.



    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(Amount Product Detail Type)
    100 1 1 1
     50 1 1 2
    200 1 1 3
     75 1 1 4
    125 1 1 5
     50 1 1 6
     60 1 2 1
    140 1 2 2
     40 1 2 3
    100 1 2 4
     60 1 2 5
    200 1 2 6
     50 2 1 1
    100 2 1 2
    100 2 1 3
    150 2 1 4
     75 2 1 5
    125 2 1 6
    120 2 2 1
     70 2 2 2
     80 2 2 3
    200 2 2 4
     30 2 2 5
    100 2 2 6
    end
    label values Product prod
    label def prod 1 "Product 1", modify
    label def prod 2 "Product 2", modify
    label values Detail det
    label def det 1 "Act.", modify
    label def det 2 "Bdgt.", modify
    label values Type typ
    label def typ 1 "Radio", modify
    label def typ 2 "Print", modify
    label def typ 3 "TV", modify
    label def typ 4 "Internet", modify
    label def typ 5 "Billboards", modify
    label def typ 6 "Other", modify


    So specifying -gap(*#)- tells Stata what gap to leave between bars. In your case, you want no gap i.e. -gap(*0)-

    Code:
    graph hbar (sum) Amount, over(Type)  over(Detail, gap(*0))  over(Product) asyvars stack
    Click image for larger version

Name:	hbar.png
Views:	1
Size:	13.1 KB
ID:	1356257














    Comment

    Working...
    X