Oct 25, 2011

Using Inkscape to improve Stata graphics

Stata produces graphics that can be hard to work with and are often ugly. Often times, I have trouble with labels running into one another or blocky looking elements. Moreover, I sometimes need to change a title, add a caption, or change a color and reproducing the graph in Stata to do this is both time consuming and annoying. Luckily, I have found an easy way to edit graphs in Stata, improve there look, and tweak them if need be. You use a program called Inkscape.


To illustrate, take a look at the following the following graph produced using the following code:
  1. 1
  2. 2
use http://www.stata-press.com/data/r11/uslifeexp2
twoway (scatter le year), scheme(s1color)

Scatterplot of Life expectancy vs. Year produced in Stata - looks ok.
Normally, in Stata if you want to adjust objects within your graph you can do so using one of the graphics dialogues or the graphics editor but you are limited in what you can do. You cannot do things like add gradients, use special fonts, add a photo, or sharpen the look of markers.

Certainly, this is a functional graph but I like to make things look nicer and maybe make a few small changes. The answer, I've found, is to use inkscape.

Tutorial (Windows)
The following will get you started in inkscape to improve the look of graphs and plots produced in Stata. You should be able to move, scale, and change the color of objects in inkscape inkscape intuitively. If your looking to do more advanced stuff, consult the Inkscape documentation.

[1] If you haven't already, download and install inkscape from their website.

[2] Begin with your graph in the graph window, and export it as an Enhanced Metafile (.emf) file (select File > Save As > select Enhanced Metafile (*.emf))

[3] Now open Inkscape and load up your emf file. The first thing you'll notice is that things seem to look better. Markers and text are sharper and less blocky and pixelated.

The above shows what graph markers look like in Stata
 and in png exports. The bottom shows what they look like
as emf exports in inkscape.

The reason this is the case is because emf files can contain, and in this case do contain vector graphics as opposed to raster graphics. Vector graphics have many advantages. Graphics are sharper and more accurate, and they don't degrade when there size is increased. If you frequently produce posters they can be exported from inkscape at enormous sizes so that they won't look fuzzy.

Once your file is in Inkscape all of the individual elements (down to each individual marker) can be edited. You can change the color, move them around, adjust labels (and prevent them from running into one another), add a background gradient, or even add a photo of your family in the background if you wanted!
Here is what your window should look like in Inkscape.
[4] In order to begin editing individual elements, you have to ungroup your graph. While the entire graph is selected click Object > Ungroup. You should now have something that looks like this:


All objects un-grouped.

As you can see, all of the individual elements are now selected. You should feel at home moving objects around, changing text, and scaling objects if you have used Microsoft Powerpoint. If you want to go further and change colors click this button:
 Fill and Stroke

This will toggle a panel allowing you to change the color and outline of objects in your graph.
  
Example
Final Version
The following adjustments were made to the graph shown above which was originally produced in Stata, and edited later in Inkscape.
  • Title Added
  • Background gradient added
  • Axis titles capitalized and moved slightly.
  • Color of markers changed from green to blue and decreased in size.
  • Outlier marker removed.