Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: AW: Is it possible to create a bar graph with two yvars


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: AW: Is it possible to create a bar graph with two yvars
Date   Thu, 1 Jul 2010 10:44:07 +0100

I don't understand the reservation. Whatever the relation between -sales- and -sales2-, or your analogues thereof, it is sufficient to have values available both just before you -graph-. 

Here is an extension of your examples making clear what I had in mind. 

clear
set obs 1000
egen id = seq(), block(10)
egen time = seq(), to(10)
format time %tq
gen time2=string(time, "%tq")
gen sector = ceil(3 * runiform())
bysort id: replace sector=sector[1]
label define sector 1 "SECTOR 1" 2 "SECTOR 2" 3 "SECTOR 3"
label values sector sector
gen sales=exp(rnormal())*1000
gen sales2=2*sales
graph bar (sum) sales, over(sector) ///
over(time2, label(labsize(vsmall))) asyvars stack legend(size(vsmall))
graph bar (sum) sales (sum) sales2, over(sector) ///
over(time2, label(labsize(vsmall))) asyvars stack legend(size(vsmall))

// new NJC code starts here 

gen obs = _n
rename sales sales1 
reshape long sales, i(obs) j(which)
graph bar (sum) sales ,  over(sector) over(which) ///
over(time2, label(labsize(vsmall))) asyvars stack legend(size(vsmall))

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

A Loumiotis

Thanks Nick for your hint but I'm not sure how I can apply it to my
problem.  In this example sales and sales2 have an exact functional
relationship but usually they will not.  Playing around with graph bar
I think that the asyvars together with the stack option stacks the
bars only when there is only one yvar.  Is this correct?

Nick Cox 

> You may need to -reshape- so that -sales- and -sales2- become one quantitative and one categorical variable.

A Loumiotis

> Thanks for your help Martin.
> By omitting the stack option I do not get what I want.  I would like a
> bar graph of sales and sales2 on the yaxis, time on the x-axis and the
> bars stacked by sector.  For each time period I would like to have two
> "stacked (by sector)" bars, one bar for sales and one for sales2.
> Your suggestion creates six bars for each time period and not two
> stacked bars.

Martin Weiss

>> Omit the -stack- option in the second call...

A Loumiotis

>> I use a variation of an example by Nick Cox to ask my question:
>>
>> clear
>> set obs 1000
>> egen id = seq(), block(10)
>> egen time = seq(), to(10)
>> format time %tq
>> gen time2=string(time, "%tq")
>> gen sector = ceil(3 * runiform())
>> bysort id: replace sector=sector[1]
>> label define sector 1 "SECTOR 1" 2 "SECTOR 2" 3 "SECTOR 3"
>> label values sector sector
>> gen sales=exp(rnormal())*1000
>> gen sales2=2*sales
>> graph bar (sum) sales, over(sector) over(time2,
>> label(labsize(vsmall))) asyvars stack legend(size(vsmall))
>> graph bar (sum) sales (sum) sales2, over(sector) over(time2,
>> label(labsize(vsmall))) asyvars stack legend(size(vsmall))
>>
>>
>> At the end of the code I'm generating two graph bars.  The first one
>> has the variable sales as yvar where the bars are stacked by sector
>> and time is the xvar.
>> What I would like to do is to generate an analogous graph but with two
>> yvars instead of one, sales and sales2.  So for each period I would
>> like to have two bars, one for sales and one for sales2.  However, my
>> second graph bar command does not achieve this. Is it possible to do
>> this with graph bar?  If yes, what am I doing wrong?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index