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

RE: st: RE: bar graph of multiple variables


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: bar graph of multiple variables
Date   Mon, 7 Mar 2005 11:03:00 -0000

Richard Sherman asked: 

----------------------------------
Here is what must be a simple graphing problem; I confess that I am 
still mostly baffled by the Stata 8 graphics syntax.

I have seven variables, c1-c7, that take values 1, 2, or 3.  I want a 
graph of the frequencies of these variables that looks something like 
this:


freq |
      |
      |
      |  *
      |  *                        *
      |  *                        *
      |  *              *         *
      |  *  *        *  *         *  *
      |  *  *  *     *  *  *      *  *  *
      |________________________________________  (... etc.)
         1  2  3     1  2  3      1  2  3        (... etc.)
	
            c1          c2           c3          (... etc.)


I have been slogging away with -graph bar- and have gotten no farther 
than the inelegant and unsatisfying method of generating three 
variables from each variable (measuring the count of each category) and 
using

   graph bar (count) all-of-these-variables

or


   graph bar (count) three-at-a-time
and then

   graph combine these-seven-graphs

But surely there is a better way.  Thanks in advance for any help.
-------------------------------------------------------------------

Clive Nicholas suggested 

...
> use Nick Cox's -catplot- routine, downloadable via SSC.
...
> > to do something like:
> > 
> > . catplot bar continuousvar, over(discretevar) sort percent asyvars
> > 
> > But even this doesn't quite do what Richard wants, which is 
> to bar-graph
> > seven _individual_ variables which all have (presumably) 
> the same three
> > categories _without_ having to specify an -over()- option. 

Scott Merryman then suggested 
 
> Ok, how about something like this:
> 
> *Create data set: 7 variables with values {1, 2, 3}
> clear
> set obs 30
> qui {
> forv i = 1/7 {
> 	gen c`i' = 1
> 	replace c`i' = 2 if uniform() <.4
> 	replace c`i' = 3 if uniform() <.3
> }
> }
> 
> *Create frequency values
> qui {
> forv i = 1/7 {
> 	tempfile file`i'
> 	xcontract c`i', freq(freq) idnum(`i')  saving(`file`i'')
> }
> }
> 
> dsconcat `file1' `file2' `file3' `file4' `file5' `file6' `file7'
> drop c*
> egen id2 = fill(1 2 3 1 2 3)
> 
> forv i = 1/7 {
> 	local relabel  `relabel' `i' C`i'
> }
> 
> graph bar freq, over(id2) over(idnum, relabel( `relabel')) 
> ytitle(Frequency)

Taking the idea from Clive of using -catplot- and from
Scott that a data restructure is needed, another way is along
these lines. First we create an example dataset as did 
Scott 

clear
set obs 30
qui forv i = 1/7 {
 	gen c`i' = 1
 	replace c`i' = 2 if uniform() <.4
 	replace c`i' = 3 if uniform() <.3
}

Then 

gen id = _n 
reshape long c , i(id) 
catplot bar  c _j 

There is no need to get into file manipulations, 
as all can be done in place. 

Nick 
[email protected] 

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