Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: RE: graph plot region size control


From   Laura Gibbons <gibbonsl@u.washington.edu>
To   statalist@hsphsun2.harvard.edu
Subject   RE: st: RE: graph plot region size control
Date   Thu, 8 Nov 2007 10:16:56 -0800 (PST)

I'm pleased and impressed to report that Nick has come up with a way to combine two box plots that plotted different variables (with overlaid lines even!) so that the plot region size is the same and results are easily compared. A nonsense example using the auto data set is below.

Many thanks to Nick for his perseverance and ingenuity!

-Laura

---------- Forwarded message ----------
Date: Thu, 8 Nov 2007 17:18:28 -0000
From: Nick Cox <n.j.cox@durham.ac.uk>
To: Laura Gibbons <gibbonsl@u.washington.edu>
Subject: RE: st: RE: graph plot region size control


I'll reply privately as Statalist has got tangled up at the moment.

I didn't see the complication you mentioned. That makes it trickier.
Consider this. I don't think the overlaid lines work well. I don't know
how to get lines as backdrop that don't extend over the whole plot
region, but I think plain -xli()- would look much better.

------------------------------------------------ cut here
use http://www.stata-press.com/data/r10/auto, clear
preserve

// tuck labels away carefully
local l1 : var label price
local l2 : var label weight
tempfile mylabel
label save origin using `mylabel'

// map 1..5 -> 3..7 to ensure offset from -foreign-'s values of 0 1
replace rep78 = rep78 + 2

stack price foreign weight rep78, into(data foreign) clear

// bring back labels
label def _stack 1 "`l1'" 2 "`l2'"
label val _stack _stack
do `mylabel'
// reverse -rep78- offset in labels
label def origin 3 "1" 4 "2" 5 "3" 6 "4" 7 "5", add
label val foreign origin

// now...
graph hbox data, over(foreign)

// or
stripplot data, over(foreign) box(barw(0.6)) ysc(reverse) ///
addplot(scatteri -0.5 4000 1.5 4000, recast(line) lcolor(gs12) ///
|| scatteri -0.5 8000 1.5 8000, recast(line) lcolor(gs12) ///
|| scatteri 2.5 2000 7.5 2000, recast(line) lcolor(gs12) ///
|| scatteri 2.5 3000 7.5 3000, recast(line) lcolor(gs12)) ///
ytitle(" Repair record
Foreign") ///
xtitle("Price and weight") yla(, noticks)

restore
------------------------------------------------ cut here

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Laura Gibbons
Sent: 07 November 2007 23:42
To: statalist@hsphsun2.harvard.edu
Subject: RE: st: RE: graph plot region size control

Nick, these are some great tricks that I was not aware of. Thank you!

In the second section, the over variable is rep78, not foreign. I
changed
the stack command to:

stack price foreign weight rep78, into(data foreign) clear

Now I get a problem I had before when I tried a less elegant approach.
The groups are now "Domestic Foreign 2 3 4 5". Is there a way to fix
that so that the value 1 is labeled "Foreign" in the first figure and
"1"
in the second? Also there must be a way to supress the not applicable
values; I haven't investigated that yet.

Finally, I'd like the ylines only on the applicable section, 4000, 8000
for foreign and 2000 3000 for rep78. Am I dreaming?!

Many thanks,

Laura


On Wed, 7 Nov 2007, Nick Cox wrote:


If not -by()-, then -over()-:

use http://www.stata-press.com/data/r10/auto, clear
preserve

// tuck labels away carefully
local l1 : var label price
local l2 : var label weight
tempfile mylabel
label save origin using `mylabel'

stack price foreign weight foreign, into(data foreign) clear

// bring back labels
label def _stack 1 "`l1'" 2 "`l2'"
label val _stack _stack
do `mylabel'
label val foreign origin

// now...
graph hbox data, over(foreign) over(_stack) yline(2000 3000 4000 8000)
///
xsize(3.25) ysize(2.375)

restore

Also, consider

. stripplot price weight, by(foreign) box

And many variations on that. (-stripplot- is from SSC.)

Come to think of it, anything wrong with

. graph hbox price weight, over(foreign)

?

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


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Laura
Gibbons
Sent: 07 November 2007 22:48
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: RE: graph plot region size control

Nick and Maartin rightfully took me to task for giving up without
explaining my actual goal.  I apologize and appreciate your continued
interest.

I am trying to make (a series of) graphs that are the same size, where
the
plot regions are also the same size.  Because the lengths of the
labels
vary, the plot regions are adjusted by Stata and I have no way to
control
them directly.  It's not wrong, but it makes quick comparisons more
difficult.  Below is the type of graph I need to make.  The values for
yline will vary between graphs.  Note that in this public example,
price
and weight measure different things, so it would seem silly to try to
compare them, but in my real graphs, the variables are on the same
scale,
and I set the x axis to be the same range in all graphs.

---------
use http://www.stata-press.com/data/r10/auto

graph hbox price, over(foreign) yline(4000 8000) xsize(3.25)
ysize(2.375)
graph export f1.wmf, replace

graph hbox weight, over(rep78)	yline(2000 3000) xsize(3.25)
ysize(2.375)
graph export f2.wmf, replace
--------

Though the overall sizes are the same, the plot region in f2 is larger
because the labels are shorter.

Since I last emailed, I have figured out how to improve things with
the
relabel option (below).  Probably if I used a fixed (monospace) font
or
added more spaces I could get pretty close.  But if there is a way to
do
this with by() or twoway, I'd love to learn it.  And I would like
there
to
be an option to set the plot region size.

---------
graph hbox weight, over(rep78, 			///
relabel(1 "       1" 2 "       2" 3 "       3" 4 "       4" 5 "
5"))
///
yline(2000 3000) xsize(3.25) ysize(2.375)
graph export f3.wmf, replace
---------

Thanks for any advice you may have, and for keeping me honest!
-Laura

On Wed, 7 Nov 2007,
Maarten
buis wrote:

--- Laura Gibbons <gibbonsl@u.washington.edu> wrote:
Unfortunately my graphs are more complicated than the bar graphs in
the original post, so by() isn't an alternative.  But if I could
control the  size of the plot region, I'd be home.  It looks like I
need to settle for  uneven plot region sizes or use a different
graphing program, and neither  option is appealing.
I can tell from experience that a surprising lot can be done with
-by()-, even in very very complicated graphs (though you may need to
use some tricks). So my gues is that you can do this with -by()- and
thus save you a lot of trouble. If you don't tell us in what way your
graphs are so complicated I cannot give you more advise than this
very
vague encouragement to have another good look at the by option.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~
Laura E. Gibbons, PhD
General Internal Medicine, University of Washington
Box 359780
Harborview Medical Center
325 Ninth Avenue, Seattle, WA  98104

phone: 206-744-1842   fax: 206-744-9917
Office address: 401 Broadway, Suite 5122
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~
*
*   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/
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index