Announcement

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

  • mybar with "over" option

    Since Stata does not provide an nice solution for bar charts including percent and count in one graph, there is an existing user written programm to do so (see below). I would like to know if it is possible to integrate an "over" option, if I would like to graph not only a categorical variable but also these categorical variable over another antoher one. That would be great.

    Thanks
    -Nick

    ---

    program mybar
    version 9
    syntax varname [if] [in] [, baropts(str asis) sc1opts(str asis) *]

    quietly {
    marksample touse, strok
    count if `touse'
    if r(N) == 0 error 2000

    preserve
    tempvar fr pc text y y1 y2

    contract `varlist' if `touse', freq(`fr') percent(`pc')
    egen `y' = group(`varlist'), label
    su `fr', meanonly
    local xmax = 1.15 * r(max)

    if _N > 12 {
    gen `text' = string(`fr') + " (" ///
    + string(`pc', "%2.1f") + "%)"
    local textcall ///
    scatter `y' `fr', ms(none) mla(`text') ///
    mlabc(green) yti("")
    }
    else {
    local nudge = 0.02 * _N
    gen `y1' = `y' - `nudge'
    gen `y2' = `y' + `nudge'
    gen `text' = "(" + string(`pc', "%2.1f") + "%)"
    local textcall ///
    scatter `y1' `fr', ms(none) mla(`fr') mlabc(green) `sc1opts' ///
    || scatter `y2' `fr', ms(none) mla(`text') mlabc(green)
    }

    levelsof `y', local(Y)
    local header `"`: var label `varlist''"'
    if `"`header'"' == "" local header "`varlist'"
    }

    twoway bar `fr' `y', horizontal barw(0.6) base(0) `baropts' ///
    || `textcall' ///
    yla(`Y', valuelabels noticks ang(h)) ysc(reverse) ///
    xti("") xla(none) xsc(r(0 `xmax')) ///
    legend(off) subtitle(`"`header'"') `options'
    end


  • #2
    I don't know anything about mybar: please explain where it comes from if it isn't yours.

    I would try working with existing programs already published. Here for example is an application of
    tabplot (SSC). For a customised bar label with counts and percents, we just build up a string variable one step at a time.

    Code:
    set scheme s1color 
    sysuse auto, clear 
    drop if missing(foreign, rep78)
    bysort foreign rep78 : gen show = string(_N)
    bysort foreign rep78 : gen pc = _N
    bysort foreign : replace pc = 100 * pc/_N
    replace show = show + "  " + string(pc, "%2.1f") + "%" 
    tabdisp foreign rep78, c(show)
    tabplot foreign rep78, showval(show, mlabcolor(black)) subtitle(# and row %)
    Click image for larger version

Name:	bornschein.png
Views:	1
Size:	11.9 KB
ID:	1307867


    tabplot has been accreting features since 1999, but there is no up-to-date write-up beyond its help, which is quite detailed.

    Comment


    • #3
      Thank you Nick. tabplot worked perfect.

      Comment

      Working...
      X