6 Including Stata Graphs

Including Stata graphics in your Statamarkdown document is fairly simple, but it is not automatic.

There are fundamentally two approaches you can take to include a Stata graph in your document. For both, you must first issue the Stata commands to create and save your graph.

sysuse auto, clear

6.1 Saving a Stata Graph

In Stata you might use two commands to create and save your graph. Use the echo=1 chunk option to show your reader only the scatter command.

```{stata scatterplot, echo=1, results="hide"}
scatter mpg weight
quietly graph export scatter.svg, replace

Your options for graphics file formats depend upon your operating system, and the version of Stata you are using. Common formats for HTML documents are png, jpg, and svg.

scatter mpg weight

6.2 Insert via Markdown

In Markdown, you can link to separate image files from within your document. Keep in mind that when you publish and HTML document with a linked graph file, you need to publish both files.

The Markdown specification allows you to insert a graphics file in the following manner

![Mileage ~ Weight](scatter.svg)

Mileage ~ Weight

6.3 Insert SVG via R

SVG is a graphics format that saves information as text in a form that many programs, including web browsers recognize and render appropriately. For files of this type, the SVG code can be included directly in an HTML document.

The following code chunk reads the contents of an SVG file into a Markdown document. This can be used to include graphics within you main HTML file, simplifying file management later.

The R code here also deletes the graphics file after it has been used in your document.

```{r graphincl, results="asis"}
```{r cleanup, include=FALSE}

Notice that this method does not give you a figure caption, nor does it center your graph on the page.

Stata Graph - Graph 10 20 30 40 Mileage (mpg) 2,000 3,000 4,000 5,000 Weight (lbs.)

6.4 Stata Graph Options

Neither method of including a Stata graph gives you control over graph size or graph aesthetics. These are all elements that must be specified in Stata, rather than R or Statamarkdown.

(This page was written using Statamarkdown version 0.7.1.)