Table of Contents

Description

The graphsto and graphout Stata programs streamline the creation and collection of graphs. graphsto and graphout can save multiple graphs to a single RTF, HTML, or TEX file. Graphsto saves graphs and stores their name. graphout then uses the stored names to create a file to view graphs. graphsto can also save titles and notes that graphout will add whenever the user is ready.

Note:
HTML files require graphs to be in PNG format, whereas TEX and RTF can use a number of different formats. RTF, HTML, and TEX files created by graphout are linked to the specified graphs. To embed graphs, HTML and RTF files must be converted to a PDF, and TEX files must be "typeset."

Return to Top

Background Information

Example graphout-background.do Here

Below, I start out by setting Stata's working directory to a folder on my computer. You'll need to change the information for your computer.

. cd "/Users/jdwolfe/Documents/Research/ActiveProjects/collect/work"
/Users/jdwolfe/Documents/Research/ActiveProjects/collect/work

Load data and then create a graph.

. sysuse auto
(1978 Automobile Data)

. dotplot price

Finally, I use the graphsto command to save the graph as a .png file in the "WORK" folder I specified above. See the "graph expor"t help file for a list of output formats that can be used. Notice that I use the replace option after the command. This tells Stata that if it finds another file named g1.png in my folder to overwrite it with this file.

. graphsto g1.png, replace
(file g1.png written in PNG format)

Return to Top

Examples

Basic

Example graphout-basic.do Here

Before I provide more specific examples, we’ll take a moment to show the basic functionality of graphsto and graphout.  Below, I begin by uploading data and creating a graph. I save the graph with graphsto, and then I use graphout to save an HTML file.

. sysuse auto
(1978 Automobile Data)


. histogram price, freq
(bin=8, start=3291, width=1576.875)

. graphsto g1.png, replace
(file g1.png written in PNG format)

. graphout using example.html, replace clear
(graphs added to example.html)

After you use the graphout command, click on "example.html" to take a look at the file it created. You'll notice that graphout automatically numbers the figures in the file. If you prefer that "Figure #" is not added to the file, you can specify the nocount option. When I used the graphout command above, I used the clear option. After graphout adds the stored graphs to the file, the clear option drops the stored graphs. This prevents the same graphs being added to the file when I use graphout again.

Below, I use the append option. This will add a graph to the end of an existing file. The append option is compatible with other commands such as esttab and outreg2. Graphout will copy the file as it exists and then place the specified graphs below any other information.

. graph twoway scatter price mpg

. graphsto g2.png, replace
(file g2.png written in PNG format)

. graphout using example.html, append clear
(graphs added to example.html)

Instead of the append option, I could just save two or more graphs using graphsto.

. histogram price, freq
(bin=8, start=3291, width=1576.875)

. graphsto g1.png, replace
(file g1.png written in PNG format)

. graph twoway scatter price mpg

. graphsto g2.png, replace
(file g2.png written in PNG format)

. graphout using example.html, replace clear
(graphs added to example.html)

Next, we use graphout to create a TEX file. I switch output formats to .eps. Not all formats are compatible with HTML, TEX, and RTF. You'll need to check which output format to use before creating a file, but we usually use .png for HTML and .png or .eps for TEX and RTF.

. histogram price, freq
(bin=8, start=3291, width=1576.875)

. graphsto g3.eps, replace
(file g3.eps written in EPS format)

. graphout using example.tex, replace
(graphs added to example.tex)

Next, we create an RTF file.

. histogram trunk, freq
(bin=8, start=5, width=2.25)

. graphsto g5.eps, replace
(file g5.eps written in EPS format)

. graphout using example.rtf, replace clear
(graphs added to example.rtf)

Return to Top

Size and Layout

Example graphout-size&layout.do Here

We begin by loading the data, creating graphs, and then saving them. We first adjust the size of graphs. Note, graphout can only adjust the size of graphs that are being saved to HTML or TEX files. For the sake of simplicity, we only create HTML graphs, but a simliar syntax is used to adjust the graph sizes in TEX files.

. sysuse auto
(1978 Automobile Data)

. histogram price, freq
(bin=8, start=3291, width=1576.875)

. graphsto g1.png, replace
(file g1.png written in PNG format)

. graph twoway scatter price mpg

. graphsto g2.png, replace
(file g2.png written in PNG format)

. graphout using example.html, append
(graphs added to example.html)

There are two ways to adjust the sizes of graphs. The first way is to specify the width and height. For HTML files, the width or height options must contain a nonnegative number that refers to the number of pixels. For TEX files, these options must contain a nonnegative number along with the units of measure (mm, cm, in). For example, to create a TEX file with graphs that are 5in tall by 5in wide, type height(5in) width(5in). The second way is to specify the overall scale of the graph. For both HTML and TEX files, # should be a nonnegative number between 0 and 1. For example, type scale(.5) to create a file that displays graphs that are half the size of the orginal.

. graphout using size-example.html, replace width(300) height(600)
(graphs added to size-example.html)

. graphout using size-example.html, append scale(.25)
(graphs added to size-example.html)

. graphout using size-example.html, append scale(.5)
(graphs added to size-example.html)


graphout can adjust the position of graphs for HTML, RTF, and TEX files. This is doen with the align() option. Alignment can be specified as center or c, right or r, left or l. The default is to center align graphs.

. graphout using LayoutEx.html, replace align(left)
(graphs added to LayoutEx.html)

. graphout using LayoutEx.html, append align(right)
(graphs added to LayoutEx.html)

. graphout using LayoutEx.html, append align(center)
(graphs added to LayoutEx.html)

Return to Top

Adding Text

Example graphout-text.do Here

Graphout allows users multiple ways to add information about graphs. Users can add titles to each graph or notes, which will appear below the graph. Below, we create and save two scatterplots. After saving, we using graphout to add the graphs to HTML files. We use the title() option to add the titles that will appear after each graph. So, above the first graph "Figure 1: Price Vs. MPG" will appear and "Figure 2: Price Vs. Weight" will appear over the second graph.

. sysuse auto
(1978 Automobile Data)

. graph twoway scatter price mpg

. graphsto g1.png, replace title("Price Vs. MPG")
(file g1.png written in PNG format)

Notes are added in a way similar to titles. Below, we create an HTML file with the first graph and place a note after the graph using the note() option. The file will have "Website Example" below the graph.

. graph twoway scatter price weight

. graphsto g2.png, replace title("Price Vs. Weight") note("Website Example")
(file g2.png written in PNG format)

. graphout using TitleEx.html, replace
(graphs added to TitleEx.html)

Return to Top

Advanced Examples

Loops with Graphs

Example graphout-loops.do Here

We have found that using graphout with loops provides an extremely useful way of quickly collecting and organizing graphs into a single file. This makes using plots to check data and model estimates much easier and more efficient. You can easily create 100s of graphs and instantly have them ready to review in a file. We tend to use HTML files for this type of analysis since you can use your internet browser's refresh button to update a file.

. sysuse auto
(1978 Automobile Data)

. foreach var in mpg rep78 headroom trunk weight length {
2. graph twoway scatter price `var'
3. graphsto price&`var'.png, replace title(Price Vs. `var')
4.}

graphout using Loops.html, replace

Return to Top

Getting More Help

In Stata, you can type “help graphout” to access statsto’s help files.  For more general questions and issues with Stata, you may also want to review discussions from Stata’s listserv, statalist.  See http://www.stata.com/statalist for more information about statalist.

Return to Top