Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: Bar labels in stacked bar chart


From   Friedrich Huebler <[email protected]>
To   [email protected]
Subject   st: Re: Bar labels in stacked bar chart
Date   Tue, 6 Apr 2004 09:04:16 -0700 (PDT)

Scott,

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));
========================================

Friedrich Huebler

--- Scott Merryman <[email protected]> wrote:
> 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))
> 
> Hope this helps,
> 
> Scott
> 
> 
> ----- Original Message ----- 
> From: "Friedrich Huebler" <[email protected]>
> To: <[email protected]>
> Sent: Saturday, April 03, 2004 4:39 PM
> Subject: st: Bar labels in stacked bar chart
> 
> 
> > 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.
> >
> > Thank you for your help.
> >
> > Friedrich Huebler

__________________________________
Do you Yahoo!?
Yahoo! Small Business $15K Web Design Giveaway 
http://promotions.yahoo.com/design_giveaway/
*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index