Friday, March 03, 2006

st: RE: graph twoway bar horizontal vs. graph hbar

As it happens, I started work last night on a rough-and-ready program in this territory called -textbarplot-.

Re-creating your data (note the short-cuts)

#delimit ; clear; set obs 11; gen TYPE_YEAR = _n;

lab define TYPE_YEAR 1 "General, 1900" 2 "General, 1930" 4 "Mental, 1900" 5 "Mental, 1930" 7 "TB, 1900" 8 "TB, 1930" 10 "OS, 1900" 11 "OS, 1930"; lab val TYPE_YEAR TYPE_YEAR;

gen AVE_NUM_BED=82 if TYPE_YEAR==1; replace AVE_NUM_BED=81 if TYPE_YEAR==2; replace AVE_NUM_BED=508 if TYPE_YEAR==4; replace AVE_NUM_BED=780 if TYPE_YEAR==5; replace AVE_NUM_BED=103 if TYPE_YEAR==7; replace AVE_NUM_BED=130 if TYPE_YEAR==8; replace AVE_NUM_BED=68 if TYPE_YEAR==10; replace AVE_NUM_BED=60 if TYPE_YEAR==11;

I then went

. textbarplot TYPE_YEAR AVE_NUM_BED, plotregion(margin(zero))

which gets you much of the way there. (You can add further options.)

As I have suggested various times in this list, for the flexibility you want, -twoway bar, hor- is the way to go, as I don't think -graph hbar- can solve your problem 2) without creating further problems.

Your problem 1) is I think solved by the option above.

Note that -textbarplot- can be recast to a different form by -recast(dot)-, -recast(dropline)-, -recast(spike)- by courtesy of some cute Wigginsypokery.

Nick n.j.cox@durham.ac.uk

textbarplot [integers_of_row_positions_on_y_axis] source_of_text_on_y_axis bar_magnitude [if] [in] [, SCatter(scatter_options) twoway_bar_options plot() addplot()]

*!!! not much testing yet *! 1.0.0 NJC 3 March 2006 program textbarplot version 9 syntax varlist(min=2 max=3) [if] [in] /// [, SCatter(str asis) PLOT(str asis) ADDPLOT(str asis) * ]

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

if "`3'" == "" { args text bar tempvar y gen long `y' = _n if `touse' local obs "`y'" } else { args y text bar capture confirm numeric variable `y' if _rc { di as err "`y' not numeric" exit _rc } capture assert `y' == floor(`y') if `touse' if _rc { di as err "`y' not integer variable" exit 498 } tempvar Y obs clonevar `Y' = `y' local y "`Y'" gen long `obs' = _n } capture confirm numeric variable `bar' if _rc { di as err "`bar' not numeric" exit _rc } levelsof `obs' if `touse', local(levels)

local lbl : value label `text'

if "`lbl'" == "" { tempname lbl foreach l of local levels { label def `lbl' `= `y'[`l']' `"`= `text'[`l']'"', add } } else { tempname newlbl foreach l of local levels { local val : label `lbl' `= `text'[`l']' label def `newlbl' `=`y'[`l']' `"`val'"', add } local lbl "`newlbl'" } label val `y' `lbl'

preserve keep if `touse' levelsof `y', local(levels) } twoway bar `bar' `y', /// yla(`levels', valuelabel noticks ang(h)) /// hor ysc(reverse) ytitle("") xtitle("") /// barw(0.6) legend(off) `options' /// || scatter `y' `bar', ytitle("") ms(none) `scatter' /// || `plot' /// || `addplot' end Hiroshi Maeda > I use Stata 9.1 for Windows XP. I am trying to draw what is > supposed to be > a simple bar graph. I experimented with and compared the "graph twoway > bar, horizontal" and "graph hbar" commands. Please find below > sample data > and commands for illustration. First I tried the "graph twoway bar, > horizontal" command. The resultant graph (GRAPH_01) was very > close to what > I wanted except that there was a small gap between the y axis and the > location where bars began. Then I tried the "graph hbar" command. The > resultant graph (GRAPH_02) took care of what I found lacking > in GRAPH_01 > but it displayed the labels of categories which I wanted to > be invisible > (that is, I wanted to create a gap between hospital categories); those > categories are 3, 6, & 9 on the y axis. > > Does anyone know: > 1) How to eliminate a gap(space) between the y axis and the > location where > bars begin in GRAPH_01, using the "graph twoway bar, > horizontal" command? > 2) How to make the labels 3, 6, & 9 on the y axis (the > categories intended > to be invisible for an aesthetic reason) disappear, using the > "graph hbar" > command? > > > *The sample data and commands begin here ============================; > > clear; > > set obs 11; > > gen TYPE_YEAR=.; > replace TYPE_YEAR=1 in 1; > replace TYPE_YEAR=2 in 2; > replace TYPE_YEAR=3 in 3; > replace TYPE_YEAR=4 in 4; > replace TYPE_YEAR=5 in 5; > replace TYPE_YEAR=6 in 6; > replace TYPE_YEAR=7 in 7; > replace TYPE_YEAR=8 in 8; > replace TYPE_YEAR=9 in 9; > replace TYPE_YEAR=10 in 10; > replace TYPE_YEAR=11 in 11; > > lab define TYPE_YEAR 1 "General, 1900" 2 "General, 1930" 4 > "Mental, 1900" > 5 "Mental, 1930" 7 "TB, 1900" 8 "TB, 1930" 10 "OS, 1900" 11 > "OS, 1930"; > lab val TYPE_YEAR TYPE_YEAR; > > gen AVE_NUM_BED=.; > replace AVE_NUM_BED=82 if TYPE_YEAR==1; > replace AVE_NUM_BED=81 if TYPE_YEAR==2; > replace AVE_NUM_BED=508 if TYPE_YEAR==4; > replace AVE_NUM_BED=780 if TYPE_YEAR==5; > replace AVE_NUM_BED=103 if TYPE_YEAR==7; > replace AVE_NUM_BED=130 if TYPE_YEAR==8; > replace AVE_NUM_BED=68 if TYPE_YEAR==10; > replace AVE_NUM_BED=60 if TYPE_YEAR==11; > > > graph twoway (bar AVE_NUM_BED TYPE_YEAR, horizontal barwidth(.8) > fcolor(gs0) lstyle(solid) lw(thin)), > > title("Average Number of Beds", size(medsmall) margin(medsmall)) > > ytitle("") > xtitle("") > > ylabel(1 2 4 5 7 8 10 11, labsize(vsmall) valuelabel > angle(horizontal) nogrid notick) > xlabel(0 50 100 (100) 800, labsize(vsmall) grid gmax gmin) > > xline(50 100, lw(vthin)) > > saving(GRAPH_01, replace); > > > graph hbar (asis) AVE_NUM_BED if TYPE_YEAR!=., > over(TYPE_YEAR, label(labsize(vsmall)) gap(50)) > > bar(1, fcolor(gs0) lstyle(solid) lw(thin)) > > title("Average Number of Beds", size(medsmall) margin(medsmall)) > > ylabel(0 50 100 (100) 800, labsize(vsmall) grid gmax gmin) > ytitle("") > yline(50 100, lw(vthin)) > > saving(GRAPH_02, replace); > > > *The same data and commands end here ==============================;

* * 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/


Tag:


Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?