假设我twoway在 Stata 中使用一些图形命令。如果我不采取任何操作,Stata 会根据数据中的最小和最大y和x值,以及决定何时更漂亮的某种算法,为y轴和x轴的范围选择一些合理的值。要扩展的范围,例如“0”而不是“0.0139”。精彩的!伟大的。
现在假设在绘制图表之后(或同时),我想在其上添加一些非常重要的文本,并且我想精确地选择文本出现的位置。拥有显示轴的最小值和最大值将很有用:如何获得这些最小值和最大值?(在调用图形命令之前或同时。)
注意:我不是问如何设置y或x轴范围。
我喜欢尼克的建议,但如果您真的下定决心,似乎可以通过检查之后的输出来找到这些值set trace on。这是一些低效的代码,但它们似乎完全符合您的要求。三点注意:
Note: Unmatched quote while processing row XXXX; this can be due to a formatting problem in the file or because a quoted data element spans multiple lines. You should carefully inspect your data after importing. Consider using option bindquote(strict) if quoted data spans multiple lines or option bindquote(nobind) if quotes are not used for binding data.* start a log (critical step for my solution)
cap log close _all
set linesize 255
log using "log", replace text
* make up some data:
clear
set obs 3
gen xvar = rnormal(0,10)
gen yvar = rnormal(0,.01)
* turn trace on, run the -twoway- call, and then turn trace off
set trace on
twoway scatter yvar xvar
set trace off
cap log close _all
* now read the log file in and find the desired info
import delimited "log.log", clear
egen my_string = concat(v*)
keep if regexm(my_string,"forvalues yf") | regexm(my_string,"forvalues xf")
drop if regexm(my_string,"delta")
split my_string, parse("=") gen(new)
gen axis = "vertical" if regexm(my_string,"yf")
replace axis = "horizontal" if regexm(my_string,"xf")
keep axis new*
duplicates drop
loc my_regex = "(.*[0-9]+)\((.*[0-9]+)\)(.*[0-9]+)"
gen min = regexs(1) if regexm(new3,"`my_regex'")
gen delta = regexs(2) if regexm(new3,"`my_regex'")
gen max_temp= regexs(3) if regexm(new3,"`my_regex'")
destring min max delta , replace
gen max = min + delta* int((max_temp-min)/delta)
*here is the info you want:
list axis min delta max
Run Code Online (Sandbox Code Playgroud)