

What is "n(#)" for? This tells Stata to draw the plot at 20 points. In our case, we specified that the new plot type is a connected graph, for which you can specify marker symbols. The recast option will tell Stata to treat the plot as a new plot. Tw function y = x^2, range(-2 2) recast(connected) msymbol(O) || ///įunction y = x^3, range(-2 2) recast(connected) msymbol(T) n(20) || ///įunction y = x^4, range(-2 2) recast(connected) msymbol(S) n(20) /// For example:įunction y = x^3, range(-2 2) lpattern(-) || ///įunction y = x^4, range(-2 2) lpattern(.-) ///Ĭols(3) pos(5) ring(0) region(lcolor(none)))īut suppose you prefer to use marker symbols rather than line patterns to differentiate the line plots, how can you specify this option? Use the recast option (-help advanced_options-): These options are particularly helpful when you have many functions to plot. Tw function y = 4*x^2, range(-2 2) lcolor(red) lwidth(medthick) lpattern(-) For example, if you want to change the color, width, and pattern of the line use lcolor, lwidth, and lpattern options: It is easy to change the line attributes of the plot. If you want to draw the other side of the parabola or change the range, you can specify the range as follows: The default is that the function is drawn over the range. For example, the half of a parabola with the equation y = x^2 can be drawn by typing: You want a line which corresponds with 0 on the second y axis: (scatteri 0 `t1' 0 `t2', recast(line) yaxis(2))įor similar problems, one should be able to calculate the appropriate coordinates from the data in each case or fall back on a constant variable plotted as a horizontal line graph.ĮDIT: Revised version benefits from comments by Roberto Ferrer.Stata's -graph twoway function- draws the line plot of a specified function. As we supply two pairs of coordinates the result is a single straight line (which happens to be horizontal).

Now the key is to fire up twoway scatteri but to recast the two points (in this case) to a line. In other examples, this may be dispensable. The technique below is more general in not assuming that the added line need be horizontal.įirst for convenience, we calculate the endpoints of the line and put them in local macros. One way to do that is just to define a variable which is constant and plot it as a line graph. So, to subvert that you have to supply your line as data. In essence, the idea behind yline() and similar options is to supply reference lines, which are never to be plotted on top of the data. Is it possible to force Stata to place this horizontal line on top of all other plots in the graph?

#Add xline stata series
The horizontal line is obscured by the shading because the shading is created first, but I need the shading to be created first because otherwise, it obscures the plot of the time series (since Stata doesn't support transparency/alpha blending).
#Add xline stata code
In practice, the indicator variable could be anything, which is why I don't want to hard code the values to shade in the twoway command, as seen in this Statalist post or the nber plugin in the SSC. Ly_dev is the time series, and USRECQ is the indicator variable. Ytitle("Deviation from trend", axis(2))),
#Add xline stata install
Here is an example, using the freduse plugin ( ssc install freduse, replace): freduse GDPC1 USRECQ, clear I want to draw a horizontal line at the point on the y-axis where the time series equals 0. Time periods are shaded if the indicator variable equals 1, and not shaded if it's missing.
#Add xline stata windows
I'm graphing a time series and shading certain time periods based on a Boolean indicator variable in Stata 13.1 on Windows 7.
