Stata 15 help for twoway bar

[G-2] graph twoway bar -- Twoway bar plots

Syntax

twoway bar yvar xvar [if] [in] [, options]

options Description ------------------------------------------------------------------------- vertical vertical bar plot; the default horizontal horizontal bar plot base(#) value to drop to; default is 0 barwidth(#) width of bar in xvar units

barlook_options change look of bars

axis_choice_options associate plot with alternative axis

twoway_options titles, legends, axes, added lines and text, by, regions, name, aspect ratio, etc. ------------------------------------------------------------------------- Options base() and barwidth() are rightmost, and vertical and horizontal are unique; see repeated options.

Menu

Graphics > Twoway graph (scatter, line, etc.)

Description

twoway bar displays numeric (y,x) data as bars. twoway bar is useful for drawing bar plots of time-series data or other equally spaced data and is useful as a programming tool. For finely spaced data, also see [G-2] graph twoway spike.

Also see [G-2] graph bar for traditional bar charts and [G-2] graph twoway histogram for histograms.

Options

vertical and horizontal specify either a vertical or a horizontal bar plot. vertical is the default. If horizontal is specified, the values recorded in yvar are treated as x values, and the values recorded in xvar are treated as y values. That is, to make horizontal plots, do not switch the order of the two variables specified.

In the vertical case, bars are drawn at the specified xvar values and extend up or down from 0 according to the corresponding yvar values. If 0 is not in the range of the y axis, bars extend up or down to the x axis.

In the horizontal case, bars are drawn at the specified xvar values and extend left or right from 0 according to the corresponding yvar values. If 0 is not in the range of the x axis, bars extend left or right to the y axis.

base(#) specifies the value from which the bar should extend. The default is base(0), and in the above description of options vertical and horizontal, this default was assumed.

barwidth(#) specifies the width of the bar in xvar units. The default is width(1). When a bar is plotted, it is centered at x, so half the width extends below x and half above.

barlook_options set the look of the bars. The most important of these options is color(colorstyle), which specifies the color and opacity of the bars; see [G-4] colorstyle for a list of color choices. See [G-3] barlook_options for information on the other barlook_options.

axis_choice_options associate the plot with a particular y or x axis on the graph; see [G-3] axis_choice_options.

twoway_options are a set of common options supported by all twoway graphs. These options allow you to title graphs, name graphs, control axes and legends, add lines and text, set aspect ratios, create graphs over by() groups, and change some advanced settings. See [G-3] twoway_options.

Remarks

Remarks are presented under the following headings:

Typical use Advanced use: Overlaying Advanced use: Population pyramid Cautions

Typical use

We have daily data recording the values for the S&P 500 in 2001:

. sysuse sp500

. list date close change in 1/5 +---------------------------------+ | date close change | |---------------------------------| 1. | 02jan2001 1283.27 . | 2. | 03jan2001 1347.56 64.29004 | 3. | 04jan2001 1333.34 -14.22009 | 4. | 05jan2001 1298.35 -34.98999 | 5. | 08jan2001 1295.86 -2.48999 | +---------------------------------+

We will use the first 57 observations from these data:

. twoway bar change date in 1/57 (click to run)

We get a different visual effect if we reduce the width of the bars from 1 day to .6 days:

. twoway bar change date in 1/57, barw(.6) (click to run)

Advanced use: Overlaying

The useful thing about twoway bar is that it can be combined with other twoway plottypes:

. twoway line close date || bar change date || in 1/52 (click to run)

We can improve this graph by typing

. twoway line close date, yaxis(1) || bar change date, yaxis(2) || in 1/52, ysca(axis(1) r(1000 1400)) ylab(1200(50)1400, axis(1)) ysca(axis(2) r(-50 300)) ylab(-50 0 50, axis(2)) ytick(-50(25)50, axis(2) grid) legend(off) xtitle("Date") title("S&P 500") subtitle("January - March 2001") note("Source: Yahoo!Finance and Commodity Systems, Inc.") yline(1150, axis(1) lstyle(foreground)) (click to run)

Notice the use of

yline(1150, axis(1) lstyle(foreground))

The 1150 put the horizontal line at y=1150; axis(1) stated that y should be interpreted according to the left y axis; and lstyle(foreground) specified that the line be drawn in the foreground style.

Advanced use: Population pyramid

We have the following aggregate data from the U.S. 2000 Census recording total population by age and sex. From this, we produce a population pyramid:

. sysuse pop2000, clear . list agegrp maletotal femtotal +------------------------------------+ | agegrp maletotal femtotal | |------------------------------------| 1. | Under 5 9,810,733 9,365,065 | 2. | 5 to 9 10,523,277 10,026,228 | 3. | 10 to 14 10,520,197 10,007,875 | 4. | 15 to 19 10,391,004 9,828,886 | 5. | 20 to 24 9,687,814 9,276,187 | |------------------------------------| 6. | 25 to 29 9,798,760 9,582,576 | 7. | 30 to 34 10,321,769 10,188,619 | 8. | 35 to 39 11,318,696 11,387,968 | 9. | 40 to 44 11,129,102 11,312,761 | 10. | 45 to 49 9,889,506 10,202,898 | |------------------------------------| 11. | 50 to 54 8,607,724 8,977,824 | 12. | 55 to 59 6,508,729 6,960,508 | 13. | 60 to 64 5,136,627 5,668,820 | 14. | 65 to 69 4,400,362 5,133,183 | 15. | 70 to 74 3,902,912 4,954,529 | |------------------------------------| 16. | 75 to 79 3,044,456 4,371,357 | 17. | 80 to 84 1,834,897 3,110,470 | +------------------------------------+

. replace maletotal = -maletotal/1e+6

. replace femtotal = femtotal/1e+6

. twoway bar maletotal agegrp, horizontal xvarlab(Males) || bar femtotal agegrp, horizontal xvarlab(Females) || , ylabel(1(1)17, angle(horizontal) valuelabel labsize(*.8)) xtitle("Population in millions") ytitle("") xlabel(-10 "10" -7.5 "7.5" -5 "5" -2.5 "2.5" 2.5 5 7.5 10) legend(label(1 Males) label(2 Females)) title("US Male and Female Population by Age") subtitle("Year 2000") note("Source: U.S. Census Bureau, Census 2000, Tables 1, 2 and 3", span) (click to run)

At its heart, the above graph is simple: we turned the bars sideways and changed the male total to be negative. Our first attempt at the above was simply

. sysuse pop2000, clear

. replace maletotal = -maletotal

. twoway bar maletotal agegrp, horizontal || bar femtotal agegrp, horizontal (click to run)

From there, we divided the population totals by 1 million and added options.

xlabel(-10 "10" -7.5 "7.5" -5 "5" -2.5 "2.5" 2.5 5 7.5 10) was a clever way to disguise that the bars for males extended in the negative direction. We said to label the values -10, -7.5, -5, -2.5, 2.5, 5, 7.5, and 10, but then we substituted text for the negative numbers to make it appear that they were positive. See [G-3] axis_label_options.

Using the span suboption to note() aligned the text on the left side of the graph rather than on the plot region. See [G-3] textbox_options.

For another rendition of the pyramid, we tried

. sysuse pop2000, clear

. replace maletotal = -maletotal/1e+6

. replace femtotal = femtotal/1e+6

. gen zero = 0

. twoway bar maletotal agegrp, horizontal xvarlab(Males) || bar femtotal agegrp, horizontal xvarlab(Females) || sc agegrp zero , mlabel(agegrp) mlabcolor(black) msymbol(i) || , xtitle("Population in millions") ytitle("") plotregion(style(none)) (note 1) ysca(noline) ylabel(none) (note 2) xsca(noline titlegap(-3.5)) (note 3) xlabel(-12 "12" -10 "10" -8 "8" -6 "6" -4 "4" 4(2)12, tlength(0) grid gmin gmax) legend(label(1 Males) label(2 Females)) legend(order(1 2)) title("US Male and Female Population by Age, 2000") note("Source: U.S. Census Bureau, Census 2000, Tables 1, 2 and 3") (click to run)

In the above rendition, we moved the labels from the x axis to inside the bars by overlaying a scatter on top of the bars. The points of the scatter we plotted at y=agegrp and x=0, and rather than showing the markers, we displayed marker labels containing the desired labelings. See [G-3] marker_label_options.

We also played the following tricks:

1. plotregion(style(none)) suppressed outlining the plot region; see [G-3] region_options.

2. ysca(noline) suppressed drawing the y axis -- see [G-3] axis_scale_options -- and ylabel(none) suppressed labeling it -- see [G-3] axis_label_options.

3. xsca(noline titlegap(-3.5)) suppressed drawing the x axis and moved the x-axis title up to be in between its labels; see [G-3] axis_scale_options.

Cautions

You must extend the scale of the axis, if that is necessary. Consider using twoway bar to produce a histogram (ignoring the better alternative of using twoway histogram). Assume that you have already aggregated data of the form

x frequency --------------- 1 400 2 800 3 3,000 4 1,800 5 1,100 ---------------

which you enter into Stata to make variables x and frequency. You type

. twoway bar frequency x

to make a histogram-style bar chart. The y axis will be scaled to go between 400 and 3,000 (labeled at 500, 1,000, ..., 3,000), and the shortest bar will have zero height. You need to type

. twoway bar frequency x, ysca(r(0))


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index