-histogram- and the -addplot- option




The beauty of Stata graphics lies in their flexibility—they can be highly customized. To illustrate, the histogram of proximity (right photo) was drawn using the command:

#delimit ;
histogram proximity, bcolor(ltblue) width(0.025) start(0) freq
addplot(histogram proximity if proximity>0.4, bcolor(yellow) width(0.025) start(0) freq ||
histogram proximity if proximity>0.55, bcolor(blue) width(0.025) start(0) freq ||
histogram proximity if proximity>0.65, bcolor(red) width(0.025) start(0) freq)
ytitle(“Number of Links”)
ylabel(0(10000)30000, format(%8.0gc) angle(0))
xlabel(0.20 0.40 0.55 0.65 0.85)
xtitle(“Proximity”)
note(“Note: The total number of links for the 779 products is (779×778)/2=303,031.”)
legend(off)
;

[Note: Since graph commands can be very long, they can be managed better by changing the delimiter to “;” ]

I know! Isn’t it easier to draw this in Microsoft Excel? If you already have a summary of the frequencies in Excel and you only need to draw it once, maybe it is easier to draw it there. But if you are to draw similar charts for 20 countries at different periods in time, Stata will make your life so much easier. Going back to our example above, this command draws 4 overlaying histograms of the same variable, proximity, in different colors.

The first line “histogram proximity, bcolor(ltblue) width(0.025) start(0) freq” draws the histogram in light blue wherein the width of each bin is equal to 0.025. By specifying start(0), we force the minimum of the range to be zero instead of the actual minimum in the data; and by specifying the option “freq”, we want to draw the frequencies not the densities or percentages. The second line uses the -addplot- option to overlay 3 more histograms in different colors. The -addplot- is an option that add plots to graphs that are not of  Stata’s -graphcommand (we will elaborate on this in future post), such as -histogram-.  If we only need to show the histogram of proximity, -addplot- here is not necessary. But, since we want to highlight different ranges of proximity values, we use -addplot- to create the different color effect.  The rest of the command are explained below:

ytitle(“Number of Links”)
/* y-axis title */

ylabel(0(10000)30000, format(%8.0gc) angle(0))
/* specifies that: (1) the y-axis will be labeled from
0 to 30000 with intervals of 10000 (2) the format of the
lables is a generic format with comma; and (3) the
orientation of the labels is horizontal */

xlabel(0.20 0.40 0.55 0.65 0.85)
/* specifies that the x-axis will be labed by the
specified numbers only */

xtitle(“Proximity”)
/* x-axis title */

note(“Note: The total number of links for the 779 products is (779×778)/2=303,031.”)
/*create a note */

legend(off)
/* supresses the legends */

All these options are general -graph- options. Once you are familiar with these most commonly used options,  you can forget Excel charts.

One Response

  1. Very helpful site – saved me much aggravation. Thanks!

Leave a Reply