Drawing scatter plots


The graph command -twoway scatter- (or -tw scatter-) draws scatter plots. Here we draw the scatter plots of the share of electronics in total export of the Philippines and Malaysia over time.

In Figure 0, we draw a scatter plot of Philippine export shares in electronics. If not specified, Stata will use default options for marker color, size, and shape, axis titles, etc.

tw scatter  exportshare year if reporter==”Philippines”, title(“Figure 0”)

In Figure 1, we specified marker and axis options and added a note.

#delimit ;
tw (scatter  exportshare year if reporter==”Philippines”,
ytitle(“Export Share in Electronics* %”) ylabel(0(10)60, angle(0))
xtitle(“”) xlabel(1965(10)1995 2007)
title(“Figure 1”)
note(“*SITC 2-digit category 77”)
;

In Figure 2, we added Malaysia’s export shares for the same period. By default, Stata will use new colors for added graphs unless this is specified. Here we moved the title to the bottom of the chart by using the suboption pos, which follows clock positions. The default for the title is at 12 o’clock position, pos(12); but we moved it to the 6 o’clcok position, pos(6). We also added the legend() option.

#delimit ;
tw (scatter  exportshare year if reporter==”Philippines”,
msize(large) mlabel(year))
(scatter  exportshare year if reporter==”Malaysia”,
msize(large) mlabel(year)),
ytitle(“Export Share in Electronics* %”) ylabel(0(10)60, angle(0))
xtitle(“”) xlabel(“”)
title(“Figure 2”, pos(6))
note(“*SITC 2-digit category 77”)
legend(label(1 “Philippines”) label(2 “Malaysia”))
;

In Figure 3, we specified marker colors and labels, draw lines connecting the dots (connect), and changed the position of marker labels (mlabpos). We also made changes on the legend(): cols(1) tells Stata to present the legend in 1 column, pos(9) moves the legend to the 9 o’clock position, and ring(0) moves the legend inside the chart.

#delimit ;
tw (scatter  exportshare year if reporter==”Philippines”,
msize(large) mcolor(dkgreen) mlabel(year) mlabpos(12) connect(l))
(scatter  exportshare year if reporter==”Malaysia”,
msize(large) mcolor(dkorange) mlabel(year) mlabpos(6) mlabcolor(black) connect(l)),
ytitle(“Export Share in Electronics* %”) ylabel(0(10)60, angle(0))
xtitle(“”) xlabel(“”)
title(“Figure 3″, pos(10) ring(0))
note(”          *SITC 2-digit category 77″)
legend(cols(1) label(1 “Philippines”) label(2 “Malaysia”) pos(9) ring(0))
;

In Figure 4, we use export share as weights.

#delimit ;
tw (scatter  exportshare year [aw=exportshare],
msymbol(Oh)mcolor(red) mlabpos(12)),
ytitle(“Export Share in Electronics* %”) ylabel(0(10)60, angle(0))
xtitle(“”) xlabel(1965(10)1995 2007)
title(“Figure 4”, pos(5) ring(0))
note(“*SITC 2-digit category 77”)
;

4 Responses

  1. I figured it out. Everything is the same as your scatter example, but the trick was how to arrange the data:
    #delim ;
    tw (bar NumberofSubjects BootstrapIndex if gCondition==1,msize(large))
    (bar NumberofSubjects BootstrapIndex if gCondition==2, msize(large)),
    ytitle(“Number of Subjects”)
    title(“Bootstrap Index for Guilty and Innocent Condititions”)
    //legend: pos(x) where x is clock position x:00, ring(0)=inside graph
    legend(cols(1) label(1 “Guilty”) label(2 “Innocent”) pos(2) ring(0))
    note(“Farwell & Donchin 1991”)
    ;
    see it here: http://screencast.com/t/zNa1dF90eA

  2. Can you do the same with bar graphs? I tried
    graph (bar ….) (bar ….)
    but it didn’t work

    • No. But you can include more than yvar in -graph bar-, e.g.

      graph bar y1 y2 y3, over(x)

      You may also stack the bars using the option stack. See -help graph bar-.

Leave a Reply