Statalist


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

Re: st: weight marker symbol fill size and color?


From   "Friedrich Huebler" <fhuebler@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: weight marker symbol fill size and color?
Date   Sun, 24 Aug 2008 14:14:13 -0400

Dave,

I asked a similar question on July 29.

http://www.stata.com/statalist/archive/2008-07/msg01081.html

A weighted scatter plot can be combined with markers in different
colors as I learned from Stata tech support. I neglected to share the
solution with the list so let me explain today how this can be done.
The solution involves duplicating the observations and recoding the
group variable in the duplicates. This way the weights are distributed
across all groups.

Say we want to take the census data from your example and identify the
regions with different colors.

. webuse census
. scatter death medage [w=pop65p], msymbol(Oh) name(A)
. twoway (scatter death medage if region==1 [w=pop65p], msymbol(Oh))
(scatter death medage if region==2 [w=pop65p], msymbol(Oh)) (scatter
death medage if region==3 [w=pop65p], msymbol(Oh)) (scatter death
medage if region==4 [w=pop65p], msymbol(Oh)), name(B) legend(off)

I turned off the legend so that it is more easy to see that the
markers in graphs A and B have a different size. Next, expand the data
and set the added observations to missing so that they won't appear in
the graph.

. expand 4
. sort state region
. replace death=. if mod(_n,4) > 0

The next step is to recode the region in the added observations.

. recode region (1=2) (2=3) (3=4) (4=1) if mod(_n,4) == 1
. recode region (1=3) (2=4) (3=1) (4=2) if mod(_n,4) == 2
. recode region (1=4) (2=1) (3=2) (4=3) if mod(_n,4) == 3

Now we can draw a graph in which the markers have the same size as in graph A.

. twoway (scatter death medage if region==1 [w=pop65p], msymbol(Oh))
(scatter death medage if region==2 [w=pop65p], msymbol(Oh)) (scatter
death medage if region==3 [w=pop65p], msymbol(Oh)) (scatter death
medage if region==4 [w=pop65p], msymbol(Oh)), name(C) legend(off)

Below is the solution to the example from my original post of July 29.

. clear all
. input x y weight group
1 1 1 1
2 1 2 1
1 2 4 2
2 2 8 2
end
. scatter y x [w=weight], name(A)
. twoway (scatter y x if group==1 [w=weight]) (scatter y x if group==2
[w=weight]), name(B) legend(off)
. expand 2
. replace x = . if _n>(_N/2)
. recode group (1=2) (2=1) if x==.
. twoway (scatter y x if group==1 [w=weight]) (scatter y x if group==2
[w=weight]), name(C) legend(off)

Friedrich

On Sun, Aug 24, 2008 at 11:42 AM, David Airey
<david.airey@vanderbilt.edu> wrote:
> At this page, we see how to change symbol size by using (analytic) weights:
>
> http://www.stata.com/support/faqs/graphics/gph/graphdocs/scatter5.html
>
> e.g.,
>
> webuse census
> scatter death medage [w=pop65p], msymbol(circle_hollow)
>
> Can we change both symbol size and color shading as in the R example below?
>
> -Dave
>
> ###------------------create the test data.frame
> y = c(.4, 1, 2.15, 1.8, 2.7, 2.2, 1.3, .25, 1.1, .1)
> x = c(.35, .4, .41, 1.4, 2.1, 2.25, 2.15, 2.2, 1.45, 1.3)
> z = c(9, 7, 7.5, 7.3, 7, 7.1, 7.75, 9.5, 8, 10)
> d = data.frame(x = x, y = y, z = z)
>
> d$col = gray(.5)
> d$col[d$z>=9] = gray(.2)
> d$col[d$z<7.5] = gray(.7)
> #thikness = sqrt(d$y^2 + d$x^2)
> thikness = abs(d$y) + abs(d$x)
>
> ###----------------simple, 2-D plot
> plot(d$x, d$y, pch=19, cex=(d$z-min(d$z)/1.5), xlim = c(-1.5,4), ylim =
> c(-2.5,5), col=d$col)
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index