st: Overlapping variable labels

 From RJ Harris To statalist@hsphsun2.harvard.edu Subject st: Overlapping variable labels Date Mon, 22 Aug 2005 16:54:30 +0100

```Have been having a problem of my own with the above as I'm often getting
groups of observations clustered together. The way I thought to deal with
this is to use the mlabvposition() option so that a variable can be used to
set the position of the label on a clock face.

Of course, this is rather tedious to set by hand, so the solution I came up
with was to do a cluster analysis on the co-ordinates so they're in
sensible groups, then do some trigonometry to set the clock positions such
that labels are spread out around each group of observations. It doesn't
work perfectly and takes a bit of fiddling with cluster sizes sometimes,
but might be worth a try. I don't know if anyone's had a go at this
approach before and already done a far better job, but here is some example
code:

gen temp = _N/5
recast int temp, force
replace temp = 1 if temp == 0
local num = temp
drop temp

cluster kmeans x y, k(`num') gen(clNo)
summ clNo
local max = r(max)
gen xx = .
gen yy = .

forvalues i = 1/`max'{
summ x if clNo == `i'
replace xx = x-r(mean) if clNo == `i'
summ y if clNo == `i'
replace yy = y-r(mean) if clNo == `i'

replace xx = 1 if xx == 0	// some clusters only 1 obs
replace yy = -1 if yy == 0
}

gen clockVar = .
replace clockVar = 3-12*atan(yy/2*xx)/(2*_pi) if xx > 0 & yy > 0
replace clockVar = 3-12*atan(yy/2*xx)/(2*_pi) if xx > 0 & yy < 0
replace clockVar = 9-12*atan(yy/2*xx)/(2*_pi) if xx < 0 & yy < 0
replace clockVar = 9-12*atan(yy/2*xx)/(2*_pi) if xx < 0 & yy > 0
recast int clockVar, force
replace clockVar = 12 if clockVar == 0
sort clNo clockVar
list

scatter y x, mlabel(name) mlabvposition(clockVar)

----------------------
RJ Harris, Social Medicine
epzrjh@bristol.ac.uk
*
*   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/
```