Reputation: 773
I load a Stata *.gph
graph file using graph use
.
I would like to load the graph and add a vertical reference line at x=123 to the graph.
I could not find from the documentation whether it was possible to do this without resorting to the graph editor. (I need to treat 200+ graphs and the value of the reference line varies for each graph.)
EDIT: I meant "add a reference line", not "ask a reference line".
Upvotes: 0
Views: 1341
Reputation: 96
It seems it would be far, far easier to add the lines when making the graphs, so if possible do that. If not, you can do it by checking how the graph editor does it (if you record the changes you can see the code needed when opening the saved recording, and use it by adding gr.edit
to the code). For this you need to put all your graphs, with unique names, in a directory were they can be looked up and a datafile (called graph_info_file.dta in the code under) with information on the graphs with these variables:
graphname X1 X2 Y1 Y2
were graphname is a string variable with the name of the graph (i.e graph1.gph, foreign.gph, etc) and X1 and Y1 are the coordinates the graph starts (in your example X1=123, Y1=0) and X2 and Y2 the coordinates where the line ends (a straight line would be X2=123, Y2=your max Y value.
graph dir *, gph
local graphlist = r(list)
di "`graphlist'"
use "graph_info_file.dta", clear
quietly foreach graph in `graphlist' {
noisily di "`graph'"
graph use `graph'
summarize X1 if graphname=="`graph'"
global x1 = r(min)
summarize X2 if graphname=="`graph'"
global x2 = r(min)
summarize Y1 if graphname=="`graph'"
global y1 = r(min)
summarize Y2 if graphname=="`graph'"
global y2 = r(min)
gr_edit .plotregion1.plotregion1[4].AddLine added_lines editor $x1 $y1 $x2 $y2
gr_edit .plotregion1.plotregion1[4].added_lines_new = 1
gr_edit .plotregion1.plotregion1[4].added_lines_rec = 1
gr_edit .plotregion1.plotregion1[4].added_lines[1].style.editstyle linestyle( width(thin) color(black) pattern(solid)) headstyle( symbol(circle) linestyle( width(thin) color(black) pattern(solid)) fillcolor(black) size(medium) angle(stdarrow) backsymbol(none) backline( width(thin) color(black) pattern(solid)) backcolor(black) backsize(zero) backangle(stdarrow)) headpos(neither) editcopy
*add code for saving/exporting graphs here
}
*
Note that local macros do not work here, so have to use global macros. This is generally not recommended. If you want to save/export the graphs just add the relevant code at the end of the loop.
Also note that graph editor code is different from the usual graph syntax, harder to understand and not so well documented, which usually leads to copying code without understanding its function which can lead to all sorts of bugs (which are often very difficult to identify and fix).
Upvotes: 2