Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: Suppress labeling of categorical axis values in graph bar/hbar


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: Suppress labeling of categorical axis values in graph bar/hbar
Date   Mon, 20 Dec 2010 15:16:08 +0000

<>


On Dec 20, 2010, at 8:12 AM, Davide Cantoni wrote:

> Eric -- thanks for your reply. It is somehow convincing me that I am
> almost as well off by typing out the relabel() option manually.
> 


Sure, I alluded to this in my post -- its utility will depend on the size of your data (and how often you might expect to change the code -- if you had to re-type the relabel() option several times because of mistakes or new/updated data, etc, then this code would be increasingly useful).  Again, this code could be significantly simplified if someone figures out how to use the local to store/pass the double quotes to relabel().



> The other thing I wonder is how to get rid of the numbers on the
> categorical axis. In your second code, which works fine, there are
> still the ordinal numbers of each observation, instead of the names,
> left. There's no way to suppress those?
> 


There are no numbers for each "make" categorical observation on my graph -- I'm not sure how/why you see them.  Did you augment the code I sent?  I ran this with Stata 11 MP, Mac OSX, using the -scheme s2color- (though I don't know how this makes a difference).


Make sure there is a space between the double quotes in the line (location of space marked with a capital X below for illustration):

if !inlist(`n', 1, 20, 40, 60 , 74)  ///
replace o`varname' = o`varname' + `" `n' "X" "'

You may have lost that space when copy/pasting the code from your email client to your Stata do-file editor.


__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754
Fax: +979.845.0249
http://ppri.tamu.edu



> 
> 
> On 18 December 2010 23:31, Eric Booth <[email protected]> wrote:
>> <>
>> 
>> I think that working with the relabel option is your best bet.  You can automate
>>  the creation of the  relabel() expression with a loop, but normally I'd suggest
>>  building it in a local macro , but I couldn't get the local to store the double
>> quotes for use in the relabel option.
>> 
>> Here's some examples of things I tried:
>> 
>> ***!
>> sysuse auto, clear
>> forval n = 1/`=_N' {
>>        loc la1 `la1' `"`n' " " "'
>>        loc la2 `la2' "`n' " " "
>>        loc la3 `la3' `n' `" "'
>>        loc la4 `"`la4' `n' " "'"'
>>        }
>> forval n = 1/4 {
>>        di in y as smcl "`n' {hline}"
>>        di in g "`la`n''"
>>        }
>> ***!
>> Anyone have an idea of how to store the double quotes  in a local to pass to the
>> relabel option?
>> 
>> 
>> Since using the local didn't work for me, here's a less straightforward solution
>> using a couple of string variables (which are limited to 244 chars) to do essentially
>> the same thing:
>> 
>> ***********!
>> sysuse auto, clear
>> 
>> foreach v in "1/15" "16/30" "31/45" "46/60" "61/74" {
>> **get variable name**
>> loc varname `v'
>> loc varname: subinstr local varname `"/"' "", all
>> 
>> **create "o" vars with relabel option**
>> g o`varname' = ""
>> forval n = `v' {
>> if !inlist(`n', 1, 20, 40, 60 , 74)  ///
>> replace o`varname' = o`varname' + `" `n' " " "'
>> if inlist(`n', 1, 20, 40, 60 , 74)  ///
>> replace o`varname' = o`varname' + `" `n' "`=make[`n']' ""'
>>  }
>> }
>> 
>> ds o* //<-- Here's the new vars for relabel()
>> 
>> graph hbar (asis) price, bargap(10)  ///
>>  over(make, relabel(`=o115' ///
>>  `=o1630'  `=o3145'  `=o4660'  `=o6174')  ///
>>  label(angle(horizontal) labsize(vsmall) labgap(5)))
>> 
>> ***********!
>> 
>> After kludging all this together, it might be more work than typing out
>>  the relabel()  for 74 obs in the auto.dta;  however, it would be useful
>> for much  larger datasets.  (However,  you can't get too much larger
>> before  the bars become indistinguishable.)
>> 
>> 
>> - Eric
>> __
>> Eric A. Booth
>> Public Policy Research Institute
>> Texas A&M University
>> [email protected]
>> Office: +979.845.6754
>> Fax: +979.845.0249
>> http://ppri.tamu.edu
>> 
>> 
>> On Dec 18, 2010, at 3:13 PM, Davide Cantoni wrote:
>> 
>>> Hello,
>>> 
>>> I would like to draw a bar chart displaying the values of a given
>>> variable for all the observations in the dataset. However, since there
>>> are many observations, I would like to label only some of them (some
>>> representative cases). Conceptually, this is similar to this working
>>> example:
>>> 
>>> *** begin example
>>> sysuse auto, clear
>>> graph hbar (asis) price, over(make, sort(price))
>>> *** end example
>>> 
>>> As you can see, putting all the labels on the categorical axis leads
>>> to overcrowding. What I would like to do is to label only some
>>> representative examples, e.g. only "Renault Le Car", "VW Diesel" and,
>>> say, "Lincoln Versailles".  I guess I can achieve the suppression of
>>> labels with some relabel() command inside the over() parenthesis, but
>>> given the large number of observations, this would be incredibly
>>> cumbersome. Any other suggestions?
>>> 
>>> Thanks in advance,
>>> 
>>> Davide
>>> *


*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index