Search code examples
graphstata

Stata: Place label on top of vertical line in twoway graph


I would like to place a label on top of a vertical line as done in the example below. I am looping over a variety of variables and therefore the y position I have to specify in the ttext() option varies. How could I automate that it gets placed on top of the vertical line?

(I tried to get the maximum of a variable into a scalar. However, the ceiling of the maximum does not always match the highest y value Stata chooses for your graph.)

twoway (tsline var, recast(connected)), tline(642) ttext(106 642 "start")

Solution

  • I would just use an axis label on the top axis for this purpose. It's at the top of the line, which seems the essence here.

    . webuse grunfeld
    
    . set scheme s1color
    
    . tsline kstock if company == 1 , xaxis(1 2) xla(1940 "start", axis(2) grid glcolor(red)) xtitle("", axis(2))
    

    enter image description here

    So, you must declare that you want two x axes. Then the vertical line can just be a grid line associated with the axis label. An axis title will spring up by default. In the example above, it is just blanked out with an empty string, but clearly you can display other text in the axis title if you wish.