Announcement

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

  • Bar graph, stacking categories

    Hi all,
    I'm producing a bar graph with one categorical variable containing 4 categories. In my graph I would like 3 of the categories to be stacked and one aside.

    This is my code so far:
    Code:
    label define catvar 1 "cat1" 2 "cat2" 3 "cat3" 4 "cat4"
    label values catvar catvar
    
    graph bar, over(catvar) asyvars bar(1, fcolor(emerald)) bar(2, fcolor(gold)) bar(3, fcolor(cranberry)) bar(4, fcolor(ebblue))
    and this is the output

    Click image for larger version

Name:	Screenshot 2023-05-25 150307.png
Views:	1
Size:	18.4 KB
ID:	1714912



    Any way I could only stack categories 1 2 and 3?

    Thank in advance

  • #2
    You'll need some more informative labels. Why be cryptic?

    https://www.brainyquote.com/quotes/howard_aiken_101540



    Code:
    clear 
    input catvar 
    1
    2
    2
    3
    3
    3
    4
    4
    4
    4
    end 
    
    label define catvar 1 "cat1" 2 "cat2" 3 "cat3" 4 "cat4"
    label values catvar catvar
    
    gen is4 = catvar == 4 
    label def is4 0 "this lot is stacked" 1 "this one is not"
    label val is4 is4 
    
    graph bar, over(catvar) over(is4)  asyvars stack bar(1, fcolor(emerald)) bar(2, fcolor(gold)) bar(3, fcolor(cranberry)) bar(4, fcolor(ebblue))
    Click image for larger version

Name:	stackedornot.png
Views:	1
Size:	22.5 KB
ID:	1714922

    Comment

    Working...
    X