Stata:向后验图添加参考线
Stata: add a reference line to a graph a posteriori
我使用 graph use
.
加载 Stata *.gph
图形文件
我想加载图形并在 x=123 处向图形添加一条垂直参考线。
我无法从文档中找到是否可以在不借助图形编辑器的情况下执行此操作。 (我需要处理 200 多张图,每张图的参考线值各不相同。)
编辑:我的意思是 "add a reference line",而不是 "ask a reference line"。
在制作图表时添加线条似乎要容易得多,所以如果可能就这样做。如果没有,您可以通过检查图表编辑器的工作方式来完成(如果您记录更改,您可以在打开保存的记录时看到所需的代码,并通过在代码中添加 gr.edit
来使用它)。为此,您需要将所有具有唯一名称的图表放在一个可以查找的目录中,以及一个数据文件(在下面的代码中称为 graph_info_file.dta),其中包含这些变量的图表信息:
图形名称 X1 X2 Y1 Y2
were graphname 是一个带有图形名称的字符串变量(即 graph1.gph、foreign.gph 等),X1 和 Y1 是图形开始的坐标(在您的示例中 X1=123 , Y1=0) 和 X2 和 Y2 直线结束处的坐标(直线为 X2=123,Y2=您的最大 Y 值。
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
}
*
注意局部宏在这里不起作用,所以必须使用全局宏。一般不推荐这样做。如果你想 save/export 图形,只需在循环末尾添加相关代码即可。
另请注意,图形编辑器代码与通常的图形语法不同,更难理解且文档不完善,这通常会导致在不理解其功能的情况下复制代码,从而导致各种错误(通常是很难识别和修复)。
我使用 graph use
.
*.gph
图形文件
我想加载图形并在 x=123 处向图形添加一条垂直参考线。
我无法从文档中找到是否可以在不借助图形编辑器的情况下执行此操作。 (我需要处理 200 多张图,每张图的参考线值各不相同。)
编辑:我的意思是 "add a reference line",而不是 "ask a reference line"。
在制作图表时添加线条似乎要容易得多,所以如果可能就这样做。如果没有,您可以通过检查图表编辑器的工作方式来完成(如果您记录更改,您可以在打开保存的记录时看到所需的代码,并通过在代码中添加 gr.edit
来使用它)。为此,您需要将所有具有唯一名称的图表放在一个可以查找的目录中,以及一个数据文件(在下面的代码中称为 graph_info_file.dta),其中包含这些变量的图表信息:
图形名称 X1 X2 Y1 Y2
were graphname 是一个带有图形名称的字符串变量(即 graph1.gph、foreign.gph 等),X1 和 Y1 是图形开始的坐标(在您的示例中 X1=123 , Y1=0) 和 X2 和 Y2 直线结束处的坐标(直线为 X2=123,Y2=您的最大 Y 值。
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
}
*
注意局部宏在这里不起作用,所以必须使用全局宏。一般不推荐这样做。如果你想 save/export 图形,只需在循环末尾添加相关代码即可。
另请注意,图形编辑器代码与通常的图形语法不同,更难理解且文档不完善,这通常会导致在不理解其功能的情况下复制代码,从而导致各种错误(通常是很难识别和修复)。