Announcement

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

  • Combining multiple histograms

    Hi Nick
    Hopefully simple question: I want to show age groups for three different categories but cannot space out the categories on the histogram, also, the width of the bars seem to differ depending on the number of observations ?
    I have Stata 12.1 and thus cannot add transparency.

    commands used for the graph
    twoway histogram agegroup if var1==1, color(blue) percent|| histogram agegroup if var1==2, percent color(green) || histogram agegroup, color(black) percent


    Click image for larger version

Name:	age group.png
Views:	1
Size:	16.6 KB
ID:	1640311


    thank you so much !

  • #2
    I don't know why you are addressing someone called Nick in a post to a community with several thousand readers....

    More importantly, there is no data example here and I am at a loss to know how your syntax produces bars of very different widths.

    I see different age groups and a desire to see the distributions separately for categories of a binary variable and also in total.

    Here is an analogue of your problem and two designs I find clearer than yours. There should be many other ways to do this; I don't rule out a histogram solution. but I doubt one is possible without some extra calculations first.

    The trick to expand the data to get a total is discussed at https://www.stata-journal.com/articl...article=gr0058

    tabplot is from the Stata Journal


    Code:
    sysuse auto, clear 
    
    drop if missing(rep78)
    
    local Np1 = _N + 1 
    expand 2 
    replace foreign = 2 in `Np1'/L 
    label def origin 2 "Total", add 
    
    
    set scheme s1color 
    
    tabplot rep78 foreign, percent(foreign) yreverse separate(foreign) showval name(G1, replace)
    
    contract foreign rep78 
    
    egen pc = pc(_freq), by(foreign)
    
    list , sepby(foreign)
    
    graph bar (asis) pc, over(rep78) over(foreign) blabel(bar, format("%2.1f")) name(G2, replace)

    Click image for larger version

Name:	expand_G1.png
Views:	1
Size:	26.1 KB
ID:	1640341
    Click image for larger version

Name:	expand_G2.png
Views:	1
Size:	25.1 KB
ID:	1640342


    Comment

    Working...
    X