

I have tried adding axis(2) to the text box as an additional argument, but this option is apparently not allowed. It seems better to be able to attach the text to the line using the second y-axis gridpoints. Of course, I could manually determine this value for the histogram (as I did in this example), but I plan to create a set of these graphs via a loop, such that there are a dozen or so panels. The corresponding lines would instead be: text(1 `median' `"median = $`=string(`median',"%6.2f")'"', /// Instead of "0.08" which gets my text into the correct location, I would like to use "1", as this is the maximum value of the second y-axis. gr7, oneway uses several colours with several variables. stripplot allows several colours in the body of the graph with its separate() option. dotplot uses only one colour in the body of the graph. This is the reason I used the second y-axis in the first place. Similarly, stripplot and dotplot allow the use of xline() and yline(). This produces the desired graph (for the most part*):Īt issue is that I do not know the height of the histogram beforehand. Scatteri 0 `median' 1 `median', recast(line) yaxis(2) /// Twoway hist mpg, start(10) width(5) || /// Here is an example of my issue: sysuse auto However, I cannot seem to add text to this line using the second y-axis coordinates (it instead uses the first y-axis coordinates, even though the object it is labeling is on the second y-axis). To fix this, I am using scatteri with coordinates.īecause I do not know the height of the histogram beforehand, I set the scatteri plot to use a separate y-axis, which I have hidden. I cannot use xline() because the line falls behind the histogram, rather than on top of it.

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.I have a plot of a histogram, to which I would like to add the median. 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.
