This post shows how to prepare a spike graph in STATA. You can think of a spike graph as a bar graph with narrow spikes rather than bars, which in some contexts may look more elegant and simplistic.

The section below will simulate data for our spike graph. The data will show how often an event occurs (how many times each year) over a 20-ish year period. To spice it up a little bit, the data shows more frequent occurrence of the event starting from year 2010.

``````clear
set seed 111
set obs 21
generate year = 1999 + _n

g count = rpoisson(5)
replace count = count + rpoisson(2) if year > 2009
list, clean noobs``````

Next, we prepare our spike graph, with minimal use of options at this point.

``````twoway (spike count year)
``````

Now, let’s add titles to our graph: main title, y-axis title and x-axis title. All three in special color that will eventually match the color of all other elements in the graph.

``````twoway (spike count year), ///
ytitle("Number of events per year", color(edkblue)) ///
xtitle(Time (Year), color(edkblue)) ///
title(Number of events over time, color(edkblue))``````

Now let’s modify the labels of our graph a little bit: on X-axis (time series) let’s mark every second year in our sample by saying that we want 11 labels for a 21 year period, and change the angle of the labels to 45 degrees. Also change the color of the labels on both axis. Remove grids and ticks on both axis too.

``````twoway (spike count year), ///
ytitle("Number of events per year", color(edkblue)) ///
xtitle(Time (Year), color(edkblue)) ///
title(Number of events over time, color(edkblue)) ///
ylabel(, nogrid notick labcolor(edkblue)) ///
xlabel(#11, angle(forty_five) notick labcolor(edkblue))``````

Since we simulated our data such that the event occurs more frequently after year 2010, let’s add a vertical line to mark that time period (specifically, at 2009.5 so that line doesn’t over lap with one of the data points). Also, add a specific text to the graph which explains when does the change in data happed (e.g. Regulation Change). Note that the text option uses numbers 14 & 2015 which are simply the coordinates for the text we added: y = 15 and x = 2015. This is a very useful option that you can use to add any comment/note to your graph.

``````twoway (spike count year), ///
ytitle("Number of events per year", color(edkblue)) ///
xtitle(Time (Year), color(edkblue)) ///
title(Number of events over time, color(edkblue)) ///
ylabel(, nogrid notick labcolor(edkblue)) ///
xlabel(#11, angle(forty_five) notick labcolor(edkblue)) ///
xline(2009.5, lcolor(cranberry)) ///
text(14 2015 "Regulation Change", color(cranberry))``````

Finally, we change the spike color, as well as the color of the background of the graph.

``````twoway (spike count year, lcolor("236 196 77")), ///
ytitle("Number of events per year", color(edkblue)) ///
xtitle(Time (Year), color(edkblue)) ///
title(Number of events over time, color(edkblue)) ///
ylabel(, nogrid notick labcolor(edkblue)) ///
xlabel(#11, angle(forty_five) notick labcolor(edkblue)) ///
yscale(lcolor(edkblue))	xscale(lcolor(edkblue))	///
xline(2009.5, lcolor(cranberry)) ///
text(14 2015 "Regulation Change", color(cranberry)) ///
graphregion(fcolor(white))``````