Skip to content
Stata.Professor : Your Partner in Research Logo Stata.Professor : Your Partner in Research Logo

  • Home
  • Paid Help
    • Empirical Models using Stata®
    • Data Management
    • Paid Help | Pricing
    • Paid Help – Frequently Asked Questions (FAQs)
    • Privacy
  • Completed Projects
    • Asset Pricing Models
      • Fama and French Model
      • Fama – MacBeth
      • Bootstrap Fund Performance
      • Investors attention
      • Misvaluing Innovation
    • Implied cost of equity (ICC)
      • Gebhardt et al. (2001) – GLS
      • ICC GLS Model
      • Claus and Thomas (2001)
      • Cost of Equity using CAPM & FF3
      • Mutual Funds performance
    • Event studies
      • Event study methodology
      • Returns to New IPOs
      • Share repurchases
    • Momentum & Trading
      • asm Momentum Portfolios
      • Absolute Strength Momentum
      • Non-parametric momentum
      • Trading rules
      • Herding behavior
      • Sentiment, disagreement
    • KMV – Default Probability
    • Earning Management Model
    • Market Liquidity
      • Market liquidity models
      • Bid Ask Spread
    • Volatility
      • Backtesting VaR Models
      • P42 – Volatility portfolios
      • P31 – Realized volatility
      • Volatility Managed Portfolios
      • Expected Idiosyncratic Skewness
    • CEO debt and R&D
    • Zero-leverage firms
    • Financial Statement Comparability
  • Stata Programs
    • ASDOC: Stata to Word
      • asdoc : examples
      • asdoc : Tables of summary statistics
    • ASROL: Rolling window stats
    • ASREG: rolling regressions
    • ASTILE: for Portfolios and Groups
    • ASCOL: Converts Data Frequency
    • ASM: Momentum Portfolios
    • Stata Random Tips
    • Download Statistics
  • BLOG
  • ForumsbbPress Forums
  • About Us
  • Home
  • Paid Help
    • Empirical Models using Stata®
    • Data Management
    • Paid Help | Pricing
    • Paid Help – Frequently Asked Questions (FAQs)
    • Privacy
  • Completed Projects
    • Asset Pricing Models
      • Fama and French Model
      • Fama – MacBeth
      • Bootstrap Fund Performance
      • Investors attention
      • Misvaluing Innovation
    • Implied cost of equity (ICC)
      • Gebhardt et al. (2001) – GLS
      • ICC GLS Model
      • Claus and Thomas (2001)
      • Cost of Equity using CAPM & FF3
      • Mutual Funds performance
    • Event studies
      • Event study methodology
      • Returns to New IPOs
      • Share repurchases
    • Momentum & Trading
      • asm Momentum Portfolios
      • Absolute Strength Momentum
      • Non-parametric momentum
      • Trading rules
      • Herding behavior
      • Sentiment, disagreement
    • KMV – Default Probability
    • Earning Management Model
    • Market Liquidity
      • Market liquidity models
      • Bid Ask Spread
    • Volatility
      • Backtesting VaR Models
      • P42 – Volatility portfolios
      • P31 – Realized volatility
      • Volatility Managed Portfolios
      • Expected Idiosyncratic Skewness
    • CEO debt and R&D
    • Zero-leverage firms
    • Financial Statement Comparability
  • Stata Programs
    • ASDOC: Stata to Word
      • asdoc : examples
      • asdoc : Tables of summary statistics
    • ASROL: Rolling window stats
    • ASREG: rolling regressions
    • ASTILE: for Portfolios and Groups
    • ASCOL: Converts Data Frequency
    • ASM: Momentum Portfolios
    • Stata Random Tips
    • Download Statistics
  • BLOG
  • ForumsbbPress Forums
  • About Us
  • Home
  • Paid Help
    • Empirical Models using Stata®
    • Data Management
    • Paid Help | Pricing
    • Paid Help – Frequently Asked Questions (FAQs)
    • Privacy
  • Completed Projects
    • Asset Pricing Models
      • Fama and French Model
      • Fama – MacBeth
      • Bootstrap Fund Performance
      • Investors attention
      • Misvaluing Innovation
    • Implied cost of equity (ICC)
      • Gebhardt et al. (2001) – GLS
      • ICC GLS Model
      • Claus and Thomas (2001)
      • Cost of Equity using CAPM & FF3
      • Mutual Funds performance
    • Event studies
      • Event study methodology
      • Returns to New IPOs
      • Share repurchases
    • Momentum & Trading
      • asm Momentum Portfolios
      • Absolute Strength Momentum
      • Non-parametric momentum
      • Trading rules
      • Herding behavior
      • Sentiment, disagreement
    • KMV – Default Probability
    • Earning Management Model
    • Market Liquidity
      • Market liquidity models
      • Bid Ask Spread
    • Volatility
      • Backtesting VaR Models
      • P42 – Volatility portfolios
      • P31 – Realized volatility
      • Volatility Managed Portfolios
      • Expected Idiosyncratic Skewness
    • CEO debt and R&D
    • Zero-leverage firms
    • Financial Statement Comparability
  • Stata Programs
    • ASDOC: Stata to Word
      • asdoc : examples
      • asdoc : Tables of summary statistics
    • ASROL: Rolling window stats
    • ASREG: rolling regressions
    • ASTILE: for Portfolios and Groups
    • ASCOL: Converts Data Frequency
    • ASM: Momentum Portfolios
    • Stata Random Tips
    • Download Statistics
  • BLOG
  • ForumsbbPress Forums
  • About Us

A Comprehensive Guide to Bar Charts in Stata

A Comprehensive Guide to Bar Charts in StataAttaullah Shah2025-02-25T09:44:23+05:00

Table of contents

  • 1. What is a Bar Chart
    • Simple Bar Chart Syntax
    • Basic Usage of Bar Chart
    • Bar Chart Options

1. What is graph bar chart?

In Stata, we use the graph bar command to create vertical bar charts that effectively summarize numerical data across categorical groups. The graph bar command:

  • Displays statistical measures (e.g., mean, sum, count) for selected variables.
  • Supports flexible grouping with the over() option.
  • Offers extensive customization for bar appearance, including colors, gaps, and label orientation.
  • Allows data filtering using conditional statements (e.g., if clause).
  • Integrates with additional options for axes, legends, and overall graph aesthetics.

The basic syntax for creating a line chart in Stata is:

[code]

*Syntax of the line command in Stata

graph bar yvars [if] [in] [weight] [, options][/code]

where, yvars represents the variables to be summarized, asis, percent or count, or is a statistic. Options like over() help group the data for more visual comparisons.
⠀

Basic examples

Simple count bar graph

[code]*First, load the dataset
sysuse nlsw88.dta, clear

* bar graph for counts
graph bar (count)[/code] Stata Graph - Graph 0 500 1,000 1,500 2,000 2,500 frequency

Adding an “over” option

[code]graph bar (count), over(race)[/code] Stata Graph - Graph 0 500 1,000 1,500 2,000 frequency White Black Other

Displaying totals

[code]graph bar (count), over(race) blabel(total)[/code] Stata Graph - Graph 1637 583 26 0 500 1,000 1,500 2,000 frequency White Black Other

Showing percentages

[code]graph bar (percent), over(race) ylabel(0(20)100)[/code] Stata Graph - Graph 0 20 40 60 80 100 percent White Black Other
  • group_options– control the appearance and connection of lines.
  • yvar_options – assign plots to specific axes.
  • lookofbar_options – for titles, legends, axes, and overall graph appearance.
  • legending_options – how yvars are labeled
  • axis_options – how the numerical y axis is labeled
  • title_and_other_options – titles, added text, aspect ratio, etc.

Let’s explore its usage with examples.

2. Horizontal bar graphs

In a horizontal bar graph, the bars are displayed horizontally, with the categories on the vertical axis and the values on the horizontal axis. The horizontal bars are usually used when you have many categories or long category labels, as it makes them easier to read.

[code]graph hbar (percent), over(occupation)[/code] Stata Graph - Graph 0 10 20 30 percent Other Household workers Service Farm laborers Farmers Laborers Transport Operatives Craftsmen Clerical/Unskilled Sales Managers/Admin Professional/Technical

Sorting and displaying all categories

[code]graph hbar (percent), over(occupation, sort(1) descending) missing allcategories[/code] Stata Graph - Graph 0 10 20 30 percent Farmers Household workers Farm laborers . Service Transport Craftsmen Clerical/Unskilled Other Operatives Managers/Admin Laborers Professional/Technical Sales

Graphs with Two or More “over” Variables

[code]graph export “C:Usersimsc.80166PicturesPicture1.svg”, as(svg) name(“Graph”) replace[/code] Stata Graph - Graph 0 100 200 300 400 500 frequency Union Nonunion Other Household workers Service Farm laborers Farmers Laborers Transport Operatives Craftsmen Clerical/Unskilled Sales Managers/Admin Professional/Technical Other Household workers Service Farm laborers Farmers Laborers Transport Operatives Craftsmen Clerical/Unskilled Sales Managers/Admin Professional/Technical

Percentages with two “over” variables

Stata Graph - Graph 0 20 40 60 80 100 percent of percent Other Household workers Service Farm laborers Farmers Laborers Transport Operatives Craftsmen Clerical/Unskilled Sales Managers/Admin Professional/Technical Nonunion Union

Two variables side by side over third

Lets’ create graph that shows two variables side by side, over third one.
[code] * Load auto dataset
sysuse auto.dta, clear
‏‏‎ ‎
* sort by two variables
sort foreign weight
‏‏‎ ‎
* Create graph
graph bar mpg trunk if foreign==1, over(weight, lab(angle(45)))

[/code] Stata Graph - Graph 0 10 20 30 40 1760 1830 1930 1980 1990 2020 2040 2050 2070 2130 2160 2200 2240 2280 2370 2410 2650 2670 2750 2830 3170 3420 mean of mpg mean of trunk
  • (line invest year if firm == 1, sort lcolor(blue)):
    Plots a blue line for Firm 1’s investment over time. The if firm == 1 condition restricts the plot to Firm 1. The sort option ensures that the data points are connected in order of the year variable.
  • line invest year if firm == 2, sort lcolor(red)):
    Plots a red line for Firm 2’s investment over time.
  • title("Investment over Time for Firms 1 and 2") adds a main title.
  • xtitle("Year") and ytitle("Investment") label the axes.
  • The legend() option provides labels for the two lines.

twoway vs line

The line command is a shorthand for the more general twoway line command. For basic line plots, both commands function identically, producing the same graphical output. The distinction arises in more complex scenarios. twoway syntax allows for the combination of multiple plot types within a single graph. For example, you can overlay a line plot with a scatter plot or a linear fit (lfit) using the twoway framework. In simpler cases, choosing between line and twoway line for a single line plot is purely a matter of preference as they are functionally equivalent.

* Both commands produce the same basic line chart
line yvar xvar
twoway line yvar xvar

* twoway syntax allows combining plot types
twoway (line yvar xvar) (scatter yvar xvar) (lfit yvar xvar)

Combine line with bar chart

Overlay graphs are typically used to display two variables on a single graph, allowing for a direct comparison between absolute and relative values. For instance, one variable may be represented by a bar chart—which effectively shows counts or proportions—while the other is illustrated with a line graph that highlights trends or changes over a continuous scale.

[code] clear*
input str40 Sector Coverage Ratio
“Agriculture; ” .85813358 0.52
“Mining ” .89187858 0.13
“Manufacturing ” .36191116 0.29
“Electricity; ” .68654997 0.18
“Construction ” .13923316 0.36
“Wholesale ” .62995644 0.35
“Transport; ” .34724069 0.27
“Financial Services ” .75544079 0.22
“CSP ” .90706484 0.31
“Private Households ” .9931992 0.80
end

g Sector1=_n
labmask Sector1, val(Sector) // ssc install labmask

twoway bar Coverage Sector1, ylab(0(.2)1, ///
notick) barwidth(.7) xtitle(“”) ytitle(“”) ///
xla(1/10, valuelabel notick ang(90)) || ///

line Ratio Sector1, sort[/code] Stata Graph - Graph 0 .2 .4 .6 .8 1 Agriculture;  Mining   Manufacturing  Electricity;   Construction  Wholesale   Transport;   Financial Services  CSP  Private Households  Coverage Ratio

Paid Help Menu

  • What is Paid Help
  • Paid Help | Pricing
  • Paid Help – FAQs
  • List of Completed Projects
  • Details of All Projects
  • Our Stata Programs
  • Contact

Key Links

  • Paid Help
  • Data Management
  • Empirical Models using Stata®
  • Paid Help – FAQs
  • Paid Help | Pricing
  • Privacy

Recent Projects

  • Conditional Beta using MGARCH Approach
  • Returns to New IPOs
  • Trading frequency | Price Impact
  • Measuring Financial Statement Comparability
  • Expected Idiosyncratic Skewness and Stock Returns
  • Absolute Strength Momentum

Contact Info

Phone: +92 3459146115

Mobile: +92 3459146115

Email: attaullah.shah@imsciences.edu.pk

Copyright 2012 - 2020 Attaullah Shah | All Rights Reserved
FacebookXInstagramPinterest
Page load link
Go to Top