Statalist The Stata Listserver


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

RE: st: Weighted Labels


From   "Maarten Buis" <[email protected]>
To   <[email protected]>
Subject   RE: st: Weighted Labels
Date   Wed, 18 Apr 2007 15:46:05 +0200

Guido Tiemann <[email protected]> writes:
> I know that there are "weighted markers" in a scatterplot (Stata 9);
> however "weights are ignored when the mlabel() option is specified"
> (Graphics Manual, p 293).
>
> Does anyone know another option so as to produce "weighted labels" in a
> scatterplot?

--- Derek Wagner, StataCorp answered:
> One way to get around the problem of using marker labels with weighted
> markers would be to create overlaid plots using the same values each time
> (one graph would be weighted while the other graph would show the labels). 
>
> For the graph showing only the labels, you can hide the markers with the
> -msymbol(none)- option.
> 
> Your resulting command might look like the following:
> 
>. twoway (scatter y x, msymbol(none) mlabel(z)) (scatter y x [weight=w])


An alternative is to overlay different scatter plots with 
labels in different fontsizes. 

In the example below I want to change the fontisize 
depending on displacement. I first create a variable that 
groups displacement into 11 equally spaced groups. Than I 
store the values into the local levels using the -levelsoff- 
command. For each group I want to graph a scatter of price 
versus mpg with its own fontsize for the labels, and overlay 
these scatters graphs. I will store the entire command in 
the local gr (so I can automatically build the command by 
looping over the levels). 

The first scatter is a bit different (it contains "twoway
scatter" instead of "scatter" and "legend(off)") so I create
that outside the loop. To make that work I need to prevent 
the loop to include the first level, otherwise these would 
be plotted twice. To remove the first level from the local 
levels I use the command -gettoken-. 

Two other things need attention: First, the local gr contains 
a very long string. To ensure that the maximum string length 
doesn't bite I took care not to include an equal sign when 
declaring the local gr. Second to wrap the local command over 
multiple lines I used the -#delimit ;- command since you 
can't use /**/ to wrap lines inside a string since it will 
just be considered as part of the string. 

*------------------ begin example -----------------
sysuse auto, clear
sum displacement
local incr = (r(max)-r(min))/12
local min = r(min) 
local max = 10000
egen grdispl = cut(displacement), at(`min'(`incr')`max')

qui levelsof grdispl, local(levels)
gettoken first levels: levels 

local size = .5
#delimit ;
local gr "twoway scatter price mpg 
         if grdispl == `first', 
         mlabel(make) mlabpos(0) msymbol(i) 
         mlabsize(*`size') legend(off) " ;

foreach lev of local levels { ;
	local size = `size' + .1 ;
	local gr "`gr' || scatter price mpg 
                if grdispl == `lev', 
                mlabel(make) mlabpos(0) msymbol(i)
                mlabsize(*`size') " ;
} ;
#delimit cr

`gr'
*------------------ end example ------------------
(For more on how to use examples I sent to the Statalist, see:
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

Hope this helps,
Maarten

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology 
Vrije Universiteit Amsterdam 
Boelelaan 1081 
1081 HV Amsterdam 
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434 

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------

 



*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index