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: graphing median values against time


From   Joe Canner <jcanner1@jhmi.edu>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: graphing median values against time
Date   Wed, 9 Apr 2014 18:23:16 +0000

Don,

You can avoid the error by putting the -legend()- option up front with the other generic -twoway- options instead of attaching it to one particular added plot:

lgraph `name' following if visspell !=. & recnum == 1, by(aborig) xlabel(0 3 9 15, labsize(tiny)) stat(mean) title("Mean `name' (Median:nonFN:Navy; FN:Gold)",size(small)) xlabel(#10) legend(label(1 "non-FN") label(2 "FN")) alsocollapse((median) median = `name') addplot((line median following if aborig == 0, lcolor(dknavy) clwidth(thick)) || (line median following if aborig == 1, lcolor(red) clwidth(thick)))

However, because -lgraph- also produces a legend, it seems that the -lgraph- legend supersedes your custom legend.  Adding the -nolegend- option for -lgraph- doesn't help because it adds a -legend(off)- option to the -twoway- command, which suppresses the legend entirely instead of suppressing the production of a default -legend()- option.  You might want to suggest to the author an alternative way to interpret the -nolegend- option.

Another alternative would be to use the -nopreserve- option for -lgraph-, which allows you to pull the -twoway- command that is generated from the r(command) and r(options) macros and create your own customized graph.  For example, you could substitute the default legend with your customized legend and run the -twoway- command yourself.

Regards,
Joe

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Donald Spady
Sent: Wednesday, April 09, 2014 12:43 PM
To: Statalist Statalist
Subject: Re: st: graphing median values against time

I get it to work if I do, starting at also collapse
alsocollapse((median) median = `name') addplot((line median followint if aborig == 0, lcolor(dknavy) clwidth(thick)) || (line median followint if aborig == 1, lcolor(red) clwidth(thick)))

In other words if I bracket each of the add plot commands i.e. addplot((command set 1)||(command set2))

I am now trying to get even fancier, because I want to label the lines in add plot but am running into a bracket problem, i.e.
alsocollapse((median) median = `name') addplot((line median followint if aborig == 0, lcolor(dknavy) clwidth(thick)) || (line median followint if aborig == 1, lcolor(red) clwidth(thick)) legend(label(0 "non-FN") label(2 "FN")))

the brackets are balanced here, but Stata stops and says
) required
r(100)

I have counted the brackets many times and do not see where the issue lies.

I think that you may be right in that there could be some form of error in lgraph.
I did the trace as Nick suggests and it always burps at the two-way command.  It gets to the part ylabel(#10) [which is just asking for 10 nicely spaced numbers]
There is no common between label(#10) and alsocollapse. There is nothing in the lgraph command to suggest there should be a comma there.  If I put a comma in, I get the message 'invalid alsocollapse'.   

For many purposes, I am satisfied with what I have, however it would be really nice to have that extra legend and it makes the graph a bit more self explanatory. It needs to be more or less automatic, as I have a lot of graphs to create.

Thanks for all your help.

Don 


On Apr 9, 2014, at 9:34 AM, Joe Canner <jcanner1@jhmi.edu> wrote:

> Don,
> 
> I had a hard time reproducing your error, perhaps because I don't have your data set, but I was eventually able to get results similar to what you found.  I think this might be a bug in -lgraph-.  If you do a -set trace on- (preceded by -set tracedepth 1- if desired) you can see the -twoway- statement that -lgraph- generates and see what might be causing the error.  It appears that when you add the -lcolor(black)- option, -lgraph- gets confused and adds too many commas.
> 
> For those who are more familiar with the ins and outs of -twoway- here are the two commands generated by -lgraph- corresponding to Don's two scenarios (I had to take the label() options out in order to get it to run):
> 
> twoway (connect statistic following if aborig == 0, color(navy) lpat(solid)  ) (connect statistic following if aborig == 1, color(maroon) lpat(solid)  )   || line median following if aborig == 0, lcolor(dknavy) clwidth(thick)|| line median following if aborig == 1, ytitle(`"Mean "') legend(on order( 1 `"0"' 2 `"1"' )) title("Mean cumten (Median:nonFN:Navy; FN:Gold)",size(small))
> 
> twoway (connect statistic following if aborig == 0, color(navy) lpat(solid)  ) (connect statistic following if aborig == 1, color(maroon) lpat(solid)  )   || line median following if aborig == 0, lcolor(dknavy) clwidth(thick)|| line median following if aborig == 1, lcolor(black), ytitle(`"Mean "') legend(on order( 1 `"0"' 2 `"1"' )) title("Mean cumten (Median:nonFN:Navy; FN:Gold)", size(small))
> 
> Hope this helps.
> 
> Regards,
> Joe Canner
> Johns Hopkins University School of Medicine
> 
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Donald Spady
> Sent: Wednesday, April 09, 2014 10:58 AM
> To: Statalist Statalist
> Subject: Re: st: graphing median values against time
> 
> I dont think that is the case.  I used a Regex search in two different text editors to look for non-ASCII characters and came up with nothing.  Besides, if there was a non-ascii char, would it not show up in the other commands, where I had not added the lcolor(black) bit.  I have run it many times without that option and no problems.
> 
> Don
> 
> On Apr 9, 2014, at 8:06 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> 
>> I'd check for strange characters. Copy your command line into a string
>> variable and look for unusual ASCII characters with -charlist- (SSC)
>> or any alternative of your choice.
>> Nick
>> njcoxstata@gmail.com
>> 
>> 
>> On 9 April 2014 14:25, Donald Spady <dspady@ualberta.ca> wrote:
>>> Tim
>>> After digesting your and Nick's advice I have used the lgraph command to good effect; however I have one problem.  If I write:
>>> foreach name in "cumten" "cumswol" {
>>> igraph `name' following if visspell !=. & recnum == 1, by(aborig) label(0 3 9 15, lab size(tiny)) stat(mean) title("Mean `name' (Median:nonFN:Navy; FN:Gold)",size(small)) label(#10) also collapse((median) median = `name') addplot(line median following if aborig == 0, lcolor(dknavy) clwidth(thick)|| line median following if aborig == 1)
>>> }
>>> 
>>> everything works fine and I get pretty pictures.
>>> 
>>> BUT if I write:
>>> foreach name in "cumten" "cumswol" {
>>> lgraph `name' following if visspell !=. & recnum == 1, by(aborig) label(0 3 9 15, lab size(tiny)) stat(mean) title("Mean `name' (Median:nonFN:Navy; FN:Gold)",size(small)) label(#10) also collapse((median) median = `name') addplot(line median following if aborig == 0, lcolor(dknavy) clwidth(thick)|| line median following if aborig == 1, lcolor(black))
>>> }
>>> The only change is the addition of lcolor(black) at the end of the command.
>>> 
>>> I get the message:
>>>       invalid 'title'
>>>       r(198)
>>> 
>>> Why.  And how do I fix it.
>>> 
>>> Many thanks.
>>> Don
>>> 
>>> On Mar 30, 2014, at 8:43 PM, Timothy Mak <tshmak@hku.hk> wrote:
>>> 
>>>> <>
>>>> Thanks Nick very much for helping to answer my question.
>>>> 
>>>> To plot more than one summary statistics is a bit difficult, and you do have to use the -addplot- and the -alsocollapse- options. If you hadn't specified -by(aborig)-, then Nick's syntax would be almost right, except that you don't need the -if bvisittm1N- in the end, i.e. -addplot(line median bvisittm1N)- would do. However, because you have specified -by(aborig)-, you really need to do:
>>>> 
>>>> addplot(line median bvisittm1N if aborig == 1 || line median bvisittm1N if aborig == 2)
>>>> 
>>>> supposing your variable aborig has 2 levels (1,2).
>>>> 
>>>> To understand the use of the -addplot- and the -alsocollapse- option a bit more, you may want to use the -nopreserve- option. This gives you the dataset that -lgraph- actually uses to plot your graph. Also, have a look at the macros `r(command)' and `r(options)'. This gives you the -twoway- syntax that lgraph uses on the collapsed data. Be sure to use command double quotes though when you display the macros, i.e.:
>>>> 
>>>> di `"`r(command)'"'
>>>> di `"`r(options)'"'
>>>> 
>>>> I hope that helps.
>>>> 
>>>> Tim
>>>> 
>>>> 
>>>> -----Original Message-----
>>>> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
>>>> Sent: 31 March 2014 08:38
>>>> To: statalist@hsphsun2.harvard.edu
>>>> Subject: Re: st: graphing median values against time
>>>> 
>>>> You *must* specify a -twoway- subcommand, not -lgraph-, in the
>>>> -addplot()- call. This is documented in the help for -addplot option-.
>>>> 
>>>> I doubt very much that giving the same variable names as in the main
>>>> call will do what you want.
>>>> 
>>>> My second guess is that you need something more like
>>>> 
>>>> alsocollapse((median) median=totend28)
>>>> addplot(line median bvisittm1N if bvisittm1N)
>>>> 
>>>> Nick
>>>> njcoxstata@gmail.com
>>>> 
>>>> 
>>>> On 31 March 2014 01:29, Donald Spady <dspady@ualberta.ca> wrote:
>>>>> Nick
>>>>> I had tried various combinations of that command and could not get it to work.
>>>>> 
>>>>> here is my command
>>>>> .lgraph tottend28  bvisittm1N if bvisittm1N < 55, by(aborig) xlabel(0(3)54) stat(mean)  alsocollapse((median)) addplot(lgraph tottend28  bvisittm1N if bvisittm1N <55, by(aborig))
>>>>> 
>>>>> It doesn't work.  I get the error
>>>>> lgraph is not a twoway plot type
>>>>> r(198);
>>>>> 
>>>>> Don
>>>>> On Mar 30, 2014, at 5:14 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>>>>> 
>>>>>> Timothy explains how to do that in the help.
>>>>>> 
>>>>>> addplot(plot) This allows additional twoway plots to be plotted.
>>>>>> See addplot_option. Note that
>>>>>>     because lgraph collapses the data before plotting in twoway,
>>>>>> plots given in addplot(plot)
>>>>>>     should also refer to the collapsed data. Specify in
>>>>>> alsocollapse(clist) any additional data
>>>>>>     that need to be collapsed.
>>>>>> 
>>>>>> So, if you want median as well as mean you must specify
>>>>>> 
>>>>>> alsocollapse(median) addplot(<whatever>)
>>>>>> 
>>>>>> Note that nothing obliges you to use another -line- call.
>>>>>> 
>>>>>> I haven't use -lgraph-, just looked at its help, but Timothy provides
>>>>>> lots of functionality.
>>>>>> Nick
>>>>>> njcoxstata@gmail.com
>>>>>> 
>>>>>> 
>>>>>> On 30 March 2014 23:51, Donald Spady <dspady@ualberta.ca> wrote:
>>>>>>> Timothy
>>>>>>> I am using your -lgraph- command and wonder if it is possible to have TWO stats created (e.g. mean and median) for the same variable(s) at the same time.  I want to plot both mean and median and cannot create an overlay to do so.
>>>>>>> 
>>>>>>> Many thanks
>>>>>>> Don
>>>>>>> On Mar 28, 2014, at 1:29 AM, Timothy Mak <tshmak@hku.hk> wrote:
>>>>>>> 
>>>>>>>> The command -lgraph- that I wrote is for exactly this kind of problem if you don't want to create all the variables before plotting a graph.
>>>>>>>> You can see it by typing:
>>>>>>>> ssc des lgraph
>>>>>>>> 
>>>>>>>> Tim
>>>>>>>> 
>>>>>>>> -----Original Message-----
>>>>>>>> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Donald Spady
>>>>>>>> Sent: 28 March 2014 08:59
>>>>>>>> To: Statalist Statalist
>>>>>>>> Subject: Re: st: graphing median values against time
>>>>>>>> 
>>>>>>>> Nick
>>>>>>>> It works.  Just as I needed. Many thanks.
>>>>>>>> John: That suggestion didn't work the way I wanted it to, but thanks for the idea.
>>>>>>>> 
>>>>>>>> Don
>>>>>>>> On Mar 27, 2014, at 5:48 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>>>>>>>> 
>>>>>>>>> Sure. If you use -egen-'s -median()- and -mean()- functions to get the
>>>>>>>>> variables you want, then it's any graph you want.
>>>>>>>>> 
>>>>>>>>> As the median and mean values are repeated, don't plot them repeatedly.
>>>>>>>>> 
>>>>>>>>> Here are some dopey examples.
>>>>>>>>> 
>>>>>>>>> . webuse grunfeld
>>>>>>>>> 
>>>>>>>>> . egen median = median(invest), by(year)
>>>>>>>>> 
>>>>>>>>> . egen mean = mean(invest), by(year)
>>>>>>>>> 
>>>>>>>>> . egen tag = tag(year)
>>>>>>>>> 
>>>>>>>>> . line mean median year if tag
>>>>>>>>> 
>>>>>>>>> For finer subdivisions, just use more variables as arguments to -by()-.
>>>>>>>>> 
>>>>>>>>> Nick
>>>>>>>>> njcoxstata@gmail.com
>>>>>>>>> 
>>>>>>>>> 
>>>>>>>>> On 27 March 2014 22:34, Donald Spady <dspady@ualberta.ca> wrote:
>>>>>>>>>> Dear all
>>>>>>>>>> I want to graph the median and mean values of a series of variables against a time variable (on the X axis) and with the option of stratifying the variables by another one (such as sex or disease state).  I think I can do this by collapsing the data but is it possible to do this while retaining the data set in its 'original' state.  I have searched the archives and the reference manuals but have not come up with anything that seems to be what I want.
>>>>>>>>>> In many ways what I would like is a box plot with JUST the median (or mean) being plotted: no box, no outliers.  Is that possible.
>>>>>>>>>> 
>>>>>>>>>> Thanks
>>>>>>>>>> Don
>>>>>>>>>> *
>>>>>>>>>> *   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/
>>>>>> 
>>>>> 
>>>>> 
>>>>> *
>>>>> *   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/
> 
> *
> *   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index