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: Bar graph with frequency


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Bar graph with frequency
Date   Thu, 18 Apr 2013 18:55:15 +0100

Various small tricks are exemplified in this script. The broad tip is:
when -graph bar- frustrates, turn to -twoway bar- and build your own
design.

clear
input sale  ad
 0.4     0.0
 5.7     0.5
 15.7    3.4
 19.1    3.8
 21.2    4.1
 25.5    10.8
 40.0    4.8
 47.4    12.3
 62.0    7.5
 64.2    16.5
 67.4    10.2
 91.8    7.0
 92.2    14.0
 end
 gen salecat = 10 * floor(sale/10)
 label var salecat "sales category"
 egen mean = mean(ad), by(salecat)
 egen count = count(ad), by(salecat)
 twoway bar mean salecat, barw(10) || scatter mean salecat, mla(count)
ms(none) mlabpos(12) legend(off)
 more
 egen salecat2 = group(salecat), label
 label var salecat2 "sales category"
 twoway bar mean salecat2, barw(.8) xla(1/6, valuelabel noticks) ||
scatter mean salecat2, mla(count) ms(none) mlabpos(12) legend(off)
Nick
njcoxstata@gmail.com


On 18 April 2013 17:25, Ray Hawkins <sylviantas@gmail.com> wrote:
> Hi,
>
> I have a related question to the one I asked before. I want to draw a graph
> using the following codes. My question is:
> If I do not specify 'asyvar', I get a simple graph without the legend I
> specified, but I get sales category numbers: 0,10,20,40,60,90 which I want
> to have. If I specify 'asyvar', I get a color graph with the legend I
> specified, but sales category numbers (0,10,20,40,60,90) disappear, which is
> weird. How can I get both legend under the graph and sales category numbers
> right below each bar? I don't need different colors.
>
> egen salecat=cut(sale), at(0 (10) 100)
> levelsof salecat, loc(temp)
> foreach v of loc temp {
>  count if salecat==`v'
> }
> graph bar (mean) ad, over(salecat) exclude0 yti(Ad) b1ti(Sale) asyvar legend
> (label(1 "N=2") label(2 "N=2") label(3 "N=2") label(4 "N=2") label(5 "N=3")
> label(6 "N=2") rows(2) pos(6) )
>
> sale    ad
> 0.4     0.0
> 5.7     0.5
> 15.7    3.4
> 19.1    3.8
> 21.2    4.1
> 25.5    10.8
> 40.0    4.8
> 47.4    12.3
> 62.0    7.5
> 64.2    16.5
> 67.4    10.2
> 91.8    7.0
> 92.2    14.0
>
> Thank you in advance.
> Ray Hawkins.
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of William Buchanan
> Sent: Thursday, April 04, 2013 11:13 AM
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: Bar graph with frequency
>
> Sorry about the confusion Ray.  Can you include the group sizes in a legend?
> If so, you could do something like this to include the sample sizes in a
> legend.
>
> sysuse auto
> egen pr2=cut(price), group(10)
> levelsof pr2, loc(pr)
> foreach v of loc pr {
>         count if pr2==`v'
>         loc gr`v' = r(N)
> }
> gr bar (mean) weight, over(pr2) asyvar legend(label(1 `"N = `gr0'"')
> label(2 `"N = `gr1'"') label(3 `"N = `gr2'"') label(4 `"N = `gr3'"')
> label(5 `"N = `gr4'"') label(6 `"N = `gr5'"') label(7 `"N = `gr6'"')
> label(8 `"N = `gr7'"') label(9 `"N = `gr8'"') label(10 `"N = `gr9'"')
> rows(3) pos(12) span)
>
> HTH,
> Billy
>
> On Apr 4, 2013, at 9:01 AM, Ray Hawkins <sylviantas@gmail.com> wrote:
>
>> Hi Billy,
>>
>> I could not find the answer for my question in -blabel()- option. Can
> you
>> please give me a detailed help? Thank you,
>>
>> Ray.
>>
>> -----Original Message-----
>> From: owner-statalist@hsphsun2.harvard.edu
>> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of William
> Buchanan
>> Sent: Thursday, April 04, 2013 10:32 AM
>> To: statalist@hsphsun2.harvard.edu
>> Subject: Re: st: Bar graph with frequency
>>
>> Check -h gr bar- and look for the option -blabel()-.
>>
>> HTH,
>> Billy
>>
>>
>> On Apr 4, 2013, at 8:27 AM, "Ray Hawkins" <sylviantas@gmail.com> wrote:
>>
>>> Hello,
>>>
>>> I am trying to classify "sale" into 10 groups by equal interval and
>>> make a bar graph like the following. How can I put frequency in each
>>> group in the bar graph?
>>>
>>> egen salecat=cut(sale), at(0 (10) 100) graph bar (mean) ad,
>>> over(salecat)
>>>
>>> sale ad
>>> 0.4  0.0
>>> 5.7  0.5
>>> 15.7 3.4
>>> 19.1 3.8
>>> 21.2 4.1
>>> 25.5 10.8
>>> 40.0 4.8
>>> 47.4 12.3
>>> 62.0 7.5
>>> 64.2 16.5
>>> 67.4 10.2
>>> 91.8 7.0
>>> 92.2 14.0
>>>
>>> Thank you for your help.
>>> Ray Hawkins.
>>>
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>> *   http://www.ats.ucla.edu/stat/stata/
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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