Transparency in Stata graphs

Updated: Feb 22, 2021

In this post, I'm going to show you how to adjust the transparency of your Stata graph. By default, the elements in Stata graph is not transparent, or 100% opaque. However, sometimes you might want to see what lies underneath, and that usually produces better-looking graphs. As a new feature in Stata 15, it allows you to specify the percentage opacity. To demonstrate, I use the automobile dataset.

First, let's start from the simple histogram. In the command pane (you can also use the menu to achieve the same result: Graphics > Histogram), I type:

histogram price, frequency

Stata will produce a basic histogram, with default color and 100% opacity. opacity is the percentage of a color that covers the background color. 100% means that the color fully hides the background, and 0% means that the color has no coverage ans is fully transparent. To adjust the transparency, in the Graph window, click the Graph Editor button to Start Graph Editor:

Then right click, select Plot region properties, then a window will pop up, you can change your graph's color and opacity manually:

Alternatively, we can type directly in the command pane. For example:

histogram price, color(%20) frequency

In the above command, color(%20) means that markers are to be the default color with 20% opacity. If you want to change the color, you will need to specify the color you want, if you omit the color specification, Stata will adjust only the opacity of the object. NOTE: Normally we use the marker color option mcolor(), we use color() option here because that is where colors are specified for histograms. If you want the marker colors to be red and with 20% opacity, you will need to specify mcolor(red%20):

histogram price, color(red%20) frequency

This will give you a graph with 20% opacity in red:

Similarly, we can use the mcolor() option in a twoway histogram:

twoway (histogram price if foreign, width(500) start(2000) color(red%50) disc freq) (histogram price if !foreign, width(500) start(2000) color(blue%50) disc freq), legend(order(1 "Foreign" 2 "Domestic")) 

It gives you a histogram of price under two different categories: Foreign and Domestic. By adjusting the opacity of the markers, it is easier for us to visualise the price difference:

For more information on Stata graphics, please take a look at the book A Visual Guide to Stata Graphics

2,164 views0 comments

Recent Posts

See All