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

st: RE: histogram format

From   "Nick Cox" <>
To   <>
Subject   st: RE: histogram format
Date   Wed, 3 Sep 2003 14:12:56 +0100

Jenny Baxter

> >I am producing a set of histograms using, for example,
> >
> >histogram educ, discrete percent addlabel xlabel(1 2 3 9 , 
> valuelabel 
> >noticks ) ...
> >
> >
> >This produces a histogram of my variable (education), showing the 
> >percentage in each category.
> >
> >I have two seemingly minor problems
> >1 I want to be able to format the label at the top of the 
> bar - currently 
> >it mostly formats to 2 decimal points, but this isn't 
> consistent, and I 
> >really only want one decimal point. Can I do this?

There should be, perhaps, an option -mlabelformat()- 
for this, but I can't see one, or a way of faking one. 

Here is a work-around for -histogram-, illustrated on a 
similar problem. 

There is probably a much better work-around, but this works. 

. sysuse auto, clear

. count if !mi(rep78) 

. bysort rep78 : gen pc = 100 *  _N / r(N) 

. gen percent = string(pc, "%2.1f") 

. twoway histogram rep78, discrete percent 
xlabel(1 2 3 4 5 , valuelabel noticks) 
|| scatter pc rep78, mlabel(percent) mlabpos(12) 
ms(none) legend(off) ytitle(percent)

> >2 The education variable on the x-axis is plotted as if it 
> is ordinal in 
> >that it allows space on the axis for all values between 3 
> and 9, even 
> >though these are not even valid values of the variable. Do 
> I have to 
> >recode the variable, or is there another one to make the 
> categories be 
> >treated as nominal?

Rather, the educational variable on the x axis is treated 
as interval (differences matter), and you want it treated as 
ordinal (differences don't matter, but order does). 

In a strong sense, this is what a histogram _is_: a plot 
of a frequency distribution for a quantitative variable 
in which the values of the variable are taken literally, 
meaning numerically. 

One alternative is to recode by a mapping to successive 
integers. This is easier than -recode-: 

. egen Educ = group(educ), label
. _crcslbl Educ educ 

Another alternative is user-written -catplot- from 
SSC. One advantage of that is that you don't need
any prior trickery: you can do it all in one command, 
and your format problem is also directly soluble. 

. ssc inst catplot 
. catplot bar educ , percent bar(1, bstyle(histogram)) 
blabel(bar, format(%2.1f))


*   For searches and help try:

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