Another way to do it is (in total)
. catplot bar rep78 for , percent(for) asyvars stack
blabel(bar, pos(center) format(%3.2f))
where I've added a format control. Here -catplot- can be
installed from SSC.
Incidentally, I assume your original
. xi: graph bar i.rep78, stack percent over(foreign) blabel(bar,
pos(center))
was just to get a graph up for discussion. The downside of using
-xi- here is that one dummy variable will be omitted from the graph.
In general, I'd assert that -xi- is an ingenious but also inappropriate
way of getting a graph.
Nick
[email protected]
Friedrich Huebler
>
> Thank you for your smart suggestion. I modified your commands by
> adding -levels- and two -foreach- loops so that no previous knowledge
> of the observed values is required. The first set of commands,
> presented below, only works if rep78 takes on consecutive integers
> starting at 1. This can be demonstrated by adding -recode rep78 1=11-
> after the -sysuse- line.
>
> ========================================
> # delimit;
>
> sysuse auto, clear;
>
> tab rep78, gen(rep78_);
> levels rep78, local(levels1);
> levels foreign, local(levels2);
>
> foreach l1 of local levels1 {;
> foreach l2 of local levels2 {;
> count if rep78==`l1' & foreign==`l2';
> replace rep78_`l1'=. if r(N)==0 & foreign==`l2';
> };
> };
>
> graph bar rep78_* , stack percent over(foreign)
> blabel(bar,pos(center));
> ========================================
>
> The commands below work with any numeric values in rep78, but not
> with strings. They generate a copy of rep78 that holds only
> consecutive integers starting at 1. If someone can suggest
> improvements, for example to the way the copy of rep78 is created,
> please let me know. I would also like to know how these commands can
> be modified so that they work with strings and numeric values in
> rep78. When double quotes are added to the -foreach- loops the
> commands no longer work with numeric values because of a "type
> mismatch."
>
> ========================================
> # delimit;
>
> sysuse auto, clear;
>
> gen rep78copy=.;
> levels rep78, local(levels);
> local i=0;
> foreach l of local levels {;
> local i = `i' + 1;
> replace rep78copy=`i' if rep78==`l';
> };
>
> tab rep78copy, gen(rep78_);
> levels rep78copy, local(levels1);
> levels foreign, local(levels2);
>
> foreach l1 of local levels1 {;
> foreach l2 of local levels2 {;
> count if rep78copy==`l1' & foreign==`l2';
> replace rep78_`l1'=. if r(N)==0 & foreign==`l2';
> };
> };
>
> graph bar rep78_* , stack percent over(foreign)
> blabel(bar,pos(center));
> ========================================
Scott Merryman
> > How about replacing the 0 values with "." for the cases where the
> > variable has no values.
> >
> > Example
> >
> > . sysuse auto
> > (1978 Automobile Data)
> >
> > . qui tab rep, gen(D)
> >
> > . replace D1 = . if fore == 1
> > (21 real changes made, 21 to missing)
> >
> > . replace D2 = . if fore == 1
> > (21 real changes made, 21 to missing)
> >
> > . graph bar D* , stack percent over(fore) blabel(bar,pos(center))
> >
Friedrich Huebler
> > > Is it possible to prevent the drawing of selected bar labels in a
> > > stacked bar chart? Take a look at this example:
> > >
> > > . sysuse auto, clear
> > > . xi: graph bar i.rep78, stack percent over(foreign) blabel(bar,
> > > pos(center))
> > >
> > > The bar for "Foreign" has no cases with rep78=2 and therefore a 0
> > is
> > > drawn at the bottom of the bar. Can the drawing of this 0 be
> > > prevented? I found no solution in the manual or in the Statalist
> > > archive.
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/