Skip to content
Module 06 of 1260 min readIntermediate

Graphing in Stata

twoway line, scatter, bar, histogram, kdensity, marginsplot. Publication-quality charts from the command line.

50%

Listen along

Read “Graphing in Stata” aloud

Plays in your browser using on-device text-to-speech — nothing leaves the page.

Learning objectives

By the end of this module, you should be able to:

  • 01Build line, scatter, bar, histogram, and kdensity plots with twoway and graph commands
  • 02Add titles, labels, legends, and notes using twoway options
  • 03Combine plots with twoway (g1) (g2) and graph combine for multi-panel figures
  • 04Export publication-quality figures to PNG, PDF, and Stata's editable .gph format

Stata graphs are command-driven, parametric, and publication-quality with a small amount of work. The base command is twoway; line, scatter, bar, hist are the workhorses.

twoway line and scatter

stata
twoway line lending_rate month
twoway scatter lending_rate deposit_rate
* Multiple series
twoway (line lending_rate month) (line deposit_rate month)

Adding labels and titles

stata
twoway (line lending_rate month) (line deposit_rate month), ///
title("Kenyan commercial bank rates") ///
ytitle("Rate (%)") xtitle("Month") ///
legend(label(1 "Lending") label(2 "Deposit"))

histogram and kdensity

stata
histogram lending_rate, frequency
histogram lending_rate, normal // overlay normal density
kdensity lending_rate

bar and graph bar

stata
graph bar (mean) lending_rate, over(year)
graph bar (mean) lending_rate (mean) deposit_rate, over(year)

Saving graphs

stata
graph export figure1.png, replace
graph export figure1.pdf, replace
graph save figure1.gph, replace // Stata's native format, can re-edit later

marginsplot — visualising regression effects

stata
regress lending_rate deposit_rate i.year
margins year
marginsplot, recast(line) // shows year fixed effects

Triple-slash is the line-continuation

Stata commands are one line by default. To split across lines for readability, use /// at the end of each line. Easy to forget; very useful.

Exercise

Plot a line graph of lending_rate against month.

Key takeaways

  • twoway is the foundation: twoway line, twoway scatter, twoway connected, etc.
  • Use /// for line continuation in long commands — readability matters in shared do-files
  • graph export figure.png handles output; graph save preserves an editable .gph file
  • marginsplot is the bridge from regression to publication-ready coefficient visualisation

Further reading

  1. 01

    A Visual Guide to Stata Graphics (4th Edition)

    Michael N. Mitchell · Stata Press · 2020

  2. 02
  3. 03

    An Introduction to Modern Econometrics Using Stata

    Christopher F. Baum · Stata Press · 2006

Loading progress…
LeadAfrikPublic Economics Hub