COMET
  • Get Started
    • Quickstart Guide
    • Install and Use COMET
    • Get Started
  • Learn By Skill Level
    • Getting Started
    • Beginner
    • Intermediate - Econometrics
    • Intermediate - Geospatial
    • Advanced

    • Browse All
  • Learn By Class
    • Making Sense of Economic Data (ECON 226/227)
    • Econometrics I (ECON 325)
    • Econometrics II (ECON 326)
    • Statistics in Geography (GEOG 374)
  • Learn to Research
    • Learn How to Do a Project
  • Teach With COMET
    • Learn how to teach with Jupyter and COMET
    • Using COMET in the Classroom
    • See COMET presentations
  • Contribute
    • Install for Development
    • Write Self Tests
  • Launch COMET
    • Launch on JupyterOpen (with Data)
    • Launch on JupyterOpen (lite)
    • Launch on Syzygy
    • Launch on Colab
    • Launch Locally

    • Project Datasets
    • Github Repository
  • |
  • About
    • COMET Team
    • Copyright Information
  1. STATA Notebooks
  2. Combining Graphs (10)
  • Learn to Research


  • STATA Notebooks
    • Setting Up (1)
    • Working with Do-files (2)
    • STATA Essentials (3)
    • Locals and Globals (4)
    • Opening Datasets (5)
    • Creating Variables (6)
    • Within Group Analysis (7)
    • Combining Datasets (8)
    • Creating Meaningful Visuals (9)
    • Combining Graphs (10)
    • Conducting Regression Analysis (11)
    • Exporting Regression Output (12)
    • Dummy Variables and Interactions (13)
    • Good Regression Practices (14)
    • Panel Data Regression (15)
    • Difference in Differences (16)
    • Instrumental Variable Analysis (17)
    • STATA Workflow Guide (18)

  • R Notebooks
    • Setting Up (1)
    • Working with R Scripts (2)
    • R Essentials (3)
    • Opening Datasets (4)
    • Creating Variables (5)
    • Within Group Analysis (6)
    • Combining Datasets (7)
    • Creating Meaningful Visuals (8)
    • Combining Graphs (9)
    • Conducting Regression Analysis (10)
    • Exporting Regression Output (11)
    • Dummy Variables and Interactions (12)
    • Good Regression Practices (13)
    • Panel Data Regression (14)
    • Difference in Differences (15)
    • Instrumental Variable Analysis (16)
    • R Workflow Guide (17)

  • Pystata Notebooks
    • Setting Up (1)
    • Working with Do-files (2)
    • STATA Essentials (3)
    • Locals and Globals (4)
    • Opening Datasets (5)
    • Creating Variables (6)
    • Within Group Analysis (7)
    • Combining Datasets (8)
    • Creating Meaningful Visuals (9)
    • Combining Graphs (10)
    • Conducting Regression Analysis (11)
    • Exporting Regression Output (12)
    • Dummy Variables and Interactions (13)
    • Good Regression Practices (14)
    • Panel Data Regression (15)
    • Difference in Differences (16)
    • Instrumental Variable Analysis (17)
    • STATA Workflow Guide (18)

On this page

  • Prerequisites
  • Learning Outcomes
  • 10.0 Intro
  • 10.1 Example 1
  • 10.2 Example 2
  • 10.3 Wrap Up
  • 10.4 Wrap-up Table
  • References
  • Report an issue

Other Formats

  • Jupyter
  1. STATA Notebooks
  2. Combining Graphs (10)

10 - Combining Graphs

econ 490
stata
visualization
combining graphs
twoway
generate
This notebook explains how to combine graphs.
Author

Marina Adshade, Paul Corcuera, Giulia Lo Forte, Jane Platt

Published

29 May 2024

Prerequisites

  1. Be able to effectively use Stata do-files and generate log-files.
  2. Be able to change your directory so that Stata can find your files.
  3. Import datasets in .csv and .dta format.
  4. Save data files.
  5. Use the command twoway.

Learning Outcomes

  1. Know how to combine and save graphs using the commands graph combine and graph export.

10.0 Intro

Note: The best approach to completing this module is to copy and paste these commands into a do-file in Stata. Because Stata produces graphs in a separate window, Jupyter Notebooks will not produce a graph that we can see when we execute the commands on this page. The most we can do is export image files to a directory on our computer. We will see these commands whenever a graph is produced below.

We’ll continue working with the fake data set we have been using as we work on developing our research skills. Recall that this data set is simulating information for workers in the years 1982-2012 in a fake country where a training program was introduced in 2003 to boost their earnings.

clear*
*cd ""
use fake_data, clear 

In this module, we will we will work on two examples. The first example covers combining two graphs with the same schema, while the second covers combining two graphs with different schemas. It will soon be very clear what we mean by schema.

10.1 Example 1

For this example, we want to generate two graphs with the same schema (they are the same type of graph and use the same variables as their x and y axis) and combine them using the graph combine command. Let’s begin by setting up the data. We are going to first generate a new variable that shows the logarithm of workers’ earnings. As explained in previous modules, collapsing data is irreversible; therefore, we can preserve the data set before we collapse it. Then, once we don’t want to use the collapsed version of the data set anymore, we can restore it back to the original data set we preserved. Therefore, we are going to preserve our data set and then collapse it by variables treated and year. This way our data has two unique identifiers, treated and year.

generate log_earnings = log(earnings)
label var log_earnings "Log-earnings"

preserve

collapse (mean) log_earnings, by(region treated year)

Now that we have our data prepared, we can start generating the two graphs and combining them using the graph combine command. We want these graphs to compare log-earnings between the control and treated groups in regions 1 and 2. To do this, we can create one graph that compares log-earnings between control and treated groups in region 1 and another that does the same comparison for region 2.

Note that because the commands are so long it is clearer for us to break them up and run them using ///. We can’t execute multi-line commands in the Command Window; so we will need to include them in a do-file. Note that we also indent the lines to make it easier to read the complete command.

*** Generate graph for Region 1 ***

twoway (connected log_earnings year if region==?? & treated==1)      /// can you think of another way to specify treated vs untreated?
    (connected log_earnings year if region==?? & treated==0),        ///
        xline(2002, lpattern(dash))                                 /// 
        ylab(9.5(0.5)11)                                            ///
        ytitle("Log-earnings") xtitle("Year")                       ///
        legend( label(1 "Treated") label(2 "Control"))              ///
        aspectratio(1)                                              ///
        title("Region 1") name("R1", replace)
*** Generate graph for Region 2 ***

twoway (connected log_earnings year if region==?? & treated) ||      ///
    (connected log_earnings year if region==?? & !treated),          ///
        xline(2002, lpattern(dash))                                 ///
        ylab(9.5(0.5)11)                                            ///
        ytitle("Log-earnings") xtitle("Year")                       ///
        legend( label(1 "Treated") label(2 "Control"))              ///
        aspectratio(1)                                              ///
        title("Region 2") name("R2", replace)

We added a number of options here to make the graphs look appealing. You can learn more about these options in Module 9.

Now let’s see what it looks like when we combine these graphs together into one panel. The syntax for graph combine goes as follows: graph combine name_of_graph1 name_of_graph2 ..., [options], where the [options] describe how we want to position our graphs. More information can be found by running help graph combine in the Stata Command Cindow.

*** Combine graphs ***

graph combine R1 R2, cols(2) title("Panel A: Log-earnings by Region") saving(panel_a, replace)

graph export graph10.jpg, as(jpg) replace

Panel A

10.2 Example 2

For this example, we want to combine graphs that do not follow the same schema. Let’s say we are interested in seeing if there is any relationship between the distribution of earnings (log_earnings) and how worker’s earnings change over time in region 1. Which graphs do you think would best present this information?

Like we saw last module, we usually use histograms to present density distributions and we can use a scatter plot or a line plot for the graph of log_earnings over time. We will begin by generating a histogram of log_earnings in region 1.

restore       // do you remember what restore does? 
histogram log_earnings if region==1,   ///
    aspectratio(1)                     ///
    name("histogram1", replace)

Let’s create our second graph.

preserve              

collapse (mean) log_earnings, by(region year)
twoway (connected log_earnings year if region==1), ///
    ytitle("Log-earnings") xtitle("Year")        ///
    aspectratio(1)                               ///
    name("plot1", replace)

Now we combine histogram1 with plot1.

graph combine histogram1 plot1, cols(2) title("Region 1") name(newcombine, replace)

graph export graph10.jpg, as(jpg) replace

10.3 Wrap Up

In this module we learned how to use the command graph combine. When producing a research paper we might want to compare statistics from different countries or different regions such as GDP, population density, inflation, exports, etc. These types of graphs allow us to see how the same variables diverge between different categories (for example how earnings diverge between region 1 and 2 in ex. 1) and also can show the relationship between different variables throughout one. Understanding which graphs to use and how to portray them is of extreme importance when building a research project, which is why working alongside the twoway and graph combine documentation is always of great value.

10.4 Wrap-up Table

Command Function
graph combine It combines two graphs with the same or different schemas.
graph export It exports graphs to a local folder.

References

Getting started in stata (includes graphing)
(Non StataCorp) Combining graphs in Stata

Creating Meaningful Visuals (9)
Conducting Regression Analysis (11)
  • Creative Commons License. See details.
 
  • Report an issue
  • The COMET Project and the UBC Vancouver School of Economics are located on the traditional, ancestral and unceded territory of the xʷməθkʷəy̓əm (Musqueam) and Sḵwx̱wú7mesh (Squamish) peoples.